📜  在Java中从其他映射创建 HashMap

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

在Java中从其他映射创建 HashMap

Java.util 包中的Map 接口表示键和值之间的映射。 Map 接口不是 Collection 接口的子类型。因此,它的行为与其他集合类型略有不同。地图包含唯一键。

Java中有3种主要的地图类型

  1. 哈希表
  2. 链接哈希映射
  3. 树形图

这些接口扩展了 Map 接口。

有多种方法可以将一张地图转换为另一张地图:

  1. 使用迭代器/循环
  2. 使用构造函数
  3. 使用 putAll() 方法

方法一:使用迭代器/循环

迭代映射的每个元素(在本例中为 LinkedHashMap)并将它们中的每一个添加到新的 HashMap 中。

Java
// Java program for creating HashMap from Other Maps
  
import java.util.*;
  
public class to_hashmap {
  
    public static void main(String a[])
    {
        // create an instance of LinkedHashMap
        LinkedHashMap lhm 
               = new LinkedHashMap();
  
        // Add mappings using put method
        lhm.put("Apurva", "Bhatt");
        lhm.put("James", "Bond");
        lhm.put("Scarlett ", "Johansson");
  
        // It prints the elements in same order
        // as they were inserted
        System.out.println(lhm);
  
        Map gfg = new HashMap();
  
        // Using entrySet() method create a set out of the same elements 
        // contained in the hash map
        for (Map.Entry entry : lhm.entrySet())
            gfg.put(entry.getKey(), entry.getValue());
  
        System.out.println(gfg);
    }
}


Java
// Java program for creating HashMap from Other Maps
// using constructer
  
import java.util.*;
  
public class to_hashmap {
  
    public static void main(String a[])
    {
        // create an instance of TreeMap
        Map tm = new TreeMap();
  
        // Add mappings using put method
        tm.put("Apurva", "Bhatt");
        tm.put("James", "Bond");
        tm.put("Scarlett ", "Johansson");
  
        // It prints the elements in same order
        // as they were inserted
        System.out.println(tm);
  
        Map gfg = new HashMap(tm);
  
        System.out.println(gfg);
    }
}


Java
// Java program for creating HashMap from Other Maps
// using putAll() method
  
import java.util.*;
  
public class two_hashmap {
  
    public static void main(String a[])
    {
        // create an instance of TreeMap
        Map tm = new TreeMap();
  
        // Add mappings using put method
        tm.put("Apurva", "Bhatt");
        tm.put("James", "Bond");
        tm.put("Scarlett ", "Johansson");
  
        // It prints the elements in same order
        // as they were inserted
        System.out.println(tm);
  
        Map gfg = new HashMap();
  
        // using put all command
        gfg.putAll(tm);
  
        System.out.println(gfg);
    }
}


输出
{Apurva=Bhatt, James=Bond, Scarlett =Johansson}
{James=Bond, Apurva=Bhatt, Scarlett =Johansson}

方法二:使用构造函数

将给定的 Map(在本例中为 TreeMap)传递给 HashMap 构造函数——它将自动负责将给定的 Map 转换为 HashMap。

Java

// Java program for creating HashMap from Other Maps
// using constructer
  
import java.util.*;
  
public class to_hashmap {
  
    public static void main(String a[])
    {
        // create an instance of TreeMap
        Map tm = new TreeMap();
  
        // Add mappings using put method
        tm.put("Apurva", "Bhatt");
        tm.put("James", "Bond");
        tm.put("Scarlett ", "Johansson");
  
        // It prints the elements in same order
        // as they were inserted
        System.out.println(tm);
  
        Map gfg = new HashMap(tm);
  
        System.out.println(gfg);
    }
}
输出
{Apurva=Bhatt, James=Bond, Scarlett =Johansson}
{Apurva=Bhatt, James=Bond, Scarlett =Johansson}

方法 3:使用putAll()方法

它与前面的方法类似,不是将给定的 Map 传递给 HashMap 构造函数,而是将其传递给 putAll() 方法,它会自动将其转换为 HashMap。

Java

// Java program for creating HashMap from Other Maps
// using putAll() method
  
import java.util.*;
  
public class two_hashmap {
  
    public static void main(String a[])
    {
        // create an instance of TreeMap
        Map tm = new TreeMap();
  
        // Add mappings using put method
        tm.put("Apurva", "Bhatt");
        tm.put("James", "Bond");
        tm.put("Scarlett ", "Johansson");
  
        // It prints the elements in same order
        // as they were inserted
        System.out.println(tm);
  
        Map gfg = new HashMap();
  
        // using put all command
        gfg.putAll(tm);
  
        System.out.println(gfg);
    }
}
输出
{Apurva=Bhatt, James=Bond, Scarlett =Johansson}
{Apurva=Bhatt, James=Bond, Scarlett =Johansson}