Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is 1 greater than 4?

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);
like image 918
Leo Avatar asked Oct 24 '14 12:10

Leo


People also ask

How do you know if a number is greater?

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.

Is 1 a bigger number than 2?

One is not bigger than 2. We define 2 to be the successor of 1, ie 1+ and we prove that 1+1=1+.

How do you show less than 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.

How do you write a fraction greater than 1?

Examples: 5/4 ( greater than 1 and the numerator is larger than the denominator). 6/5 ( more than1 ) 8/6 ( more than1 )


2 Answers

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
like image 157
Elliott Frisch Avatar answered Oct 22 '22 06:10

Elliott Frisch


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.

like image 26
Terry Storm Avatar answered Oct 22 '22 06:10

Terry Storm