• Tags
  •         
  • www.breakyizhan.com
  •    

    Java.util.TreeSet.headSet()方法用作TreeSet的限制设置器,以排除元素的方式将元素返回到方法参数中定义的限制。

    句法:

    head_set =(TreeSet)tree_set.headSet(Object element)

    参数:参数元素是树集类型,并且是头部点,该头部点是允许树返回除元素本身之外的值的限制。

    返回值:该方法以排序方式返回值的一部分,该方式严格小于参数中提到的元素。

    下面的程序说明了java.util.TreeSet.headSet()的使用:

    程序1:在排序的TreeSet中:

    // Java code to illustrate headSet()
    import java.io.*;
    import java.util.Iterator;
    import java.util.TreeSet;
    
    public class Tree_Set_Demo {
        public static void main(String[] args)
        {
    
            // Creating an empty TreeSet
            TreeSet<Integer> tree_set = new TreeSet<Integer>();
    
            // Adding the elements using add()
            tree_set.add(1);
            tree_set.add(2);
            tree_set.add(3);
            tree_set.add(4);
            tree_set.add(5);
            tree_set.add(10);
            tree_set.add(20);
            tree_set.add(30);
            tree_set.add(40);
            tree_set.add(50);
    
            // Creating the headSet tree
            TreeSet<Integer> head_set = new TreeSet<Integer>();
    
            // Limiting the values till 5
            head_set = (TreeSet<Integer>)tree_set.headSet(30);
    
            // Creating an Iterator
            Iterator iterate;
            iterate = head_set.iterator();
    
            // Displaying the tree set data
            System.out.println("The resultant values till head set: ");
    
            // Iterating through the headSet
            while (iterate.hasNext()) {
                System.out.println(iterate.next() + " ");
            }
        }
    }
    

    输出:

    The resultant values till head set: 
    1 
    2 
    3 
    4 
    5 
    10 
    20

    程序2:在未排序的树集中:

    // Java code to illustrate headSet()
    import java.io.*;
    import java.util.Iterator;
    import java.util.TreeSet;
    
    public class Tree_Set_Demo {
        public static void main(String[] args)
        {
    
            // Creating an empty TreeSet
            TreeSet<Integer> tree_set = new TreeSet<Integer>();
    
            // Adding the elements using add()
            tree_set.add(9);
            tree_set.add(2);
            tree_set.add(100);
            tree_set.add(40);
            tree_set.add(50);
            tree_set.add(10);
            tree_set.add(20);
            tree_set.add(30);
            tree_set.add(15);
            tree_set.add(16);
    
            // Creating the headSet tree
            TreeSet<Integer> head_set = new TreeSet<Integer>();
    
            // Limiting the values till 5
            head_set = (TreeSet<Integer>)tree_set.headSet(30);
    
            // Creating an Iterator
            Iterator iterate;
            iterate = head_set.iterator();
    
            // Displaying the tree set data
            System.out.println("The resultant values till head set: ");
    
            // Iterating through the headSet
            while (iterate.hasNext()) {
                System.out.println(iterate.next() + " ");
            }
        }
    }
    

    输出:

    The resultant values till head set: 
    2 
    9 
    10 
    15 
    16 
    20

    程序3:在未排序的树集中但具有String类型元素:

    // Java code to illustrate headSet()
    import java.io.*;
    import java.util.Iterator;
    import java.util.TreeSet;
    
    public class Tree_Set_Demo {
        public static void main(String[] args)
        {
    
            // Creating an empty TreeSet
            TreeSet<String> tree_set = new TreeSet<String>();
    
            // Adding the elements using add()
            tree_set.add("Welcome");
            tree_set.add("To");
            tree_set.add("Geek");
            tree_set.add("4");
            tree_set.add("Geeks");
            tree_set.add("TreeSet");
    
            // Creating the headSet tree
            TreeSet<String> head_set = new TreeSet<String>();
    
            // Limiting the values till 5
            head_set = (TreeSet<String>)tree_set.headSet("To");
    
            // Creating an Iterator
            Iterator iterate;
            iterate = head_set.iterator();
    
            // Displaying the tree set data
            System.out.println("The resultant values till head set: ");
    
            // Iterating through the headSet
            while (iterate.hasNext()) {
                System.out.println(iterate.next() + " ");
            }
        }
    }
    

    输出:

    The resultant values till head set: 
    4 
    Geek 
    Geeks

     
    转载请保留页面地址:https://www.breakyizhan.com/java/4924.html