ArrayList
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("d");
Iterator<String> ite = list.iterator();
while(ite.hasNext()) {
System.out.print(ite.next() + "\t");
}
// ListIterator 专门用来遍历List的
ListIterator<String> listite = list.listIterator();
while (listite.hasNext()) {
String str = listite.next();
System.out.println(str);
}
// -----------------------------
for (String str : list) {
System.out.print(str + "\t");
}
LinkedList
List<String> link = new LinkedList<>();
link.add("e");
link.add("f");
link.add("g");
Iterator<String> ite = link.iterator();
while(ite.hasNext()) {
System.out.print(ite.next() + "\t");
}
for (String str : link) {
System.out.print(str + "\t");
}
Set
Set<String> sets = new HashSet<>();
sets.add("z");
sets.add("x");
sets.add("c");
Iterator<String> setIte = sets.iterator();
while (setIte.hasNext()) {
String string = (String) setIte.next();
System.out.println(string);
}
Map
Map<Integer, String> map = new HashMap<>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
Set<Entry<Integer, String>> set = map.entrySet();
Iterator<Entry<Integer, String>> iterator = set.iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, String> entry = (Map.Entry<Integer, String>) iterator.next();
String value = entry.getValue();
Integer key = entry.getKey();
System.out.println(key + "=" + value);
}
for (Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}