8 Replies Latest reply on Dec 13, 2007 1:05 AM by 807601

# TreeSet

How do I sort through integers with TreeSet:

I have the following code, but it doesn't work when two integers are equal. Is there any way around this?

Examples of input:
3 4 5
5 2 5
3 7 8
99 99 99

``````TreeSet t1 = new TreeSet();

Iterator<Integer> it1 = t1.iterator();

while (it1.hasNext()) {
side1 = it1.next();
side2 = it1.next();
side3 = it1.next();
}``````
• ###### 1. Re: TreeSet
My crystal ball tells me you are living in denial of the class Triangle.
• ###### 2. Re: TreeSet
I'm sorry what do you mean by this?
• ###### 3. Re: TreeSet
What are side1, side2 and side3?
• ###### 4. Re: TreeSet
from your code, it seems that you just need to sort a few integers

why not put them in an array or list and then use Arrays.sort() or Collections.sort()?
• ###### 5. Re: TreeSet
Yes I was actually just looking at that

I can't figure that out either. Currently I have the code:

`````` Integer[] numbers= {num1, num2, num3};
Integer[] convertedNumbers = null;
Arrays.sort(numbers);
Arrays.fill(convertedNumbers, 0, 2, num3);``````
I am getting it to sort, but I don't really know how to use it after this.
• ###### 6. Re: TreeSet
DrLaszloJamf the sides are as follows:

3 4 5
5 2 5
3 7 8
99 99 99
7 12 -38
8 0 5
9 3 4
• ###### 7. Re: TreeSet
needhelp83 wrote:
DrLaszloJamf the sides are as follows:
I believe he was asking what do they represent. Not what are the actual values.
• ###### 8. Re: TreeSet
Alright I got it.

I guess that was a dumb statement earlier that I couldn't use the data earlier.

I was trying to use System.out.println with my sorted array and it was coming out incorrectly.

I am now using this code which works for what I needed:

Integer[] numbers= {num1, num2, num3};
Arrays.sort(numbers);
side1 = numbers[0];
side2 = numbers[1];
side3 = numbers[2];