public class ListDemo {
public void list() {
List
ArrayList arrayList = new ArrayList<>(array);
System.out.println(arrayList.toString());
for (Object object : array) {
System.out.println(object);
}
}
public void list1() {
List
ArrayList arrayList = new ArrayList<>(array);
Iterator iterator = arrayList.iterator();
for (; iterator.hasNext();) {
Integer object = (Integer) iterator.next();
System.out.println(object);
}
}
public void list2() {
List
ArrayList arrayList = new ArrayList<>(array);
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()) {
Object object = iterator.next();
System.out.println(object);
}
}
@Test
public void list3() {
List
ArrayList arrayList = new ArrayList<>(array);
Iterator iterator = arrayList.iterator();
do {
Object object = iterator.next();
System.out.println(object);
} while (iterator.hasNext());
}
public class SetDemo {
// TreeSet实现了SortedSet接口,底层是TreeMap,实现了comparator方法,默认为升序排列的
// 比较的时候如果值的类型不一样,会抛出ClassCastException,如果add(null),会抛出NullPointerException
public void treeSet() {
TreeSet
// hashset底层是hashmap,不会记录插入的顺序
public void hashSet() {
HashSet
hashSet.add(22);
hashSet.add(44);
hashSet.add(33);
hashSet.add("22");
hashSet.add(11);
hashSet.add(11);
hashSet.add(null);
for (Object object : hashSet) {
System.out.println("forEach----" + object);
}
Iterator
while (iterator.hasNext()) {
System.out.println("iteratorWhile-----" + iterator.next());
}
Iterator
do {
System.out.println("iteratorDoWhile-----" + iteratorOne.next());
} while (iteratorOne.hasNext());
}
// LinkedHashSet底层是hashmap,记录插入的顺序
public void LinkHashSet() {
LinkedHashSet
linkedHashSet.add(22);
linkedHashSet.add(44);
linkedHashSet.add(33);
linkedHashSet.add("22");
linkedHashSet.add(11);
linkedHashSet.add(11);
linkedHashSet.add(null);
for (Object object : linkedHashSet) {
System.out.println("forEach----" + object);
}
Iterator
while (iterator.hasNext()) {
System.out.println("iteratorWhile-----" + iterator.next());
}
Iterator
do {
System.out.println("iteratorDoWhile-----" + iteratorOne.next());
} while (iteratorOne.hasNext());
}
}
public void list4() {
List
LinkedList
for (int i = 0; i < linkedList.size(); i++) {
Object object = linkedList.get(i);
System.out.println(object);
}
}
}
public class MapDemo {
// treeMap是有序的,默认为升序排列的,key的类型必须为一致,key不能为null,key重复时,会将后边的key覆盖掉前边的key
public void TreeMap() {
TreeMap
treeMap.put(2, "xiao");
treeMap.put(1, "xiao");
treeMap.put(3, "xiaoss");
treeMap.put(1, "xiaos");
Set
for (Entry
System.out.println(entry2.getKey() + "-111-" + entry2.getValue());
}
Iterator
while (iterator2.hasNext()) {
Entry
System.out.println(entry1.getKey() + "-333-" + entry1.getValue());
}
Set
for (Object key : keySet) {
System.out.println(key + "-444-" + treeMap.get(key));
}
Iterator
while (iterator.hasNext()) {
Object key = (Object) iterator.next();
Object value = treeMap.get(key);
System.out.println(key + "-666-" + value);
}
}
public void hashMap() {
HashMap
hashMap.put(2, "xiao");
hashMap.put(3, "xiao");
hashMap.put(4, "xiaoss");
hashMap.put(1333, "xiaos11");
hashMap.put(null, "xiaos11");
Set
for (Entry
System.out.println(entry2.getKey() + "-111-" + entry2.getValue());
}
Iterator
while (iterator2.hasNext()) {
Entry
System.out.println(entry.getKey() + "-333-" + entry.getValue());
}
Set
for (Object key : keySet) {
System.out.println(key + "-444-" + hashMap.get(key));
}
Iterator
while (iterator.hasNext()) {
Object key = (Object) iterator.next();
Object value = hashMap.get(key);
System.out.println(key + "-666-" + value);
}
}
@Test
public void linkedhashMap() {
LinkedHashMap
linkedHashMap.put(2, "xiao");
linkedHashMap.put(3, "xiao");
linkedHashMap.put(4, "xiaoss");
linkedHashMap.put(1, "xiaos11");
linkedHashMap.put(null, "xiaos11");
Set
for (Entry
System.out.println(entry2.getKey() + "-111-" + entry2.getValue());
}
Iterator
while (iterator2.hasNext()) {
Entry
System.out.println(entry.getKey() + "-333-" + entry.getValue());
}
Set
for (Object key : keySet) {
System.out.println(key + "-444-" + linkedHashMap.get(key));
}
Iterator
while (iterator.hasNext()) {
Object key = (Object) iterator.next();
Object value = linkedHashMap.get(key);
System.out.println(key + "-666-" + value);
}
}
}