# Java NavigableSet

 Jakob Jenkov Last update: 2019-10-03

The Java NavigableSet interface, `java.util.NavigableSet`, is a subtype of the Java SortedSet interface. Therefore the `NavigableSet` behaves like a `SortedSet`, but with an additional set of navigation methods available in addition to the sorting mechanisms of the `SortedSet`. In this text I will look closer at some of the navigation methods of the Java `NavigableSet`.

## Java NavigableSet Implementations

In Java 6 to 13 there is only one implementation of the `NavigableSet` interface in the `java.util` package: The `java.util.TreeSet` class. There is an implementation in the `java.util.concurrent` package called `ConcurrentSkipListSet` but that is outside the scope of this trail.

## Create a NavigableSet

To create a Java `NavigableSet` you must create an instance of one of the classes implementing the `NavigableSet` interface. Here is an example of creating an instance of the class `TreeSet` which implements the `NavigableSet` interface:

```NavigableSet navigableSet = new TreeSet();
```

## descendingSet()

The `descendingSet()` method returns a `NavigableSet` in which the order of the elements is reversed compared to this one. The returned "view" is backed by the original `NavigableSet`, so changes to the descending set are also reflected in the original set.

Here is a simple example:

```NavigableSet reverse = original.descendingSet();
```

## descendingIterator()

The `descendingIterator()` method allows you to iterate the elements of the `NavigableSet` (which is also a `SortedSet`) in reverse order, without changing the order of the elements internally.

```Iterator reverse = original.descendingIterator();
```

The `headSet()` method returns a view of the original `NavigableSet` which only contains elements that are "less than" the given element. Here is an example:

```NavigableSet original = new TreeSet();

//this headset will contain "1" and "2"

//this headset will contain "1", "2", and "3" because "inclusive"=true
```

## tailSet()

The `tailSet()` method works the same way as the `headSet()` method, except it returns all elements that are equal to or higher than the given parameter element. Here is a `NavigableSet` `tailSet()` method example:

```NavigableSet original = new TreeSet();

//this tailSet will contain "2" and "3"
SortedSet tailSet = original.tailSet("2");

//this tailSet will contain "3" only because "inclusive"=false
NavigableSet tailSet = original.tailSet("2", false);

```

## subSet()

The `subSet()` method allows you to pass two parameters demarcating the boundaries of the view set to return. The elements matching the first boundary is included, where as elements matching the last boundary are not. Here is a Java `NavigableSet` `subSet()` example:

```NavigableSet original = new TreeSet();

//this subset will contain "2" and "3"
SortedSet    subset  = original.subSet("2", "4");

//this subset will contain "2", "3" and "4" because
//    fromInclusive=true, and toInclusive=true
NavigableSet subset = original.subSet("2", true, "4", true);
```

## ceiling()

The `ceiling()` method returns the least (smallest) element in this set that is greater than or equal to the element passed as parameter to the `ceiling()` method. Here is a Java `NavigableSet` `ceiling()` example:

```NavigableSet original = new TreeSet();

//ceiling will be "3".
Object ceiling = original.ceiling("2");
```

## floor()

The `floor()` method does the opposite of the `ceiling()` method, meaning it returns the greatest element that is less than or equal to the given parameter value. Here is a Java `NavigableSet` `floor()` example:

```NavigableSet original = new TreeSet();

//floor will be "1".
Object floor = original.floor("2");
```

## higher()

The `higher()` method returns the least (smallest) element in this set that is greater than (not equal too) the element passed as parameter to the `higher()` method. Here is a Java `NavigableSet` `higher()` example:

```NavigableSet original = new TreeSet();

//higher will be "3".
Object higher = original.higher("2");
```

## lower()

The `lower()` method does the opposite of the `higher()` method, meaning it returns the highest element that is less than (not equal to) the given parameter. Here is a Java `NavigableSet` `lower()` example:

```NavigableSet original = new TreeSet();

//lower will be "1"
Object lower = original.lower("2");
```

## pollFirst()

The `pollFirst()` method returns and removes the "first" element in the `NavigableSet` or `null` if the set is empty. "First" means smallest element according to the sort order of the set. Here is a Java `NavigableSet` `pollFirst()` example:

```NavigableSet original = new TreeSet();

//first is "1"
Object first = original.pollFirst();
```

## pollLast()

The `pollLast()` method returns and removes the "last" element in the `NavigableSet`. "Last" means largest according to the element sorting order of the set. Here is a Java `NavigableSet` `pollLast()` example:

```NavigableSet original = new TreeSet();

//last is "3"
Object last = original.pollLast();
```
 Tweet Jakob Jenkov
Featured Videos High-Performance
Java Persistence
Close TOC All Trails Trail TOC Page TOC Previous Next
{"ft_lang_label":"__label__en","ft_lang_prob":0.58178854,"math_prob":0.7436336,"size":5514,"snap":"2023-40-2023-50","text_gpt3_token_len":1346,"char_repetition_ratio":0.23303086,"word_repetition_ratio":0.14133333,"special_character_ratio":0.24537541,"punctuation_ratio":0.16122448,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9511602,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-28T10:00:39Z\",\"WARC-Record-ID\":\"<urn:uuid:12ab8386-034d-4b83-a713-78562ee9cfbc>\",\"Content-Length\":\"44961\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8a07281a-fa5f-476c-9f20-1c3bb1614f8c>\",\"WARC-Concurrent-To\":\"<urn:uuid:f4b15eae-7f06-426c-b44f-1115810aaa34>\",\"WARC-IP-Address\":\"157.90.167.190\",\"WARC-Target-URI\":\"https://jenkov.com/tutorials/java-collections/navigableset.html\",\"WARC-Payload-Digest\":\"sha1:VNNEV2SLBXGZJ3RAHQYRWHMZYGGD6WTJ\",\"WARC-Block-Digest\":\"sha1:O6F6G33U4PSARNNWPW3SMSLPDZDHZXP2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510387.77_warc_CC-MAIN-20230928095004-20230928125004-00780.warc.gz\"}"}