标签:try png 循环 tle size tar isp net com
原文地址:https://www.javaguides.net/2020/03/5-best-ways-to-iterate-over-hashmap-in-java.html
翻译:高行行
在本文中,我们将通过示例讨论在 Java 上遍历 ?HashMap? 的五种最佳方式。
通过 ?https://www.javaguides.net/2018/07/java-8-lambda-expressions.html了解有关 lambda 表达式的信息
package com.java.tutorials.iterations;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/**
* 在 Java 中遍历 HashMap 的5种最佳方式
* @author Ramesh Fadatare
*
*/
public class IterateHashMapExample {
public static void main(String[] args) {
// 1. 使用 Iterator 遍历 HashMap EntrySet
Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
coursesMap.put(1, "C");
coursesMap.put(2, "C++");
coursesMap.put(3, "Java");
coursesMap.put(4, "Spring Framework");
coursesMap.put(5, "Hibernate ORM framework");
Iterator < Entry < Integer, String >> iterator = coursesMap.entrySet().iterator();
while (iterator.hasNext()) {
Entry < Integer, String > entry = iterator.next();
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}
Output:
1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
package com.java.tutorials.iterations;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* 在 Java 中遍历 HashMap 的5种最佳方式
* @author Ramesh Fadatare
*
*/
public class IterateHashMapExample {
public static void main(String[] args) {
Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
coursesMap.put(1, "C");
coursesMap.put(2, "C++");
coursesMap.put(3, "Java");
coursesMap.put(4, "Spring Framework");
coursesMap.put(5, "Hibernate ORM framework");
// 2. 使用 Iterator 遍历 HashMap KeySet
Iterator < Integer > iterator = coursesMap.keySet().iterator();
while (iterator.hasNext()) {
Integer key = iterator.next();
System.out.println(key);
System.out.println(coursesMap.get(key));
}
}
}
Output:
1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
package com.java.tutorials.iterations;
import java.util.HashMap;
import java.util.Map;
/**
* 在 Java 中遍历 HashMap 的5种最佳方式
* @author Ramesh Fadatare
*
*/
public class IterateHashMapExample {
public static void main(String[] args) {
Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
coursesMap.put(1, "C");
coursesMap.put(2, "C++");
coursesMap.put(3, "Java");
coursesMap.put(4, "Spring Framework");
coursesMap.put(5, "Hibernate ORM framework");
// 3. 使用 For-each 循环遍历 HashMap
for (Map.Entry < Integer, String > entry: coursesMap.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}
Output:
1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
package com.java.tutorials.iterations;
import java.util.HashMap;
import java.util.Map;
/**
* 在 Java 中遍历 HashMap 的5种最佳方式
* @author Ramesh Fadatare
*
*/
public class IterateHashMapExample {
public static void main(String[] args) {
Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
coursesMap.put(1, "C");
coursesMap.put(2, "C++");
coursesMap.put(3, "Java");
coursesMap.put(4, "Spring Framework");
coursesMap.put(5, "Hibernate ORM framework");
// 4. 使用 Lambda 表达式遍历 HashMap
coursesMap.forEach((key, value) -> {
System.out.println(key);
System.out.println(value);
});
}
}
Output:
1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
package com.java.tutorials.iterations;
import java.util.HashMap;
import java.util.Map;
/**
* 在 Java 中遍历 HashMap 的5种最佳方式
* @author Ramesh Fadatare
*
*/
public class IterateHashMapExample {
public static void main(String[] args) {
Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
coursesMap.put(1, "C");
coursesMap.put(2, "C++");
coursesMap.put(3, "Java");
coursesMap.put(4, "Spring Framework");
coursesMap.put(5, "Hibernate ORM framework");
// 5. 使用 Stream API 遍历 HashMap
coursesMap.entrySet().stream().forEach((entry) - > {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
});
}
}
Output:
1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
标签:try png 循环 tle size tar isp net com
原文地址:https://www.cnblogs.com/gaohanghang/p/12491979.html