Java 从其他Map创建HashMap

Java 从其他Map创建HashMap

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

在java中主要有三种类型的Map

1.HashMap
2.链接哈希图
3.树状图

这些接口扩展了Map接口。

有各种方法可以将一张Map转换为另一张。

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

方法1:使用迭代器/a loop

遍历Map(本例中为LinkedHashMap)的每个元素,并在新的HashMap中添加其中的每个元素。

// 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<String, String> lhm
               = new LinkedHashMap<String, String>();
 
        // 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<String, String> gfg = new HashMap<String, String>();
 
        // Using entrySet() method create a set out of the same elements
        // contained in the hash map
        for (Map.Entry<String, String> entry : lhm.entrySet())
            gfg.put(entry.getKey(), entry.getValue());
 
        System.out.println(gfg);
    }
}

输出

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

方法2:使用构造函数

将给定的Map(本例中为TreeMap)传递给HashMap构造函数,它将自动处理将给定的Map转换为HashMap。

// Java program for creating HashMap from Other Maps
// using constructor
 
import java.util.*;
 
public class to_hashmap {
 
    public static void main(String a[])
    {
        // create an instance of TreeMap
        Map<String, String> tm = new TreeMap<String, String>();
 
        // 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<String, String> gfg = new HashMap<String, String>(tm);
 
        System.out.println(gfg);
    }
}

输出

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

方法3:使用 方法

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

// 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<String, String> tm = new TreeMap<String, String>();
 
        // 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<String, String> gfg = new HashMap<String, String>();
 
        // using put all command
        gfg.putAll(tm);
 
        System.out.println(gfg);
    }
}

输出

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程