Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaConcurrentSkipListSet headSet() method in Java

ConcurrentSkipListSet headSet() method in Java

headSet(E toElement)

The java.util.concurrent.ConcurrentSkipListSet.headSet() method is an in-built function in Java which returns a view of the portion of this set whose elements are strictly less than toElement. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports.

Syntax:

public NavigableSet headSet(E toElement)

Parameters: The function accepts a single parameter toElement i.e. high endpoint of the returned set.

Return Value: The function returns a NavigableSet which is a view of the portion of this set whose elements are strictly less than toElement.

Below programs illustrate the ConcurrentSkipListSet.headSet(E toElement) method:
Program 1:




// Java Program Demonstrate headSet()
// method of ConcurrentSkipListSet */
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetHeadSetExample1 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet<Integer>
            set = new ConcurrentSkipListSet<Integer>();
  
        // Adding elements to this set
        for (int i = 10; i <= 50; i += 10)
            set.add(i);
  
        // Creating a headSet object with upper limit 30
        NavigableSet<Integer> hd_set = set.headSet(30);
  
        // Printing the elements of the set
        System.out.println("Contents of the set: " + set);
  
        // Printing the elements of the headSet set
        System.out.println("Contents of the headset"
                           + " with upper limit 30: "
                           + hd_set);
    }
}


Output:

Contents of the set: [10, 20, 30, 40, 50]
Contents of the headset with upper limit 30: [10, 20]

Program 2: Program to show NullPointerException.




// Java Program Demonstrate headSet()
// method of ConcurrentSkipListSet */
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetHeadSetExample2 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet<Integer>
            set = new ConcurrentSkipListSet<Integer>();
  
        // Adding elements to this set
        for (int i = 10; i <= 50; i += 10)
            set.add(i);
  
        // Printing the elements of the set
        System.out.println("Contents of the set: " + set);
  
        try {
            // Trying to creating a headSet object with upper limit null
            NavigableSet<Integer> hd_set = set.headSet(null);
        }
  
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
        // Printing the elements of the headSet set
        // System.out.println("Contents of the headset"
+" with upper limit 30: "+ hd_set);
    }
}


Output:

Contents of the set: [10, 20, 30, 40, 50]
Exception: java.lang.NullPointerException

headSet(E toElement, boolean inclusive)

The java.util.concurrent.ConcurrentSkipListSet.headSet() method is an in-built function in Java which returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports.

Syntax:

public NavigableSet headSet(E toElement,
                      boolean inclusive)

Parameters: The function accept the following parameters

  • toElement i.e. high endpoint of the returned set.
  • inclusive – true if the high endpoint is to be included in the returned view.
  • Return Value: The function returns a NavigableSet which is a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement.

    Below programs illustrate the ConcurrentSkipListSet.headSet(E toElement, boolean inclusive) method:

    Program 3:




    // Java Program Demonstrate headSet()
    // method of ConcurrentSkipListSet */
      
    import java.util.NavigableSet;
    import java.util.concurrent.ConcurrentSkipListSet;
      
    class ConcurrentSkipListSetHeadSetExample3 {
        public static void main(String[] args)
        {
      
            // Initializing the set
            ConcurrentSkipListSet<Integer>
                set = new ConcurrentSkipListSet<Integer>();
      
            // Adding elements to this set
            for (int i = 10; i <= 50; i += 10)
                set.add(i);
      
            // Creating a headSet object with upper limit 30 inclusive
            NavigableSet<Integer> hd_set = set.headSet(30, true);
      
            // Printing the elements of the set
            System.out.println("Contents of the set: " + set);
      
            // Printing the elements of the headSet set
            System.out.println("Contents of the headset"
                               + " with upper limit 30 inclusive: "
                               + hd_set);
        }
    }

    
    
    Output:

    Contents of the set: [10, 20, 30, 40, 50]
    Contents of the headset with upper limit 30 inclusive: [10, 20, 30]
    

    Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#headSet-E-

    RELATED ARTICLES

    Most Popular

    Recent Comments