Java HashMap转TreeMap用法及代码示例

您所在的位置:网站首页 hashmap转bean Java HashMap转TreeMap用法及代码示例

Java HashMap转TreeMap用法及代码示例

2024-06-12 10:51| 来源: 网络整理| 查看: 265

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

Java中的TreeMap用于实现Map接口,NavigableMap与抽象类一起实现。映射根据其键的自然顺序进行排序,或者通过映射创建时提供的比较器进行排序,具体取决于使用的构造函数。事实证明,这是一种排序和存储键值对的有效方法。

下面是在 Java 中将 HashMap 转换为 TreeMap 的方法,这样生成的 TreeMap 应包含 HashMap 的所有映射,并按键的自然顺序排序。

例子:

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

Input: HashMap: {1=1, 2=2, 3=3} Output: TreeMap: {1=1, 2=2, 3=3}  

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

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

算法:

获取要转换的HashMap。 从 hashMap 中获取条目 将映射条目转换为流 使用Collector收集条目并将其转换为TreeMap 现在收集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)     {         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);     } }

输出:

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。

算法:

获取要转换的HashMap。 创建一个新的TreeMap 将 hashMap 传递给 treeMap 的 putAll() 方法 返回形成的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实例。

算法:

获取要转换的HashMap。 使用Guava库的Maps.newTreeMap()创建一个新的TreeMap 将 hashMap 传递给 treeMap 的 putAll() 方法 返回形成的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的类型不同,可以使用此方法。在这种情况下,需要手动完成转换。

算法:

获取要转换的HashMap。 创建一个新的TreeMap 对于 hashMap 的每个条目: 通过强制转换将 Key 和 Value 转换为所需的类型 通过treeMap的put()方法插入转换后的pair 返回形成的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}



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3