Java TreeSet 特殊方法
TreeSet 是Java中SortedSet接口最重要的实现之一,它使用Tree进行存储。 TreeSet 通过继承AbstractSet类实现了NavigableSet接口。这意味着存储在 TreeSet 中的元素是有序的,即升序。由于TreeSet的这个特性,它除了传统的Collection接口提供的方法外,还提供了一些令人惊奇的NavigableSet接口方法。
Note: Since TreeSet is the implementation of Set, therefore it does not allow duplication of elements.
TreeSet 提供的一些具体方法如下:
- floor()方法
- 下()方法
- 天花板()方法
- 更高()方法
- subSet()方法
- headSet()方法
- tailSet()方法
现在让我们讨论所有方法及其实现,如下所示
方法一: floor()方法
此方法返回集合中小于或等于给定元素的最大元素,如果没有这样的元素,则返回 null。
参数:需要搜索小于或等于值的数字
句法:
treeSetObject.floor(argument) ;
例子:
Java
// Java Program to demonstrate Floor Method in TreeSet
// Importing input output classes
import java.io.*;
// Importing TreeSet and Set classes from java.util package
import java.util.Set;
import java.util.TreeSet;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of TreeSet of Integer type
// Initializing object with integer values using
// Set.of() method
TreeSet treeSet
= new TreeSet<>(Set.of(12, 98, 54, 37, 68));
// Print and display elements in object
System.out.println("Tree set = " + treeSet);
// Case 1
// Using floor() method over treeSet elements but
// note floor() method is inclsive of the limit
// Hence maximum element is returned
System.out.println("floor(60) = "
+ treeSet.floor(60));
// Case 2
// Using floor() method over treeSet elements,
// therefore output of the below line will be the
// number itself
System.out.println("floor(54) = "
+ treeSet.floor(54));
}
}
Java
// Java Program to demonstrate Lower Method in TreeSet
// Importing input output classes
import java.io.*;
// Importing TreeSet and Set classes from java.util package
import java.util.Set;
import java.util.TreeSet;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of TreeSet of Integer type
// Initializing object with integer values using
// Set.of() method
TreeSet treeSet
= new TreeSet<>(Set.of(12, 98, 54, 37, 68));
// Print and display elements in object
System.out.println(treeSet);
// Case 1
// Using lower() method ober treeSet elements where
// argument passed is greater then max elememnt in
// TreeSet therefore returning max element itself
System.out.println("lower(90) = "
+ treeSet.lower(90));
// Case 2
// Using lower() method ober treeSet elements where
// argument passed is not greater then max elememnt
// in TreeSet therefore returning element lesser
// than that passed as an argumennt
System.out.println("lower(68) = "
+ treeSet.lower(68));
// Also note that lower() method is exclusive of the
// limit
}
}
Java
// Java Program to demonstrate Ceiling Method in TreeSet
// Importing input output classes
import java.io.*;
// Importing TreeSet and Set classes from java.util package
import java.util.Set;
import java.util.TreeSet;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of TreeSet of Integer type
// Initializing object with integer values using
// Set.of() method
TreeSet treeSet
= new TreeSet<>(Set.of(12, 98, 54, 37, 68));
// Print and display elements in object
System.out.println("tree set = " + treeSet);
// Using ceiling() method.
System.out.println("ceiling(50) = "
+ treeSet.ceiling(50));
// Note that ceiling method is inclsive of the
// limit. Therefore,output of the below line will be
// the same number.
System.out.println("ceiling(68) = "
+ treeSet.ceiling(68));
// Verification for null
System.out.println("ceiling(100) = "
+ treeSet.ceiling(100));
}
}
Java
// Java Program to demonstrate Higher Method of TreeSet
// Importing input output classes
import java.io.*;
// Importing Set snd TreeSet classes from java.util package
import java.util.Set;
import java.util.TreeSet;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of TreeSet of Integer type
// Initializing object with integer values using
// Set.of() method
TreeSet treeSet
= new TreeSet<>(Set.of(12, 98, 54, 37, 68));
// Print and display elements in object
System.out.println("tree set = " + treeSet);
// Using higher() method
// Case 1
System.out.println("higher(50) = "
+ treeSet.higher(50));
// Case 2
// Note that higher method is exclusive of the limit
// causing
// output be the number greater than the number
// passed
System.out.println("higher(68) = "
+ treeSet.higher(68));
// Case 3
// Verification for null
System.out.println("higher(98) = "
+ treeSet.higher(100));
}
}
Java
// Java Program to demonstrate subset Method in TreeSet
// Importing input output classes
import java.io.*;
// Importing Set snd TreeSet classes from java.util package
import java.util.Set;
import java.util.TreeSet;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of TreeSet of Integer type
// Initializing object with integer values using
// Set.of() method
TreeSet treeSet
= new TreeSet<>(Set.of(12, 98, 54, 37, 68));
// Print and display elements in object
System.out.println("tree set = " + treeSet);
// Using subSet() method
// Case 1
System.out.println(
"Elements between 54 and 98 are : "
+ treeSet.subSet(54, 98));
// Case 2
// If we want to incude the upper limit
System.out.println(
"Elements between 54 and 98 including both the limits : "
+ treeSet.subSet(54, true, 98, true));
// Case 3
// If we want to exclude the lower limit
System.out.println(
"Elements between 54 (exclusive) and 98 (inclusive) : "
+ treeSet.subSet(54, false, 98, true));
// Case 4
// If we want to exclude both the limits
System.out.println(
"Elements between 54 (exclusive) and 98 (exclusive) : "
+ treeSet.subSet(54, false, 98, false));
}
}
Java
// Java Program to demonstrate headSet Method in TreeSet
// Importing input output classes
import java.io.*;
// Importing Set snd TreeSet classes from java.util package
import java.util.Set;
import java.util.TreeSet;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of TreeSet of Integer type
// Initializing object with integer values using
// Set.of() method
TreeSet treeSet
= new TreeSet<>(Set.of(12, 98, 54, 37, 68));
// Print and display elements in object
System.out.println("tree set = " + treeSet);
// Implementing headSet() method via TreeSet object
// Case 1
System.out.println("Elements upto 90 : "
+ treeSet.headSet(90));
// Case 2
// The function limit is exclusive of the argument
// passed
System.out.println("Elements upto 68 : "
+ treeSet.headSet(68));
// Case 3
// If we want to include the number passed, then
System.out.println("Elements upto 68 (inclusive) : "
+ treeSet.headSet(68, true));
}
}
Java
// Java Program to demonstrate tailSet Method in TreeSet
// Importing input output classes
import java.io.*;
import java.util.Set;
import java.util.TreeSet;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of TreeSet of Integer type
// Initializing object with integer values using
// Set.of() method
TreeSet treeSet
= new TreeSet<>(Set.of(12, 98, 54, 37, 68));
// Print and display elements in object
System.out.println("tree set = " + treeSet);
// Implementing tailSet()
// Case 1
System.out.println("Elements above 50 : "
+ treeSet.tailSet(50));
// Case 2
// The function limit is inclusive of the argument
// passed
System.out.println("Elements above 54 : "
+ treeSet.tailSet(54));
// Case 3
// If we want to exclude the number passed
System.out.println(
"Elements above 54 (exclusive) : "
+ treeSet.tailSet(54, false));
}
}
Tree set = [12, 37, 54, 68, 98]
floor(60) = 54
floor(54) = 54
方法二: lower()方法
此方法返回此集合中严格小于给定元素的最大元素,如果没有这样的元素,则返回 null。
参数:需要找到小于值的数字。
句法:
treeSetObject.lower(argument) ;
例子:
Java
// Java Program to demonstrate Lower Method in TreeSet
// Importing input output classes
import java.io.*;
// Importing TreeSet and Set classes from java.util package
import java.util.Set;
import java.util.TreeSet;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of TreeSet of Integer type
// Initializing object with integer values using
// Set.of() method
TreeSet treeSet
= new TreeSet<>(Set.of(12, 98, 54, 37, 68));
// Print and display elements in object
System.out.println(treeSet);
// Case 1
// Using lower() method ober treeSet elements where
// argument passed is greater then max elememnt in
// TreeSet therefore returning max element itself
System.out.println("lower(90) = "
+ treeSet.lower(90));
// Case 2
// Using lower() method ober treeSet elements where
// argument passed is not greater then max elememnt
// in TreeSet therefore returning element lesser
// than that passed as an argumennt
System.out.println("lower(68) = "
+ treeSet.lower(68));
// Also note that lower() method is exclusive of the
// limit
}
}
[12, 37, 54, 68, 98]
lower(90) = 68
lower(68) = 54
方法三:天花板()方法
此方法返回此集合中大于或等于给定元素的最小元素,如果没有这样的元素,则返回 null。
参数:需要找到小于或等于值的数字。
句法:
treeSetObject.lower(argument) ;
例子:
Java
// Java Program to demonstrate Ceiling Method in TreeSet
// Importing input output classes
import java.io.*;
// Importing TreeSet and Set classes from java.util package
import java.util.Set;
import java.util.TreeSet;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of TreeSet of Integer type
// Initializing object with integer values using
// Set.of() method
TreeSet treeSet
= new TreeSet<>(Set.of(12, 98, 54, 37, 68));
// Print and display elements in object
System.out.println("tree set = " + treeSet);
// Using ceiling() method.
System.out.println("ceiling(50) = "
+ treeSet.ceiling(50));
// Note that ceiling method is inclsive of the
// limit. Therefore,output of the below line will be
// the same number.
System.out.println("ceiling(68) = "
+ treeSet.ceiling(68));
// Verification for null
System.out.println("ceiling(100) = "
+ treeSet.ceiling(100));
}
}
tree set = [12, 37, 54, 68, 98]
ceiling(50) = 54
ceiling(68) = 68
ceiling(100) = null
方法四: higher()方法
此方法返回此集合中严格大于给定元素的最小元素,如果没有这样的元素,则返回 null。
参数:需要找到小于 value 的数字。
句法:
treeSetObject.lower(argument) ;
例子:
Java
// Java Program to demonstrate Higher Method of TreeSet
// Importing input output classes
import java.io.*;
// Importing Set snd TreeSet classes from java.util package
import java.util.Set;
import java.util.TreeSet;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of TreeSet of Integer type
// Initializing object with integer values using
// Set.of() method
TreeSet treeSet
= new TreeSet<>(Set.of(12, 98, 54, 37, 68));
// Print and display elements in object
System.out.println("tree set = " + treeSet);
// Using higher() method
// Case 1
System.out.println("higher(50) = "
+ treeSet.higher(50));
// Case 2
// Note that higher method is exclusive of the limit
// causing
// output be the number greater than the number
// passed
System.out.println("higher(68) = "
+ treeSet.higher(68));
// Case 3
// Verification for null
System.out.println("higher(98) = "
+ treeSet.higher(100));
}
}
tree set = [12, 37, 54, 68, 98]
higher(50) = 54
higher(68) = 98
higher(98) = null
方法五: subSet()方法
此方法将返回范围从“ fromElement ”到“ toElement ”的元素。
Note: ‘fromElement’ is inclusive and ‘toElement’ is exclusive.
句法:
treeSetObject.subSet(fromElement, toElement);
// where fromElement is the lower limit (inclusive) and
// toElement is the upper limit (exclusive)
// of the set containing values between these limits
例子:
Java
// Java Program to demonstrate subset Method in TreeSet
// Importing input output classes
import java.io.*;
// Importing Set snd TreeSet classes from java.util package
import java.util.Set;
import java.util.TreeSet;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of TreeSet of Integer type
// Initializing object with integer values using
// Set.of() method
TreeSet treeSet
= new TreeSet<>(Set.of(12, 98, 54, 37, 68));
// Print and display elements in object
System.out.println("tree set = " + treeSet);
// Using subSet() method
// Case 1
System.out.println(
"Elements between 54 and 98 are : "
+ treeSet.subSet(54, 98));
// Case 2
// If we want to incude the upper limit
System.out.println(
"Elements between 54 and 98 including both the limits : "
+ treeSet.subSet(54, true, 98, true));
// Case 3
// If we want to exclude the lower limit
System.out.println(
"Elements between 54 (exclusive) and 98 (inclusive) : "
+ treeSet.subSet(54, false, 98, true));
// Case 4
// If we want to exclude both the limits
System.out.println(
"Elements between 54 (exclusive) and 98 (exclusive) : "
+ treeSet.subSet(54, false, 98, false));
}
}
tree set = [12, 37, 54, 68, 98]
Elements between 54 and 98 are : [54, 68]
Elements between 54 and 98 including both the limits : [54, 68, 98]
Elements between 54 (exclusive) and 98 (inclusive) : [68, 98]
Elements between 54 (exclusive) and 98 (exclusive) : [68]
方法六: headSet()方法
此方法将返回 TreeSet 中小于指定元素的元素。
句法:
treeSetObject.headSet(upToNumber) ;
Note: ‘upToNumber‘ is the upper limit of the numbers to be found (exclusive)
例子:
Java
// Java Program to demonstrate headSet Method in TreeSet
// Importing input output classes
import java.io.*;
// Importing Set snd TreeSet classes from java.util package
import java.util.Set;
import java.util.TreeSet;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of TreeSet of Integer type
// Initializing object with integer values using
// Set.of() method
TreeSet treeSet
= new TreeSet<>(Set.of(12, 98, 54, 37, 68));
// Print and display elements in object
System.out.println("tree set = " + treeSet);
// Implementing headSet() method via TreeSet object
// Case 1
System.out.println("Elements upto 90 : "
+ treeSet.headSet(90));
// Case 2
// The function limit is exclusive of the argument
// passed
System.out.println("Elements upto 68 : "
+ treeSet.headSet(68));
// Case 3
// If we want to include the number passed, then
System.out.println("Elements upto 68 (inclusive) : "
+ treeSet.headSet(68, true));
}
}
tree set = [12, 37, 54, 68, 98]
Elements upto 90 : [12, 37, 54, 68]
Elements upto 68 : [12, 37, 54]
Elements upto 68 (inclusive) : [12, 37, 54, 68]
方法七:
此方法将返回大于或等于指定元素的 TreeSet 元素。
句法:
treeSetObject.tailSet(aboveThisNumber) ;
Note: ‘aboveThisNumber’ is the lower limit of the numbers to be found (inclusive).
例子:
Java
// Java Program to demonstrate tailSet Method in TreeSet
// Importing input output classes
import java.io.*;
import java.util.Set;
import java.util.TreeSet;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of TreeSet of Integer type
// Initializing object with integer values using
// Set.of() method
TreeSet treeSet
= new TreeSet<>(Set.of(12, 98, 54, 37, 68));
// Print and display elements in object
System.out.println("tree set = " + treeSet);
// Implementing tailSet()
// Case 1
System.out.println("Elements above 50 : "
+ treeSet.tailSet(50));
// Case 2
// The function limit is inclusive of the argument
// passed
System.out.println("Elements above 54 : "
+ treeSet.tailSet(54));
// Case 3
// If we want to exclude the number passed
System.out.println(
"Elements above 54 (exclusive) : "
+ treeSet.tailSet(54, false));
}
}
tree set = [12, 37, 54, 68, 98]
Elements above 50 : [54, 68, 98]
Elements above 54 : [54, 68, 98]
Elements above 54 (exclusive) : [68, 98]