从 TreeMap 获取排序子图的Java程序
Java中的 TreeMap 是以映射方式存储元素的容器,即键值和映射值对。每个元素都有一个键值和一个相应的映射值。所有的键值都是唯一的,并且两个映射的值不能有相同的键值是必要的。它基于红黑树实现,与 HashMap 不同,在 TreeMap 中插入值的顺序无关紧要,它们仍然根据它们的键进行排序。
TreeMap 的语法:
TreeMap tree_map= new TreeMap();
SubMap 的语法:
情况 1:方法将包含开始键但不包含结束键
tree_map.submap(starting_key, ending_key);
情况 2:要包含两个键,请使用键传递 true
tree_map.submap(starting_key, true, ending_key, true);
情况 3:要排除任何键,请使用键传递 false
tree_map.submap(starting_key, false, ending_key, true);
方法:
- 蛮力法
- 使用预定义函数
- 使用定义的比较器
- 使用用户定义的比较器
方法一:打印默认排序子图
Java
// Java Program to Get Sorted Sub-Map from TreeMap
// Importing all classes of
// java.util package
import java.util.*;
public class GFG {
// Main driver code
public static void main(String[] args)
{
// Create a TreeMap
TreeMap my_map
= new TreeMap();
// Adding Element to TreeMap
my_map.put(5, "DoubleAlpha");
my_map.put(1, "Alpha");
my_map.put(3, "Beta");
my_map.put(2, "Gamma");
my_map.put(4, "Theta");
// Printing default sorted elements in given range
// using subMap function
System.out.println(
"Elements: "
+ my_map.subMap(1, true, 5, true));
}
}
Java
// Java Program to Get Sorted Sub-Map from TreeMap
// using predefined functions
// Importing all classes of
// java.util package
import java.util.*;
public class GFG {
// Main driver code
public static void main(String[] args)
{
// Create a TreeMap
TreeMap my_map
= new TreeMap();
// Adding elements to TreeMap
my_map.put(5, "DoubleAlpha");
my_map.put(1, "Alpha");
my_map.put(3, "Beta");
my_map.put(2, "Gamma");
my_map.put(4, "Theta");
// Reversing elements of map in
// descending order
Map reversed_map
= my_map.descendingMap();
// Printing default reverse sorted elements
// using subMap() function
System.out.println("Elements: " + reversed_map);
}
}
Java
// Java Program to Get Sorted Sub-Map from TreeMap
// using user-defined comparator
// Importing all classes of
// java.util package
import java.util.*;
// Class
public class GFG {
// Main driver Code
public static void main(String a[])
{
// Sorting TreeMap by key
TreeMap my_map
= new TreeMap(new UserComp());
// Add key-value pair to TreeMap
my_map.put("DoubleAlpha", "RedDotGeek");
my_map.put("Alpha", "GeekAmongGeek");
my_map.put("Beta", "MasterGeek");
my_map.put("Gamma", "Geek");
my_map.put("Theta", "OnwayGeek");
// Display above added elements of TreeMap
System.out.println(
"Elements: "
+ my_map.subMap("A", true, "Z", true));
}
}
// Pre-defined comparator
class UserComp implements Comparator {
public int compare(String a, String b)
{
return a.compareTo(b);
}
}
Java
// Java Program to Get Sorted Sub-Map from TreeMap
// using pre-defined comparator
// Importing all classes of
// java.util package
import java.util.*;
public class GFG {
// Main driver method
public static void main(String a[])
{
// Sorting treeMap by key
TreeMap my_map
= new TreeMap(new UserComp());
// Adding key-value pair to TreeMap
my_map.put(1, "DoubleAlpha");
my_map.put(8, "Alpha");
my_map.put(5, "Beta");
my_map.put(3, "Gamma");
my_map.put(4, "Theta");
// Display elements added of TreeMap
System.out.println("Elements : " + my_map);
}
}
// Defined comparator
class UserComp implements Comparator {
public int compare(Integer a, Integer b)
{
if (b > a)
return 1;
return -1;
}
}
输出
Elements: {1=Alpha, 2=Gamma, 3=Beta, 4=Theta, 5=DoubleAlpha}
方法 2:使用预定义函数反向排序 Map
例子:
Java
// Java Program to Get Sorted Sub-Map from TreeMap
// using predefined functions
// Importing all classes of
// java.util package
import java.util.*;
public class GFG {
// Main driver code
public static void main(String[] args)
{
// Create a TreeMap
TreeMap my_map
= new TreeMap();
// Adding elements to TreeMap
my_map.put(5, "DoubleAlpha");
my_map.put(1, "Alpha");
my_map.put(3, "Beta");
my_map.put(2, "Gamma");
my_map.put(4, "Theta");
// Reversing elements of map in
// descending order
Map reversed_map
= my_map.descendingMap();
// Printing default reverse sorted elements
// using subMap() function
System.out.println("Elements: " + reversed_map);
}
}
输出
Elements: {5=DoubleAlpha, 4=Theta, 3=Beta, 2=Gamma, 1=Alpha}
方法 3:使用用户定义的比较器打印排序的子地图
用户定义比较器背后的想法是,如果用户想要根据用户的偏好对地图进行排序。比较器的函数是用来实现我们的排序逻辑的。
例子:
Java
// Java Program to Get Sorted Sub-Map from TreeMap
// using user-defined comparator
// Importing all classes of
// java.util package
import java.util.*;
// Class
public class GFG {
// Main driver Code
public static void main(String a[])
{
// Sorting TreeMap by key
TreeMap my_map
= new TreeMap(new UserComp());
// Add key-value pair to TreeMap
my_map.put("DoubleAlpha", "RedDotGeek");
my_map.put("Alpha", "GeekAmongGeek");
my_map.put("Beta", "MasterGeek");
my_map.put("Gamma", "Geek");
my_map.put("Theta", "OnwayGeek");
// Display above added elements of TreeMap
System.out.println(
"Elements: "
+ my_map.subMap("A", true, "Z", true));
}
}
// Pre-defined comparator
class UserComp implements Comparator {
public int compare(String a, String b)
{
return a.compareTo(b);
}
}
输出
Elements: {Alpha=GeekAmongGeek, Beta=MasterGeek, DoubleAlpha=RedDotGeek, Gamma=Geek, Theta=OnwayGeek}
方法 4:使用用户定义的比较器反向排序映射
Java
// Java Program to Get Sorted Sub-Map from TreeMap
// using pre-defined comparator
// Importing all classes of
// java.util package
import java.util.*;
public class GFG {
// Main driver method
public static void main(String a[])
{
// Sorting treeMap by key
TreeMap my_map
= new TreeMap(new UserComp());
// Adding key-value pair to TreeMap
my_map.put(1, "DoubleAlpha");
my_map.put(8, "Alpha");
my_map.put(5, "Beta");
my_map.put(3, "Gamma");
my_map.put(4, "Theta");
// Display elements added of TreeMap
System.out.println("Elements : " + my_map);
}
}
// Defined comparator
class UserComp implements Comparator {
public int compare(Integer a, Integer b)
{
if (b > a)
return 1;
return -1;
}
}
输出
Elements : {8=Alpha, 5=Beta, 4=Theta, 3=Gamma, 1=DoubleAlpha}