1、List
2、Set
3、Map
package com.an.collection; import java.util.*; import java.util.Map; /** * @description: * @author: anpeiyong * @date: Created in 2020/3/4 9:30 * @since: */ public class MapTest { public static void main(String[] args) { // iteratorTest(); // iteratorTest2(); // foreachTest(); // foreachTest2(); // foreachMethodTest(); valuesTest(); } public static void iteratorTest(){ HashMap<String,String> map=new HashMap<>(); map.put("1","a"); map.put("2","b"); map.put("3","c"); Set<String> keySet=map.keySet(); Iterator<String> iterator =keySet.iterator(); while (iterator.hasNext()){ String key=iterator.next(); System.out.println(map.get(key)); } } public static void iteratorTest2(){ HashMap<String,String> map=new HashMap<>(); map.put("1","a"); map.put("2","b"); map.put("3","c"); Set<Map.Entry<String,String>> entrySet=map.entrySet(); Iterator<Map.Entry<String,String>> entryIterator=entrySet.iterator(); while (entryIterator.hasNext()){ Map.Entry<String,String> entry=entryIterator.next(); String key=entry.getKey(); String value=entry.getValue(); System.out.println(key+":"+value+"=="); } } public static void foreachTest(){ HashMap<String,String> map=new HashMap<>(); map.put("1","a"); map.put("2","b"); map.put("3","c"); Set<String> keySet=map.keySet(); for (String s:keySet) { System.out.println(map.get(s)); } } public static void foreachTest2(){ HashMap<String,String> map=new HashMap<>(); map.put("1","a"); map.put("2","b"); map.put("3","c"); Set<Map.Entry<String,String>> entrySet=map.entrySet(); for (Map.Entry<String,String> s:entrySet) { System.out.println(s.getKey()+":"+s.getValue()); } } public static void foreachMethodTest(){ HashMap<String,String> map=new HashMap<>(); map.put("1","a"); map.put("2","b"); map.put("3","c"); map.forEach((k,v)->{ System.out.println(k+":"+v); }); } public static void valuesTest(){ HashMap<String,String> map=new HashMap<>(); map.put("1","a"); map.put("2","b"); map.put("3","c"); Collection<String> collection =map.values(); System.out.println(collection); } }
原文:https://www.cnblogs.com/anpeiyong/p/12407776.html