-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.java
More file actions
51 lines (40 loc) · 1.08 KB
/
algorithm.java
File metadata and controls
51 lines (40 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package Chapter13ConnectionInterface;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class algorithm {
public static void main(String[] args) {
Set<String> a=new HashSet<>();
Set<String> b=new HashSet<>();
a.add("12");
b.add("12");
a.add("23");
a.add("34");
Set<String> result=new HashSet<>(a);
//返回即在a中也在b中出现的元素
result.retainAll(b);
//将set转换成数组,,没有创建任何新的对象
//a.toArray(new String[a.size()]);
//System.out.println(a);
//排序
//比较器
Comparator<Integer> comparator=new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
return o1.compareTo(o2);
}
};
List<Integer> lista=new ArrayList<>();
lista.add(123);
lista.add(23);
lista.add(89);
//按照比较器中的顺序
//Collections.sort(lista, comparator);
//比较器中逆序
Collections.sort(lista,Collections.reverseOrder(comparator));
System.out.println(lista);
}
}