The NavigableSet.lower(E)
Javadoc says it returns the greatest element in this set strictly less than the given element, or null
if there is no such element. Why is 1 the output here? Shouldn't it be 4?
NavigableSet original = new TreeSet();
original.add("1");
original.add("2");
original.add("3");
original.add("4");
original.add("10");
Object lower = original.lower("10");
System.out.println(lower);
The greater than symbol means the number on the left is greater than the number on the right. The greater than or equal symbol means the number on the left is greater than or equal to the number on the right. The less than symbol means that the number on the left is less than the number on the right.
One is not bigger than 2. We define 2 to be the successor of 1, ie 1+ and we prove that 1+1=1+.
The symbol used to represent the less than inequality is “< “. Less than sign is a universally adopted math symbol of two equal measure strokes that meet in the acute angle at the left.
Examples: 5/4 ( greater than 1 and the numerator is larger than the denominator). 6/5 ( more than1 ) 8/6 ( more than1 )
Because the values are String
(s) the Set
is comparing by lexical order. Please, don't use Raw Types
.
NavigableSet<Integer> original = new TreeSet<>();
original.add(1);
original.add(2);
original.add(3);
original.add(4);
original.add(10);
Object lower = original.lower(10);
System.out.println(lower);
Output is
4
thats because you are comparing Strings here, and not as assumed integers. So the actual order in the Set is : 1, 10, 2, 3, 4 !
Use the generics : NavigableSet<Integer> original = new TreeSet<>();
and add the values as integers :
original.add(1);
and so on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With