import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class HashSetTest {
public static void main(String[] args) {
Set<Integer> set=new HashSet<Integer>();
set.add(90);
set.add(10);
set.add(9);
set.add(100);
set.remove(9);
System.out.println("----------第一种方法--toSring-----------");
System.out.println(set);
System.out.println("----------第二种方法--for-----------");
int length=set.size();
for(int i=0;i<length;i++) {
}
System.out.println("-----------第三种--forEach---------------");
for(Object o:set) {
System.out.println(o);
}
System.out.println("-----------第四种--迭代器---------------");
Iterator iterator=set.iterator();
while(iterator.hasNext()) {
Object o=iterator.next();
System.out.println(o);
}
System.out.println("--------------排序--------------");
Object[]array=set.toArray();
Arrays.sort(array);
for(Object object:array) {
System.out.println(object);
}
}
}
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
public class LinkListETest {
public static void main(String[] args) {
List<Integer> list = new LinkedList<Integer>();
list.add(10);
list.add(5);
list.add(11);
list.add(33);
list.add(9);
list.add(7);
System.out.println(list);
list.sort(new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
return i2-i1;
}
});
System.out.println("排序后");
System.out.println(list);
System.out.println("第二种排序");
Collections.sort(list);
System.out.println(list);
System.out.println("第三种排序");
Collections.sort(list,new Comparator<Integer>() {
public int compare(Integer o1,Integer o2) {
return o2-o1;
}});
System.out.println(list);
System.out.println("第四种排序");
MyComparator mycom=new MyComparator();
Collections.sort(list,mycom);
System.out.println(list);
}
}
import java.util.Comparator;
public class MyComparator implements Comparator<Integer>{
public class myComparator implements Comparator<Integer>{
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
}
@Override
public int compare(Integer o1, Integer o2) {
return 0;
}
}
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
Set set=new TreeSet<Integer>();
set.add(20);
set.add(20);
set.add(90);
set.add(10);
set.add(6);
set.add(30);
set.remove(0);
System.out.println("---------------第一种--toString--------------------");
System.out.println(set);
System.out.println("---------------第二种----------------------");
for(Object o:set) {
System.out.println(o);
}
System.out.println("---------------第三种----------------------");
Iterator iterator=set.iterator();
while(iterator.hasNext()) {
Object o=iterator.next();
System.out.println(o);
}
System.out.println("--------------排序--------------");
Object[]array=set.toArray();
Arrays.sort(array);
for(Object object:array) {
System.out.println(object);
}
}
}