📜  Java中将HashMap转换为TreeMap的程序

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

Java中将HashMap转换为TreeMap的程序

HashMap 是自Java 1.2 以来 Java 集合的一部分。它提供了Java Map 接口的基本实现,将数据存储在(键,值)对中。要访问 HashMap 中的值,必须知道它的键。 HashMap 之所以称为 HashMap,是因为它使用一种技术 Hashing 来存储数据。

Java中的TreeMap用于实现Map接口和NavigableMap以及抽象类。地图根据其键的自然顺序排序,或者由地图创建时提供的比较器排序,具体取决于使用的构造函数。这被证明是排序和存储键值对的一种有效方式。

以下是在Java中将 HashMap 转换为 TreeMap 的方法,结果 TreeMap 应包含 HashMap 的所有映射,按其键的自然顺序排序。

例子:

以下是在Java中将 HashMap 转换为 TreeMap 的方法:

1. 在Java 8中:此方法包括将 HashMap 转换为 Stream,并使用 Stream.collect() 方法在 TreeMap 中收集流的元素,该方法接受收集器。

算法

  1. 获取要转换的HashMap。
  2. 从 hashMap 中获取条目
  3. 将地图条目转换为流
  4. 使用 Collectors,收集条目并将其转换为 TreeMap
  5. 现在收集 TreeMap
  6. 返回形成的TreeMap

程序:

Java
// Java Program to convert
// HashMap to TreeMap in Java 8
   
import java.util.*;
import java.util.stream.*;
   
class GFG {
   
    // Generic function to construct a new 
    // TreeMap from HashMap
    public static  Map convertToTreeMap(Map hashMap)
    {
        Map
            treeMap = hashMap
                          // Get the entries from the hashMap
                          .entrySet()
   
                          // Convert the map into stream
                          .stream()
   
                          // Now collect the returned TreeMap
                          .collect(
                              Collectors
   
                                  // Using Collectors, collect the entries
                                  // and convert it into TreeMap
                                  .toMap(
                                      Map.Entry::getKey,
                                      Map.Entry::getValue,
                                      (oldValue,
                                       newValue)
                                          -> newValue,
                                      TreeMap::new));
   
        // Return the TreeMap
        return treeMap;
    }
   
    public static void main(String args[])
    {
        // Create a HashMap
        Map hashMap = new HashMap<>();
   
        // Add entries to the HashMap
        hashMap.put("1", "Geeks");
        hashMap.put("2", "forGeeks");
        hashMap.put("3", "A computer Portal");
   
        // Print the HashMap
        System.out.println("HashMap: " + hashMap);
   
        // construct a new TreeMap from HashMap
        Map treeMap = convertToTreeMap(hashMap);
   
        // Print the TreeMap
        System.out.println("TreeMap: " + treeMap);
    }
}


Java
// Java Program to convert
// HashMap to TreeMap in Java 8
   
import java.util.*;
import java.util.stream.*;
   
class GFG {
   
    // Generic function to construct a 
    // new TreeMap from HashMap
    public static  Map convertToTreeMap(Map hashMap)
    {
        // Create a new TreeMap
        Map treeMap = new TreeMap<>();
   
        // Pass the hashMap to putAll() method
        treeMap.putAll(hashMap);
   
        // Return the TreeMap
        return treeMap;
    }
   
    public static void main(String args[])
    {
        // Create a HashMap
        Map hashMap = new HashMap<>();
   
        // Add entries to the HashMap
        hashMap.put("1", "Geeks");
        hashMap.put("2", "forGeeks");
        hashMap.put("3", "A computer Portal");
   
        // Print the HashMap
        System.out.println("HashMap: " + hashMap);
   
        // construct a new TreeMap from HashMap
        Map treeMap = convertToTreeMap(hashMap);
   
        // Print the TreeMap
        System.out.println("TreeMap: " + treeMap);
    }
}


Java
// Java Program to convert
// HashMap to TreeMap in Java 8
   
import com.google.common.collect.*;
import java.util.*;
import java.util.stream.*;
   
class GFG {
   
    // Generic function to construct a
    // new TreeMap from HashMap
    public static  Map 
                       convertToTreeMap(Map hashMap)
    {
        // Create a new TreeMap
        Map treeMap = Maps.newTreeMap();
   
        // Pass the hashMap to putAll() method
        treeMap.putAll(hashMap);
   
        // Return the TreeMap
        return treeMap;
    }
   
    public static void main(String args[])
    {
        // Create a HashMap
        Map hashMap = new HashMap<>();
   
        // Add entries to the HashMap
        hashMap.put("1", "Geeks");
        hashMap.put("2", "forGeeks");
        hashMap.put("3", "A computer Portal");
   
        // Print the HashMap
        System.out.println("HashMap: " + hashMap);
   
        // construct a new TreeMap from HashMap
        Map treeMap = convertToTreeMap(hashMap);
   
        // Print the TreeMap
        System.out.println("TreeMap: " + treeMap);
    }
}


Java
// Java Program to convert
// HashMap to TreeMap in Java 8
   
import java.util.*;
import java.util.stream.*;
   
class GFG {
   
    // Function to construct a new TreeMap from HashMap
    public static Map 
               convertToTreeMap(Map hashMap)
    {
        // Create a new TreeMap
        Map treeMap = new TreeMap<>();
   
        // Convert the HashMap to TreeMap manually
        for (Map.Entry e : hashMap.entrySet()) {
            treeMap.put(Integer.parseInt(e.getKey()), e.getValue());
        }
   
        // Return the TreeMap
        return treeMap;
    }
   
