1. Five traversal methods of Map
package com.grandage.test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MapTraversing {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("key1"."value1");
map.put("key2"."value2");
map.put("key3"."value3");
//1. This parameter is widely used and can be used twice
System.out.println("Traversal of keys and values by map. keySet");
for (String key : map.keySet()) {
System.out.println("key= " + key + " and value= " + map.get(key));
}
/ / 2.
System.out.println(Iterator iterator through key and value via map. entrySet:);
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//3. Recommended, especially for large capacity
System.out.println("Traversal key and value via map. entrySet");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
Map.values() traverses all values but not keys
for (String v : map.values()) {
System.out.println("value= " + v);
}
Lambda traverses the map
map.forEach((k,v)-> System.out.println(k+"-<key=====value->"+v)); }}Copy the code
2.Set traversal 4 methods
package com.utils;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
public class SetTraversing {
public static void main(String[] args) {
//Hashset does not guarantee the iteration order of the collection; In particular, it does not guarantee the constancy of the order
Set<String> set = new HashSet<>();
set.add("value1");
set.add("value2");
set.add("value3");
//LinkedHashSet defines the iteration order in which elements are inserted into the collection (insertion order)
Set<String> setOrder = new LinkedHashSet<>();
setOrder.add("value1");
setOrder.add("value2");
setOrder.add("value3");
//1. Iterate through
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String str = it.next();
System.out.println(str);
}
//2
for (String str : setOrder) {
System.out.println(str);
}
//2-1. The enhanced for loop also has the advantage of being generic if :set holds Object
Set<Object> setObject = new HashSet<>();
setObject.add(111);
setObject.add("222");
for (Object obj : setObject) {
if (obj instanceof Integer) {
int resultValue = (Integer) obj;
System.out.println("Integer:" + resultValue);
} else if (obj instanceof String) {
String resultValue = (String) obj;
System.out.println("String:"+ resultValue); }}//3. Common traversal of collection classes, from very early versions, using iterators
for (Iterator it2 = set.iterator(); it2.hasNext(); ) {
System.out.println(it2.next());
}
/ / 4. Jdk1.8 laterset.forEach(System.out::println); }}Copy the code
3. Five ways to traverse a List collection
package com.utils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class ListTraversing {
public static void main(String[] args) {
// Ordered elements can be iterated repeatedly
List<String> arrayList=new ArrayList<>();
arrayList.add("value1");
arrayList.add("value2");
//1. Foreach traversal (without index recommendation)
for(String str:arrayList){
System.out.println(str);
}
//2.
for (int i=0,n=arrayList.size(); i < n ; i++) {
System.out.println(arrayList.get(i));
}
//3. Iterator traversal
Iterator<String> it = arrayList.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
/ / 4. Jdk1.8 later
arrayList.forEach(System.out::println);
/ / 5. Use a ListIterator
ListIterator<String> listIt = arrayList.listIterator();
while(listIt.hasNext()) { System.out.println(listIt.next()); }}}Copy the code
All of the blog’s work is licensed under CC0 1.0 Generic (CC0 1.0) public domain contributions