I am reading about the TreeSet and found it interesting. I have one question lets we have a TreeSet of String. How does this Ceiling and floor function will behave in this case.

 NavigableSet<String> ns= new TreeSet<String>();ns.add("Yogi");ns.add("Yogendra");ns.add("Yogesh");ns.add("hello");String ns1= ns.ceiling("Yog");System.out.println(ns1);Output==> Yogendra
3

Best Answer


See here for documentation of those two methods.

It's easier to explain for Numbers then for String as String internally will use compareTo method, unless you pass custom Comparator to constructor of treeset. Consider this code:

TreeSet<Integer> set = new TreeSet<Integer>();set.add(1);set.add(2);set.add(3);set.add(4);set.add(5);

Cieling says

Returns the least element in this set greater than or equal to the given element, or null if there is no such element.

So if i want to search for 10, i dont see any element greater than 10 and hence it will return me null. Wile if i want to search for 0, the next greater element is 1, so it will return 1. While if i give 4, then i have exact match so it will return 4.

While floor says:

Returns the greatest element in this set less than or equal to the given element, or null if there is no such element

So if i want to search for 0, i dont see any element less than 0 in the entire set and hence it will return me null. Wile if i want to search for 10, the next lesser element is 5, so it will return 5. While if i give 4, then i have exact match so it will return 4.

For String it will internally call compareTo method and compare two string lexicographically and the behavior would be same as what is for integer.

They would behave the same way as they behave for any reference type you choose to put in a TreeSet<E>.

The returned element will be determined by the ordering of the TreeSet, which would either be the natural ordering of Strings (lexicographical order) if you don't supply a Comparator<String> to the constructor, or the ordering determined by the Comparator<String> you choose to use.

 TreeSet<String> treeSet = new TreeSet<String>();treeSet.add("aaa");treeSet.add("ddd");treeSet.add("bbb");treeSet.add("ccc");System.out.println(treeSet.ceiling("aaa"));output : aaaSystem.out.println(treeSet.ceiling("aab"));output : bbbSystem.out.println(treeSet.ceiling("bbb"));output : bbbSystem.out.println(treeSet.ceiling("bbbb"));output : cccSystem.out.println(treeSet.ceiling("dddd"));output : nullSystem.out.println(treeSet.floor("aaa"));System.out.println(treeSet.floor("aaa"));output : aaaSystem.out.println(treeSet.floor("aab"));output : aaaSystem.out.println(treeSet.floor("bbb"));output : bbbSystem.out.println(treeSet.floor("bbbb"));output : bbbSystem.out.println(treeSet.floor("aa"));output : null

As per Javadoc :

Ceiling Returns the least element in this set greater than or equal to the given element, or null if there is no such element.

Floor Returns the greatest element in this set less than or equal to the given element, or null if there is no such element.