遍历Java中的HashMap

哈希图 以(键、值)对存储数据,您可以通过另一种类型的索引访问它们。HashMap类实现了Map接口,允许我们存储密钥。hashMap是java 1.2之后开发的java集合框架的一部分。它内部使用 散列技术 这很快。

null

图片[1]-遍历Java中的HashMap-yiteyi-C++库

语法:

public class HashMap<K,V>extends AbstractMap<K,V>implements Map<K,V>, Clonnable, Serial     

不同的遍历方式

我们可以在下面列出的键和值对的映射上进行迭代,后面的描述如下:

方法:

  1. 使用迭代器
  2. 使用增强的for循环(针对每个循环)
  3. 使用forEach()方法

方法1: 使用迭代器

迭代器是 JAVAutil包 用于遍历集合。因此,讨论迭代器并没有什么特别之处,所以我们将提出一些方法 迭代器接口 用于遍历HashMap。

  • hm.entrySet() 用于检索所有名为Map的键值对。条目并在内部存储到一个集合中。
  • 嗯,entrySet()。迭代器() 返回一个迭代器,该迭代器充当光标,指向集合的第一个元素,并一直移动到最后。
  • 催眠器。hasNext() 检查集合中的下一个元素并返回布尔值
  • 催眠器。下一个() 返回集合中的下一个元素(Map.Entry)。
  • mapElement。getKey() 返回关联映射的键。进入
  • mapElement。getValue() 返回关联映射的值。进入

例子:

JAVA

// Java Program to Traverse through HashMap
// Using Iterator
// Importing required classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an Hashmap of string-integer pairs
// It contains student name and their marks
HashMap<String, Integer> hm
= new HashMap<String, Integer>();
// Adding mappings to above HashMap
// using put() method
hm.put( "GeeksforGeeks" , 54 );
hm.put( "A computer portal" , 80 );
hm.put( "For geeks" , 82 );
// Printing all elements of HashMap
System.out.println( "Created hashmap is" + hm);
// Getting an iterator
Iterator hmIterator = hm.entrySet().iterator();
// Display message only
System.out.println(
"HashMap after adding bonus marks:" );
// Iterating through Hashmap and
// adding some bonus marks for every student
while (hmIterator.hasNext()) {
Map.Entry mapElement
= (Map.Entry)hmIterator.next();
int marks = (( int )mapElement.getValue() + 10 );
// Printing mark corresponding to string entries
System.out.println(mapElement.getKey() + " : "
+ marks);
}
}
}


输出

Created hashmap is{GeeksforGeeks=54, A computer portal=80, For geeks=82}HashMap after adding bonus marks:GeeksforGeeks : 64A computer portal : 90For geeks : 92

方法2: 用于每个循环

例子:

JAVA

// Java program for Traversing through HashMap
// Using for-each Loop
// Importing required classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// creating an empty HashMap of string and integer
// pairs Mappings denotes Student name and marks
HashMap<String, Integer> hm
= new HashMap<String, Integer>();
// Adding mappings to HashMap
// using put() method
hm.put( "GeeksforGeeks" , 54 );
hm.put( "A computer portal" , 80 );
hm.put( "For geeks" , 82 );
// Printing all elements of above Map
System.out.println( "Created hashmap is" + hm);
// Display message only
System.out.println(
"HashMap after adding bonus marks:" );
// Looping through the HashMap
// Using for-each loop
for (Map.Entry mapElement : hm.entrySet()) {
String key = (String)mapElement.getKey();
// Adding some bonus marks to all the students
int value = (( int )mapElement.getValue() + 10 );
// Printing above marks corresponding to
// students names
System.out.println(key + " : " + value);
}
}
}


输出:

Created hashmap is{GeeksforGeeks=54, A computer portal=80, For geeks=82}HashMap after adding bonus marks:GeeksforGeeks : 64A computer portal : 90For geeks : 92

方法3: 使用forEach()方法

forEach()是java 8中引入的HashMap方法。它用于迭代hashmap,还可以减少代码行数,如下所示:

例子:

JAVA

// Java program for traversing Through HashMap
// Using forEach() Method
// Importing required classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an  empty HashMap of string-integer
// pairs
HashMap<String, Integer> hm
= new HashMap<String, Integer>();
// Adding mappings to HashMap
// using put() method
hm.put( "GeeksforGeeks" , 54 );
hm.put( "A computer portal" , 80 );
hm.put( "For geeks" , 82 );
// Printing all elements of above HashMap
System.out.println( "Created hashmap is" + hm);
// Display message only
System.out.println(
"HashMap after adding bonus marks:" );
// Looping through HashMap and adding bonus marks
// using HashMap.forEach()
hm.forEach((k, v)
-> System.out.println(k + " : "
+ (v + 10 )));
}
}


输出:

Created hashmap is{GeeksforGeeks=54, A computer portal=80, For geeks=82}HashMap after adding bonus marks:GeeksforGeeks : 64A computer portal : 90For geeks : 92

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享