    public static void main(String args[])
    {
        // Create a HashMap
        Map hashMap = new HashMap<>();
   
        // Add entries to the HashMap
        hashMap.put("1", "Geeks");
        hashMap.put("2", "forGeeks");
        hashMap.put("3", "A computer Portal");
   
        // Print the HashMap
        System.out.println("HashMap: " + hashMap);
   
        // construct a new TreeMap
        // from HashMap
        Map treeMap = convertToTreeMap(hashMap);
   
        // Print the TreeMap
        System.out.println("TreeMap: " + treeMap);
    }
}


输出:

HashMap: {1=Geeks, 2=forGeeks, 3=A computer Portal}
TreeMap: {1=Geeks, 2=forGeeks, 3=A computer Portal}

2.使用纯Java :在这个方法中,要么将HashMap实例传递给TreeMap构造函数,要么传递给putAll()方法。这将直接从 HashMap 创建 TreeMap。

算法

  1. 获取要转换的HashMap。
  2. 创建一个新的树图
  3. 将 hashMap 传递给 treeMap 的 putAll() 方法
  4. 返回形成的TreeMap

程序:

Java

// Java Program to convert
// HashMap to TreeMap in Java 8
   
import java.util.*;
import java.util.stream.*;
   
class GFG {
   
    // Generic function to construct a 
    // new TreeMap from HashMap
    public static  Map convertToTreeMap(Map hashMap)
    {
        // Create a new TreeMap
        Map treeMap = new TreeMap<>();
   
        // Pass the hashMap to putAll() method
        treeMap.putAll(hashMap);
   
        // Return the TreeMap
        return treeMap;
    }
   
    public static void main(String args[])
    {
        // Create a HashMap
        Map hashMap = new HashMap<>();
   
        // Add entries to the HashMap
        hashMap.put("1", "Geeks");
        hashMap.put("2", "forGeeks");
        hashMap.put("3", "A computer Portal");
   
        // Print the HashMap
        System.out.println("HashMap: " + hashMap);
   
        // construct a new TreeMap from HashMap
        Map treeMap = convertToTreeMap(hashMap);
   
        // Print the TreeMap
        System.out.println("TreeMap: " + treeMap);
    }
}

输出:

HashMap: {1=Geeks, 2=forGeeks, 3=A computer Portal}
TreeMap: {1=Geeks, 2=forGeeks, 3=A computer Portal}

3. 使用 Google 的 Guava 库:Guava 还提供了一个 TreeMap 实现,可以用来创建一个空的 TreeMap 实例。

算法:

  1. 获取要转换的HashMap。
  2. 使用 Guava 库的 Maps.newTreeMap() 创建一个新的 TreeMap
  3. 将 hashMap 传递给 treeMap 的 putAll() 方法
  4. 返回形成的TreeMap

程序:

Java

// Java Program to convert
// HashMap to TreeMap in Java 8
   
import com.google.common.collect.*;
import java.util.*;
import java.util.stream.*;
   
class GFG {
   
    // Generic function to construct a
    // new TreeMap from HashMap
    public static  Map 
                       convertToTreeMap(Map hashMap)
    {
        // Create a new TreeMap
        Map treeMap = Maps.newTreeMap();
   
        // Pass the hashMap to putAll() method
        treeMap.putAll(hashMap);
   
        // Return the TreeMap
        return treeMap;
    }
   
    public static void main(String args[])
    {
        // Create a HashMap
        Map hashMap = new HashMap<>();
   
        // Add entries to the HashMap
        hashMap.put("1", "Geeks");
        hashMap.put("2", "forGeeks");
        hashMap.put("3", "A computer Portal");
   
        // Print the HashMap
        System.out.println("HashMap: " + hashMap);
   
        // construct a new TreeMap from HashMap
        Map treeMap = convertToTreeMap(hashMap);
   
        // Print the TreeMap
        System.out.println("TreeMap: " + treeMap);
    }
}

输出:

HashMap: {1=Geeks, 2=forGeeks, 3=A computer Portal}
TreeMap: {1=Geeks, 2=forGeeks, 3=A computer Portal}

4.不兼容类型之间的转换:如果需要的TreeMap与HashMap的类型不同,则可以使用此方法。在这种情况下,转换需要手动完成。

算法

  1. 获取要转换的HashMap。
  2. 创建一个新的树图
  3. 对于 hashMap 的每个条目:
    • 通过强制转换将 Key 和 Value 转换为所需的类型
    • 通过treeMap的put()方法插入转换后的pair
  4. 返回形成的TreeMap

程序:

Java

// Java Program to convert
// HashMap to TreeMap in Java 8
   
import java.util.*;
import java.util.stream.*;
   
class GFG {
   
    // Function to construct a new TreeMap from HashMap
    public static Map 
               convertToTreeMap(Map hashMap)
    {
        // Create a new TreeMap
        Map treeMap = new TreeMap<>();
   
        // Convert the HashMap to TreeMap manually
        for (Map.Entry e : hashMap.entrySet()) {
            treeMap.put(Integer.parseInt(e.getKey()), e.getValue());
        }
   
        // Return the TreeMap
        return treeMap;
    }
   
    public static void main(String args[])
    {
        // Create a HashMap
        Map hashMap = new HashMap<>();
   
        // Add entries to the HashMap
        hashMap.put("1", "Geeks");
        hashMap.put("2", "forGeeks");
        hashMap.put("3", "A computer Portal");
   
        // Print the HashMap
        System.out.println("HashMap: " + hashMap);
   
        // construct a new TreeMap
        // from HashMap
        Map treeMap = convertToTreeMap(hashMap);
   
        // Print the TreeMap
        System.out.println("TreeMap: " + treeMap);
    }
}

输出:

HashMap: {1=Geeks, 2=forGeeks, 3=A computer Portal}
TreeMap: {1=Geeks, 2=forGeeks, 3=A computer Portal}