该headset()
方法返回的元素达到定义为参数的限制。
首先,创建一个NavigableSet并添加元素-
NavigableSet<Integer> set = new TreeSet<>(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85); set.add(100);
现在,使用headset()
方法-
set.headSet(55));
以下是一个例子-
import java.util.NavigableSet; import java.util.TreeSet; public class Demo { public static void main(String[] args) { NavigableSet<Integer> set = new TreeSet<>(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85); set.add(100); System.out.println("Returned Value = " + set.headSet(55)); System.out.println("Returned Value (high endpoint included) = " + set.headSet(55, true)); } }
输出结果
Returned Value = [10, 25, 40] Returned Value (high endpoint included) = [10, 25, 40, 55]