# TreeSet集合排序问题

得到结果{4,2,6,1,7,8}

```import java.util.*;
public class Text {

public static void main(String[] args) {
int[] s={4,2,4,6,1,2,4,7,8};
for(int x:quChong(s))
System.out.print(x+" ");

}
public static int[] quChong(int[] arr){
TreeSet<Integer> a=new TreeSet(new MyCompare());
for(int x=0;x<arr.length;x++){
}
System.out.println(a);
int[] i=new int[a.size()];
int temp=0;
for(Integer j:a){

i[temp++]=j;
}
return i;
}

}

//自定义比较器，使元素按照存入的先后顺序存储
class MyCompare implements Comparator{
public int compare(Object o1,Object o2){
int num;
Integer i1=(Integer)o1;
Integer i2=(Integer)o2;
num=i1.compareTo(i2);
if(num==0)
return 0;
return 1;
}
}```

0
Integer[] s = {4,2,4,6,1,2,4,7,8};
List<Integer>list = Arrays.asList(s);
List<Integer> ret = new ArrayList<Integer>();
for (Integer integer : list) {
if(!ret.contains(integer)) {
}
}

System.out.println(ret);

-------------------------------------------------------------------

import java.util.Comparator;
import java.util.TreeSet;

public class Test {

public static void main(String[] args) {
int[] s = { 4, 2, 4, 6, 1, 2, 4, 7, 8 };
for (int x : quChong(s))
System.out.print(x + " ");

}

public static int[] quChong(int[] arr) {
TreeSet<Integer> a = new TreeSet(new MyCompare());
for (int x = 0; x < arr.length; x++) {
System.out.println("------------------------------");
System.out.println("aaaaaaaaaaaa" + a);
}
int[] i = new int[a.size()];
int temp = 0;
for (Integer j : a) {

i[temp++] = j;
}
return i;
}

}

// 自定义比较器，使元素按照存入的先后顺序存储
class MyCompare implements Comparator {
public int compare(Object o1, Object o2) {
int num;
System.out.println("o1:"+o1);
System.out.println("o2:"+o2);
Integer i1 = (Integer) o1;
Integer i2 = (Integer) o2;
System.out.println("i1:"+i1);
System.out.println("i2:"+i2);
//-1 小于   0 等于   1
num = i1.compareTo(i2);
System.out.println("num:"+num);
if (num == 0)
return 0;
return 1;
}
}

------------------------------
o1:4
o2:2
i1:4
i2:2
num:1
o1:4
o2:6
i1:4
i2:6
num:-1
o1:4
o2:1
i1:4
i2:1
num:1
aaaaaaaaaaaa[4, 2, 6, 1, 4]
------------------------------

0
num=i1.compareTo(i2); 会有 1，-1，0  三个值。 按你这MyCompare 写的意思，只要o1，o2不等，o1 就大于o2？

0
```import java.util.HashMap;
import java.util.Map;

public class Test {

public static void main(String[] args) {
int[] a = new int[] { 4, 2, 4, 6, 1, 2, 4, 7, 8 };
Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();
for (int i = 0; i < a.length; i++) {
if (map.get(a[i]) == null) {
System.out.println(a[i]);
map.put(a[i], Boolean.TRUE);
}
}
}
}```