pranjal sharma pranjal sharma - 3 years ago 250
Java Question

cannot find symbol: method ceiling(int)

I can't understand why I am getting this error despite

ceiling
and
floor
functions are present in the
TreeSet
class. I get following error:

cannot find symbol: method ceiling(int)


Here is my Java code:

import java.util.Set;
import java.util.TreeSet;

public static boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
Set<Integer> set = new TreeSet<>();
for (int i = 0; i<nums.length; i++) {
Integer x = set.floor(nums[i]);
if(x!= null && x<= nums[i]+t)
return true;
Integer y = set.ceiling(nums[i]);
if(y!= null && nums[i]<= y+t)
return true;
set.add(nums[i]);
if(set.size()>k)
set.remove(nums[i-k]);


}
return false;
}

Answer Source

Change

 Set<Integer> set = new TreeSet<>();

To

TreeSet<Integer> set = new TreeSet<>();

and you will be able to call the methods. The methods ceiling and floor do not exist for just Set.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download