📜  如何在Java中迭代 LinkedHashMap?

📅  最后修改于: 2022-05-13 01:54:22.541000             🧑  作者: Mango

如何在Java中迭代 LinkedHashMap?

LinkedHashMap 类扩展了 HashMap 并按照插入顺序维护映射中条目的链接列表。这允许在地图上进行插入顺序迭代。也就是说,在迭代 LinkedHashMap 时,元素将按照它们被插入的顺序返回。

基本上有两种方法可以迭代 LinkedHashMap:

  1. 使用 keySet() 和 get() 方法
  2. 使用 entrySet() 和 Iterator

方法一:使用keySet()和get()方法迭代LinkedHashMap

句法:

linked_hash_map.keySet()

参数:该方法不带任何参数。

返回值:该方法返回一个具有 LinkedHashMap 键的集合。

  • 通过 keySet() 方法,我们将获得一个包含地图键的集合。
  • 然后在这个集合上运行一个循环之后,我们可以使用 get() 方法获取每个键及其值。
Java
// Java Program to iterate through LinkedHashMap using 
// keySet() and get() Method
  
import java.util.LinkedHashMap;
import java.util.Set;
  
public class GFG {
  
    public static void main(String a[])
    {
        // making the object of LinkedHashMap
        LinkedHashMap linkedHashMap
                 = new LinkedHashMap();
        
        // adding the elements in linkedHashMap
        linkedHashMap.put("One", "First element");
        linkedHashMap.put("Two", "Second element");
        linkedHashMap.put("Three", "Third element");
  
        Set keys = linkedHashMap.keySet();
        
        // printing the elements of LinkedHashMap
        for (String key : keys) {
            System.out.println(key + " -- "
                               + linkedHashMap.get(key));
        }
        
    }
}


Java
// Java Program to iterate over linkedHashMap using
// entrySet() and iterator
  
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create a LinkedHashMap and populate it with
        // elements
        LinkedHashMap linkedHashMap
            = new LinkedHashMap();
  
        // adding the elements to the linkedHashMap
        linkedHashMap.put("One", "First element");
        linkedHashMap.put("Two", "Second element");
        linkedHashMap.put("Three", "Third element");
  
        // Get a set of all the entries (key - value pairs)
        // contained in the LinkesHashMap
        Set entrySet = linkedHashMap.entrySet();
  
        // Obtain an Iterator for the entries Set
        Iterator it = entrySet.iterator();
  
        // Iterate through LinkedHashMap entries
        System.out.println("LinkedHashMap entries : ");
  
        while (it.hasNext())
            // iterating over each element using it.next()
            // method
            System.out.println(it.next());
    }
}


输出
One -- First element
Two -- Second element
Three -- Third element

方法二:使用 entrySet() 和 Iterator 迭代 LinkedHashMap

句法:

Linkedhash_map.entrySet()

参数:该方法不带任何参数。

返回值:该方法返回一个与 LinkedHashMap 具有相同元素的集合。

Java

// Java Program to iterate over linkedHashMap using
// entrySet() and iterator
  
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create a LinkedHashMap and populate it with
        // elements
        LinkedHashMap linkedHashMap
            = new LinkedHashMap();
  
        // adding the elements to the linkedHashMap
        linkedHashMap.put("One", "First element");
        linkedHashMap.put("Two", "Second element");
        linkedHashMap.put("Three", "Third element");
  
        // Get a set of all the entries (key - value pairs)
        // contained in the LinkesHashMap
        Set entrySet = linkedHashMap.entrySet();
  
        // Obtain an Iterator for the entries Set
        Iterator it = entrySet.iterator();
  
        // Iterate through LinkedHashMap entries
        System.out.println("LinkedHashMap entries : ");
  
        while (it.hasNext())
            // iterating over each element using it.next()
            // method
            System.out.println(it.next());
    }
}
输出
LinkedHashMap entries : 
One=First element
Two=Second element
Three=Third element