获取 TreeMap 键、值或大于或小于指定值的条目的Java程序
TreeMap 类是一个红黑树实现。它帮助我们按排序顺序存储键值对。这里讨论了 3 种方法,其中所有方法中 TreeMap 中的 4 个键值对以及语法如下。
tree.put100, "=> Welcoming");
tree.put(120, "=> you to ");
tree.put(140, "=> computer science portal");
tree.put(200, "=> Geeks for Geeks");
方法:
- 蛮力方法仅用于存储 TreeMap 键值对并仅显示。
- 使用内置函数: 更高键() TreeMap 类的方法和lowerKey()方法。
- 使用 TreeMap 类的highEntry().getValue()函数和lowerEntry().getValue()函数。
方法一:在TreeMap中存储键值对并打印键值对。
例子:
Java
// Java Program to Get TreeMap Key and Value
// Importing all classes
// of java.util package
import java.util.*;
class GFG {
// Main driver method
public static void main(String[] args)
{
// Initialization of a TreeMap
// using Generics
TreeMap tree
= new TreeMap();
// Inserting the Elements into TreeMap
tree.put(100, "=> Welcoming");
tree.put(120, "=> you to ");
tree.put(140, "=> computer science portal");
tree.put(200, "=> Geeks for Geeks");
// Iterating over TreeMap using for-each loop
for (Map.Entry map :
tree.entrySet())
// Displaying all entries- keys and values
// using getKey() and getValue() method
System.out.println(map.getKey() + " "
+ map.getValue());
}
}
Java
// Java Program to Get TreeMap Key, Value, then
// Entry Greater or Less than the Specified Value
// using lowerKey() and higherKey() of Tree class
// Importing all classes of
// java.util package
import java.util.*;
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a TreeMap
TreeMap tree
= new TreeMap();
// Inserting the Elements into TreeMap
tree.put(100, "=> Welcoming");
tree.put(120, "=> you to ");
tree.put(140, "=> computer science portal");
tree.put(200, "=> Geeks for Geeks");
// Returning the smallest key among all the keys
// greater than 150
System.out.println("Smallest key among key > 150 : "
+ tree.higherKey(150));
// Returning the greatest key among all the keys
// smaller than 150
System.out.println("Greatest key among key < 150 : "
+ tree.lowerKey(150));
}
}
Java
// Java Program to Get TreeMap Key, Value, or
// Entry Greater or Less than the Specified Value
// using higherEntry().getValue() function and
// lowerEntry().getValue()
// Importing all classes of
// java.util package
import java.util.*;
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a TreeMap
TreeMap tree
= new TreeMap();
// Inserting the Elements into TreeMap
tree.put(100, "=> Welcoming");
tree.put(120, "=> you to ");
tree.put(140, "=> computer science portal");
tree.put(200, "=> Geeks for Geeks");
// Returning the value corresponding to the key
// which is smallest among the keys greater than 150
System.out.println(
tree.higherEntry(150).getValue());
// Returning the value corresponding to the key
// which is greatest among the keys smaller than 150
System.out.println(tree.lowerEntry(150).getValue());
}
}
100 => Welcoming
120 => you to
140 => computer science portal
200 => Geeks for Geeks
方法二:使用内置函数打印大于或小于指定值的键: 更高键() TreeMap 类的方法和lowerKey()方法
Java.util.TreeMap类的 highKey ()方法用于返回严格大于给定键的最小键,如果没有这样的键,则返回 null。
句法:
public K higherKey(K key)
参数:此方法将键 k 作为参数。
返回值:此方法返回大于该键的最小键,如果没有这样的键,则返回 null。
异常:如果指定的键为空并且此映射使用自然排序,或者其比较器不允许空键,则此方法将引发 NullPointerException。
lowerKey()方法用于返回严格小于给定键的最大键,作为参数传递。简单来说,此方法用于查找作为参数传递的元素之后的下一个最大元素。
句法:
public K TreeMap.lowerKey(K key)
参数:此方法需要一个强制参数键,它是要匹配的键。
返回值:此方法返回严格小于 to key 的最大键,如果没有这样的键,则返回 null。
例子:
Java
// Java Program to Get TreeMap Key, Value, then
// Entry Greater or Less than the Specified Value
// using lowerKey() and higherKey() of Tree class
// Importing all classes of
// java.util package
import java.util.*;
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a TreeMap
TreeMap tree
= new TreeMap();
// Inserting the Elements into TreeMap
tree.put(100, "=> Welcoming");
tree.put(120, "=> you to ");
tree.put(140, "=> computer science portal");
tree.put(200, "=> Geeks for Geeks");
// Returning the smallest key among all the keys
// greater than 150
System.out.println("Smallest key among key > 150 : "
+ tree.higherKey(150));
// Returning the greatest key among all the keys
// smaller than 150
System.out.println("Greatest key among key < 150 : "
+ tree.lowerKey(150));
}
}
Smallest key among key > 150 : 200
Greatest key among key < 150 : 140
方法三:通过TreeMap类的higherEntry().getValue()函数和lowerEntry().getValue()函数打印大于或小于指定key的key对应的值
Java.util.TreeMap类的highEntry()方法用于返回与严格大于给定键的最小键关联的键值映射,如果没有这样的键,则返回 null 而 java 的lowerEntry()方法。 util.TreeMap类用于返回与严格小于给定键的最大键关联的键值映射,如果没有这样的键,则返回 null。
例子:
Java
// Java Program to Get TreeMap Key, Value, or
// Entry Greater or Less than the Specified Value
// using higherEntry().getValue() function and
// lowerEntry().getValue()
// Importing all classes of
// java.util package
import java.util.*;
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a TreeMap
TreeMap tree
= new TreeMap();
// Inserting the Elements into TreeMap
tree.put(100, "=> Welcoming");
tree.put(120, "=> you to ");
tree.put(140, "=> computer science portal");
tree.put(200, "=> Geeks for Geeks");
// Returning the value corresponding to the key
// which is smallest among the keys greater than 150
System.out.println(
tree.higherEntry(150).getValue());
// Returning the value corresponding to the key
// which is greatest among the keys smaller than 150
System.out.println(tree.lowerEntry(150).getValue());
}
}
=> Geeks for Geeks
=> computer science portal