How to Get Tail Set from TreeSet in Java


The example aims to get Tail Set from TreeSet containing the values grater than or equal to the specified value in Java.

Source Code

package com.beginner.examples;

import java.util.SortedSet;
import java.util.TreeSet;

public class GetTailSetFromTreeSet {

	public static void main(String[] args) {
		
		//create TreeSet.
		TreeSet treeSet = new TreeSet();
		
		treeSet.add("A");
		treeSet.add("B");
		treeSet.add("C");
		treeSet.add("D");
		
		/*
		 * To get a Tail Set from Java TreeSet use tailSet method of TreeSet.
		 */
		SortedSet sortedSet = treeSet.tailSet("C");
		
		System.out.println("Tail Set Contains : " + sortedSet);

	}

}

Output:

Tail Set Contains : [C, D]

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments