Wednesday, July 3, 2024
HomeLanguagesJavaConcurrentSkipListSet floor() method in Java

ConcurrentSkipListSet floor() method in Java

The floor() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java which returns the greatest element in this set less than or equal to the given element, or null if there is no such element.

Syntax:

ConcurrentSkipListSet.floor(E e)

Parameter: The function accepts a single parameter e i.e. the value to match.

Return Value: The function returns the greatest element less than or equal to e, or null if there is no such element.

Exception: The function throws the following exceptions:

  • ClassCastException – if the specified element cannot be compared with the elements currently in the set.
  • NullPointerException – if the specified element is null
  • Below programs illustrate the ConcurrentSkipListSet.floor() method:

    Program 1:




    // Java program to demonstrate floor()
    // method of ConcurrentSkipListSet
      
    import java.util.concurrent.*;
      
    class ConcurrentSkipListSetFloorExample1 {
        public static void main(String[] args)
        {
            // Creating a set object
            ConcurrentSkipListSet<Integer>
                Lset = new ConcurrentSkipListSet<Integer>();
      
            // Adding elements to this set
            for (int i = 10; i <= 50; i += 10)
                Lset.add(i);
      
            // Finding floor of 20 in the set
            System.out.println("The floor of 20 in the set "
                               + Lset.floor(20));
      
            // Finding floor of 39 in the set
            System.out.println("The floor of 39 in the set "
                               + Lset.floor(39));
      
            // Finding floor of 9 in the set
            System.out.println("The floor of 10 in the set "
                               + Lset.floor(9));
        }
    }

    
    
    Output:

    The floor of 20 in the set 20
    The floor of 39 in the set 30
    The floor of 10 in the set null
    

    Program 2: Program to show NullPointerException in floor().




    // Java program to demonstrate floor()
    // method of ConcurrentSkipListSet
      
    import java.util.concurrent.*;
      
    class ConcurrentSkipListSetFloorExample2 {
        public static void main(String[] args)
        {
            // Creating a set object
            ConcurrentSkipListSet<Integer>
                Lset = new ConcurrentSkipListSet<Integer>();
      
            // Adding elements to this set
            for (int i = 10; i <= 50; i += 10)
                Lset.add(i);
      
            // Trying to find the floor of null
            try {
                System.out.println("The floor of null in the set "
                                   + Lset.floor(null));
            }
            catch (Exception e) {
                System.out.println("Exception: " + e);
            }
        }
    }

    
    
    Output:

    Exception: java.lang.NullPointerException
    

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

    Nokonwaba Nkukhwana
    Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
    RELATED ARTICLES

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here

    Most Popular

    Recent Comments