Use Lambda expression to output Map key-value pairs

A method that outputs a HashMap that I saw in "Java Core Technology Volume 1" feels very high:

public static void main(String[] args) {
	// Create a HashMap using generics
	Map<String, String> map = new HashMap<String, String>();
		
	//add key-value pair
	map.put("zhangsan", "15254305006");
	map.put("lisi", "15254305068");
	map.put("wangwu", "13405786007");

	//Use keySet to get all key values
	Set<String> ketSet = map.keySet();
	Iterator<String> iterator = ketSet.iterator(); // get the iterator
		
	//Use Lambda method to output
	System.out.println("lambda output:");
	iterator.forEachRemaining(element -> {
		String value = map.get(element);
		System.out.println(element + "->" + value);
	});

}
Because reading books is relatively crude, and my level is limited, the specifics are mentioned on page P438 of the book .

General idea : call the forEachRemaining method (Java SE 8 and later) and provide a lambda expression. This lambda expression is called on each element of the iterator until there are no more elements.


This paragraph is taken from the book:

The order in which elements are accessed depends on the collection type. How to iterate over an ArrayList, the iterator will start at index 0, and the index value will be incremented by 1 if it is not iterated once. However, if the elements in the HashSet are accessed, each element will appear in some random order. Although it is certain that each element in the collection can be traversed during the iteration process, the order in which the elements are accessed cannot be predicted. This is not a problem for order-independent operations such as calculating a sum or counting the number of elements that meet a certain condition.

In addition, there are several methods of traversing the Map seen in the textbook:

  1. Using an iterator, use the hashNext() method to traverse:
    public static void main(String[] args) {
    
    	// create object and add key-value pair
    	Map<String, String> map = new HashMap<String, String>();
    	map.put("zhangsan", "15254305006");
    	map.put("lisi", "15254305068");
    	map.put("wangsu", "13405786007");
    
    	// Method 1 uses keySet to get all key values
    	Set<String> ketSet = map.keySet();
    	Iterator<String> iterator = ketSet.iterator(); // get the iterator
    		
    	System.out.println("\nMode 1 output: ");
    	iterator = ketSet.iterator();
    	while (iterator.hasNext()) {
    		String key = iterator.next();
    		String value = map.get(key);
    		System.out.println(key + "->" + value);
    	}
    }
  2. Use Collection to get the value of Map directly:
    public static void main(String[] args) {
    
    	// create object and add key-value pair
    	Map<String, String> map = new HashMap<String, String>();
    	map.put("zhangsan", "15254305006");
    	map.put("lisi", "15254305068");
    	map.put("wangsu", "13405786007");
    
    	// Method 2 use values ​​to get all the values
    	System.out.println("\nMode 2 output: ");
    	Collection<String> collection = map.values();
    	for (String value : collection) {
    		System.out.println(value);
    	}
    }
  3. Use entrySet to get the set of key-value pairs:
    public static void main(String[] args) {
    
    	// create object and add key-value pair
    	Map<String, String> map = new HashMap<String, String>();
    	map.put("zhangsan", "15254305006");
    	map.put("lisi", "15254305068");
    	map.put("wangsu", "13405786007");
    
    	// Method 3 uses entrySet to get the set of key-value pairs
    	System.out.println("\nMode 3 output: ");
    	Set<Entry<String, String>> keyValues = map.entrySet();
    	for (Entry<String, String> entry : keyValues) {
    		System.out.println(entry.getKey() + "->" + entry.getValue());
    	}
    }



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325688805&siteId=291194637