Map的四种取值方式

时间:2025-03-16 08:35:59
```java public void testMap(){ Map<String,String> map = new HashMap<>(); map.put("1","first"); map.put("2","second"); map.put("3","three"); //输出所有value值 // ("-----------" + ()); //根据遍历key值获取所有value值 // for (String key : ()){ // String value = (key); // ("-----------" + value); // } //通过遍历key和value,容量大时推荐使用 // for (<String, String> entry : ()) { // ("key= " + () + " and value= " + ()); // } //根据keySet()使用迭代器获取所有value值(性能较低) // Iterator<String> it = ().iterator(); // while (()){ // String key = (); // String value = (key); // ("-------------" + value); // } //根据entrySet()使用迭代器获取所有value值(性能较高) // Iterator<<String,String>> it = ().iterator(); // while (()){ // <String, String> entry = (); // String key = (); // String value = (); // (key + "--------" + value); // } }