推荐,尤其是容量大时
Map<String,String> map = new HashMap<>(); for (Map.Entry<String,String> entry : map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); }
避免使用这种方式:
Map<String,String> map = new HashMap<>(); for (String key : map.keySet()) { String value = map.get(key); }
遍历Map的四种方法:
第一种:通过Map.keySet遍历key和value
for (String key : map.keySet()) { System.out.println("key= "+ key + " and value= " + map.get(key)); }
第二种:通过Map.entrySet使用iterator遍历key和value
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()); }
第三种:通过Map.entrySet遍历key和value,推荐,尤其是容量大时
for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); }
第四种:通过Map.values()遍历所有的value,但不能遍历key
for (String v : map.values()) { System.out.println("value= " + v); }
javap比较entrySet()keySet()的区别:
public class Test5 { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("1", "value1"); map.put("2", "value2"); map.put("3", "value3"); //第一种:普遍使用,二次取值 System.out.println("通过Map.keySet遍历key和value:"); for (String key : map.keySet()) { System.out.println("key= "+ key + " and value= " + map.get(key)); } //第三种:推荐,尤其是容量大时 System.out.println("通过Map.entrySet遍历key和value"); for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); } } }
class文件内容如下:
Classfile /D:/project/prism/NMPA/sdjg-rest/target/test-classes/com/ccb/jawa/rest/Test5.class Last modified 2022-1-16; size 1735 bytes MD5 checksum d0c7a40d5fe9c26751d42005a3ce2c8f Compiled from "Test5.java" public class com.ccb.jawa.rest.Test5 minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Methodref #25.#54 // java/lang/Object."<init>":()V #2 = Class #55 // java/util/HashMap #3 = Methodref #2.#54 // java/util/HashMap."<init>":()V #4 = String #56 // 1 #5 = String #57 // value1 #6 = InterfaceMethodref #58.#59 // java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; #7 = String #60 // 2 #8 = String #61 // value2 #9 = String #62 // 3 #10 = String #63 // value3 #11 = Fieldref #64.#65 // java/lang/System.out:Ljava/io/PrintStream; #12 = String #66 // 通过Map.keySet遍历key和value: #13 = Methodref #67.#68 // java/io/PrintStream.println:(Ljava/lang/String;)V #14 = InterfaceMethodref #58.#69 // java/util/Map.keySet:()Ljava/util/Set; #15 = InterfaceMethodref #70.#71 // java/util/Set.iterator:()Ljava/util/Iterator; #16 = InterfaceMethodref #72.#73 // java/util/Iterator.hasNext:()Z #17 = InterfaceMethodref #72.#74 // java/util/Iterator.next:()Ljava/lang/Object; #18 = Class #75 // java/lang/String #19 = InterfaceMethodref #58.#76 // java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object; #20 = String #77 // 通过Map.entrySet遍历key和value #21 = InterfaceMethodref #58.#78 // java/util/Map.entrySet:()Ljava/util/Set; #22 = Class #79 // java/util/Map$Entry #23 = InterfaceMethodref #22.#80 // java/util/Map$Entry.getValue:()Ljava/lang/Object; #24 = Class #81 // com/ccb/jawa/rest/Test5 #25 = Class #82 // java/lang/Object #26 = Utf8 <init> #27 = Utf8 ()V #28 = Utf8 Code #29 = Utf8 LineNumberTable #30 = Utf8 LocalVariableTable #31 = Utf8 this #32 = Utf8 Lcom/ccb/jawa/rest/Test5; #33 = Utf8 main #34 = Utf8 ([Ljava/lang/String;)V #35 = Utf8 key #36 = Utf8 Ljava/lang/String; #37 = Utf8 entry #38 = Utf8 Entry #39 = Utf8 InnerClasses #40 = Utf8 Ljava/util/Map$Entry; #41 = Utf8 args #42 = Utf8 [Ljava/lang/String; #43 = Utf8 map #44 = Utf8 Ljava/util/Map; #45 = Utf8 LocalVariableTypeTable #46 = Utf8 Ljava/util/Map$Entry<Ljava/lang/String;Ljava/lang/String;>; #47 = Utf8 Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>; #48 = Utf8 StackMapTable #49 = Class #83 // java/util/Map #50 = Class #84 // java/util/Iterator #51 = Utf8 MethodParameters #52 = Utf8 SourceFile #53 = Utf8 Test5.java #54 = NameAndType #26:#27 // "<init>":()V #55 = Utf8 java/util/HashMap #56 = Utf8 1 #57 = Utf8 value1 #58 = Class #83 // java/util/Map #59 = NameAndType #85:#86 // put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; #60 = Utf8 2 #61 = Utf8 value2 #62 = Utf8 3 #63 = Utf8 value3 #64 = Class #87 // java/lang/System #65 = NameAndType #88:#89 // out:Ljava/io/PrintStream; #66 = Utf8 通过Map.keySet遍历key和value: #67 = Class #90 // java/io/PrintStream #68 = NameAndType #91:#92 // println:(Ljava/lang/String;)V #69 = NameAndType #93:#94 // keySet:()Ljava/util/Set; #70 = Class #95 // java/util/Set #71 = NameAndType #96:#97 // iterator:()Ljava/util/Iterator; #72 = Class #84 // java/util/Iterator #73 = NameAndType #98:#99 // hasNext:()Z #74 = NameAndType #100:#101 // next:()Ljava/lang/Object; #75 = Utf8 java/lang/String #76 = NameAndType #102:#103 // get:(Ljava/lang/Object;)Ljava/lang/Object; #77 = Utf8 通过Map.entrySet遍历key和value #78 = NameAndType #104:#94 // entrySet:()Ljava/util/Set; #79 = Utf8 java/util/Map$Entry #80 = NameAndType #105:#101 // getValue:()Ljava/lang/Object; #81 = Utf8 com/ccb/jawa/rest/Test5 #82 = Utf8 java/lang/Object #83 = Utf8 java/util/Map #84 = Utf8 java/util/Iterator #85 = Utf8 put #86 = Utf8 (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; #87 = Utf8 java/lang/System #88 = Utf8 out #89 = Utf8 Ljava/io/PrintStream; #90 = Utf8 java/io/PrintStream #91 = Utf8 println #92 = Utf8 (Ljava/lang/String;)V #93 = Utf8 keySet #94 = Utf8 ()Ljava/util/Set; #95 = Utf8 java/util/Set #96 = Utf8 iterator #97 = Utf8 ()Ljava/util/Iterator; #98 = Utf8 hasNext #99 = Utf8 ()Z #100 = Utf8 next #101 = Utf8 ()Ljava/lang/Object; #102 = Utf8 get #103 = Utf8 (Ljava/lang/Object;)Ljava/lang/Object; #104 = Utf8 entrySet #105 = Utf8 getValue { public com.ccb.jawa.rest.Test5(); descriptor: ()V flags: ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 11: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this Lcom/ccb/jawa/rest/Test5; public static void main(java.lang.String[]); descriptor: ([Ljava/lang/String;)V flags: ACC_PUBLIC, ACC_STATIC Code: stack=3, locals=4, args_size=1 0: new #2 // class java/util/HashMap 3: dup 4: invokespecial #3 // Method java/util/HashMap."<init>":()V 7: astore_1 8: aload_1 9: ldc #4 // String 1 11: ldc #5 // String value1 13: invokeinterface #6, 3 // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; 18: pop 19: aload_1 20: ldc #7 // String 2 22: ldc #8 // String value2 24: invokeinterface #6, 3 // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; 29: pop 30: aload_1 31: ldc #9 // String 3 33: ldc #10 // String value3 35: invokeinterface #6, 3 // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; 40: pop 41: getstatic #11 // Field java/lang/System.out:Ljava/io/PrintStream; 44: ldc #12 // String 通过Map.keySet遍历key和value: 46: invokevirtual #13 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 49: aload_1 50: invokeinterface #14, 1 // InterfaceMethod java/util/Map.keySet:()Ljava/util/Set; 55: invokeinterface #15, 1 // InterfaceMethod java/util/Set.iterator:()Ljava/util/Iterator; 60: astore_2 61: aload_2 62: invokeinterface #16, 1 // InterfaceMethod java/util/Iterator.hasNext:()Z 67: ifeq 99 70: aload_2 71: invokeinterface #17, 1 // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object; 76: checkcast #18 // class java/lang/String 79: astore_3 80: getstatic #11 // Field java/lang/System.out:Ljava/io/PrintStream; 83: aload_1 84: aload_3 85: invokeinterface #19, 2 // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object; 90: checkcast #18 // class java/lang/String 93: invokevirtual #13 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 96: goto 61 99: getstatic #11 // Field java/lang/System.out:Ljava/io/PrintStream; 102: ldc #20 // String 通过Map.entrySet遍历key和value 104: invokevirtual #13 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 107: aload_1 108: invokeinterface #21, 1 // InterfaceMethod java/util/Map.entrySet:()Ljava/util/Set; 113: invokeinterface #15, 1 // InterfaceMethod java/util/Set.iterator:()Ljava/util/Iterator; 118: astore_2 119: aload_2 120: invokeinterface #16, 1 // InterfaceMethod java/util/Iterator.hasNext:()Z 125: ifeq 156 128: aload_2 129: invokeinterface #17, 1 // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object; 134: checkcast #22 // class java/util/Map$Entry 137: astore_3 138: getstatic #11 // Field java/lang/System.out:Ljava/io/PrintStream; 141: aload_3 142: invokeinterface #23, 1 // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object; 147: checkcast #18 // class java/lang/String 150: invokevirtual #13 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 153: goto 119 156: return LineNumberTable: line 15: 0 line 16: 8 line 17: 19 line 18: 30 line 21: 41 line 22: 49 line 23: 80 line 24: 96 line 27: 99 line 28: 107 line 29: 138 line 30: 153 line 31: 156 LocalVariableTable: Start Length Slot Name Signature 80 16 3 key Ljava/lang/String; 138 15 3 entry Ljava/util/Map$Entry; 0 157 0 args [Ljava/lang/String; 8 149 1 map Ljava/util/Map; LocalVariableTypeTable: Start Length Slot Name Signature 138 15 3 entry Ljava/util/Map$Entry<Ljava/lang/String;Ljava/lang/String;>; 8 149 1 map Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>; StackMapTable: number_of_entries = 4 frame_type = 253 /* append */ offset_delta = 61 locals = [ class java/util/Map, class java/util/Iterator ] frame_type = 250 /* chop */ offset_delta = 37 frame_type = 252 /* append */ offset_delta = 19 locals = [ class java/util/Iterator ] frame_type = 250 /* chop */ offset_delta = 36 MethodParameters: Name Flags args } SourceFile: "Test5.java" InnerClasses: public static #38= #22 of #58; //Entry=class java/util/Map$Entry of class java/util/Map
虽然使用keyset及entryset来进行遍历能取得相同的结果,但两者的遍历速度是有差别的,keySet()的速度比entrySet()慢了很多,也就是keySet方式遍历Map的性能不如entrySet性能好,为了提高性能,以后多考虑用entrySet()方式来进行遍历。