1.ArrayList
package CollentionResult;
import java.util.ArrayList;
import java.util.Collections;
public class ListResult {
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>();
ArrayList arrayList2 = new ArrayList<Integer>();
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);
arrayList.add(4);
arrayList.add(5);
arrayList.add(6);
arrayList.add(7);
arrayList.add(8);
arrayList.add(9);
arrayList.add(10);
for(int i=0;i<5;i++) {
arrayList2.add(i);
}
arrayList.remove(0);
arrayList.set(1, -1);
int num = arrayList.get(3);
System.out.println(arrayList2.get(2).getClass());
Collections.sort(arrayList);
System.out.println(arrayList);
}
}
2.HashSet
package CollectionResult;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.function.Consumer;
public class DemoHashSet {
public static void main(String[] args) {
HashSet hashSet = new HashSet<String>();
HashSet hashSet_genericity = new HashSet<Integer>();
hashSet.add("b");
hashSet.add("5");
hashSet.add("1");
hashSet.add("2");
hashSet.add("3");
hashSet.add("7");
hashSet.add("4");
hashSet.add("6");
hashSet.remove("24");
System.out.println("利用lambda进行遍历");
hashSet.forEach(obj->System.out.print(obj + " "));
System.out.println();
System.out.println("利用遍历器Iterator进行遍历");
Iterator iterator = hashSet.iterator();
while(iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("6-1使用TreeSet进行排序");
TreeSet treeSet = new TreeSet(hashSet);
treeSet.forEach(obj->System.out.print(obj + " "));
System.out.println();
System.out.println("6-1-2:");
TreeSet treeSet2 = new TreeSet(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
});
treeSet2.addAll(hashSet);
treeSet2.forEach(obj->System.out.print(obj + " "));
System.out.println();
System.out.println("6-1-3:");
TreeSet treeSet21 = new TreeSet((o1,o2)->((String) o2).compareTo((String) o1));
treeSet21.addAll(hashSet);
treeSet21.forEach(obj->System.out.print(obj + " "));
System.out.println();
System.out.println("6-2使用ArrayList进行排序");
ArrayList arrayList = new ArrayList(hashSet);
Collections.sort(arrayList);
arrayList.forEach(obj->System.out.print(obj + " "));
System.out.println();
System.out.println("6-3使用stream()进行排序");
hashSet.stream().sorted().forEach(obj->System.out.print(obj + " "));
System.out.println();
hashSet.stream().sorted((o1,o2)->((String) o2).compareTo((String) o1)).forEach(obj->System.out.print(obj + " "));
System.out.println();
System.out.println("6-4使用Iterator进行排序");
}
}
3.LinkedHashSet
package CollectionResult;
import java.util.LinkedHashSet;
import java.util.TreeSet;
public class DemoLinkedHashSet {
public static void main(String[] args){
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<Integer>();
linkedHashSet.add(3);
linkedHashSet.add(2);
linkedHashSet.add(4);
linkedHashSet.add(1);
TreeSet treeSet = new TreeSet<Integer>(linkedHashSet);
treeSet.forEach(obj->System.out.println(obj));
linkedHashSet.stream().sorted().forEach(num->System.out.println(num));
}
}
4.HashMap
package CollectionResult;
import java.util.HashMap;
import java.util.Map.Entry;
public class DemoHashMap {
public static void main(String[] args) {
HashMap<Integer,Integer> hashMap = new HashMap<Integer,Integer>();
for(int i=1;i<10;i++) {
hashMap.put(i, i);
}
for(Entry<Integer,Integer> entry : hashMap.entrySet()) {
entry.getKey();
entry.getValue();
}
}
}