Thursday, July 4, 2024
HomeLanguagesJavaNavigableSet headSet() method in Java

NavigableSet headSet() method in Java

The headSet() method of NavigableSet interface in Java is used to return 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.

Note: The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.

Syntax:

NavigableSet<E> headSet(E toElement,
                        boolean inclusive)

Where, E is the type of elements maintained by this Set container.

Parameters: This function has two parameters :

  • toElement – high endpoint of the returned set
  • inclusive – true if the high endpoint is to be included in the returned view

Return Value: It return 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 headSet() method in Java:

Program 1: NavigableSet with integer elements.




// A Java program to demonstrate
// headSet() method of NavigableSet
  
import java.util.NavigableSet;
import java.util.TreeSet;
  
public class GFG {
    public static void main(String[] args)
    {
        NavigableSet<Integer> ns = new TreeSet<>();
        ns.add(0);
        ns.add(1);
        ns.add(2);
        ns.add(3);
        ns.add(4);
        ns.add(5);
        ns.add(6);
  
        System.out.println("Map with key-value less than "+
                        "given argument : " + ns.headSet(6));
  
        System.out.println("Map with key-value less than or"
          " equal to  given argument : " + ns.headSet(6, true));
    }
}


Output:

Map with key-value less than given argument : [0, 1, 2, 3, 4, 5]
Map with key-value less than or equal to  given argument : [0, 1, 2, 3, 4, 5, 6]

Program 2: NavigableSet with string elements.




// A Java program to demonstrate
// headSet?() method of NavigableSet
  
import java.util.NavigableSet;
import java.util.TreeSet;
  
public class GFG {
    public static void main(String[] args)
    {
        NavigableSet<String> ns = new TreeSet<>();
        ns.add("A");
        ns.add("B");
        ns.add("C");
        ns.add("D");
        ns.add("E");
        ns.add("F");
        ns.add("G");
  
        System.out.println("Map with key-value less than"+
                    " given argument : " + ns.headSet("F"));
  
        System.out.println("Map with key-value less than "+
           "or equal to given argument : " + ns.headSet("F"));
    }
}


Output:

Map with key-value less than given argument : [A, B, C, D, E]
Map with key-value less than or equal to given argument : [A, B, C, D, E]

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/NavigableSet.html#headSet(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