Java中将HashMap转换为TreeMap的程序

哈希图 是自Java1.2以来Java系列的一部分。它提供了Java Map接口的基本实现,该接口以(键、值)对存储数据。要访问HashMap中的值,必须知道它的键。HashMap被称为HashMap,因为它使用哈希技术来存储数据。

null

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

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

例如:

输入: HashMap:{1=极客,2=伪造者,3=计算机门户} 输出: 树形图:{1=极客,2=伪造者,3=计算机门户}

输入: HashMap:{1=1,2=2,3=3} 输出: 树映射:{1=1,2=2,3=3}

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

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

算法 :

  1. 获取要转换的哈希映射。
  2. 从hashMap中获取条目
  3. 将地图条目转换为流
  4. 使用收集器,收集条目并将其转换为树形图
  5. 现在收集树形图
  6. 归还已成型的 树图

节目:

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 <K, V> Map<K, V> convertToTreeMap(Map<K, V> hashMap)
{
Map<K, V>
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<String, String> 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<String, String> 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创建树映射。

算法 :

  1. 获取要转换的哈希映射。
  2. 创建一个新的树映射
  3. 将hashMap传递给treeMap的putAll()方法
  4. 归还已成型的 树图

节目:

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 <K, V> Map<K, V> convertToTreeMap(Map<K, V> hashMap)
{
// Create a new TreeMap
Map<K, V> 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<String, String> 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<String, String> 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.使用谷歌的番石榴图书馆 :Guava还提供了一个TreeMap实现,可用于创建一个空的TreeMap实例。

算法:

  1. 获取要转换的哈希映射。
  2. 使用地图创建一个新的树状图。番石榴图书馆的newTreeMap()
  3. 将hashMap传递给treeMap的putAll()方法
  4. 归还已成型的 树图

节目:

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 <K extends Comparable, V> Map<K, V>
convertToTreeMap(Map<K, V> hashMap)
{
// Create a new TreeMap
Map<K, V> 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<String, String> 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<String, String> 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.不兼容类型之间的转换 :如果所需树映射的类型与HashMap不同,则可以使用此方法。在这种情况下,转换需要手动完成。

算法 :

  1. 获取要转换的哈希映射。
  2. 创建一个新的树映射
  3. 对于hashMap的每个条目:
    • 通过强制转换将键和值转换为所需的类型
    • 通过treeMap的put()方法插入转换后的对
  4. 归还已成型的 树图

节目:

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<Integer, String>
convertToTreeMap(Map<String, String> hashMap)
{
// Create a new TreeMap
Map<Integer, String> treeMap = new TreeMap<>();
// Convert the HashMap to TreeMap manually
for (Map.Entry<String, String> 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<String, String> 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<Integer, String>
// from HashMap<String, String>
Map<Integer, String> 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}

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享