I have a scenario where I have to fetch all the elements in the bucket of a HashMap / Set by using generated hashcode. For example,
public class Interval{
private int minimum;
private int maximum;
Interval(int minimum, int maximum){
this.minimum = minimum;
this.maximum = maximum;
}
//getters &setters
@Override
public int hashCode() {
//for now say I return hash as below
return (minimum % 500)
}
}
Set <Interval> intervals = new <>Set();
intervals.add(new Interval(10,100));
intervals.add(new Interval(20,300));
intervals.add(new Interval(50,100));
intervals.add(new Interval(501,1000));
I will generate same hashcode for a interval falling between 500 intervals i.e. any interval between 1-500 will generate same hashcode and hence will be put in same bucket. Hence, for above, the internal data strcture could look like
|wsed@wed:|Object(10,100),Object(20,300),Object(10,100)|,wesd@3eww:|Object(10,200)||
Suppose here | | block is representing a Set & value inside it are hashcode : Linked List
I know I could do manual implementations of the data structure mentioned but is there any way in java of doing that.
Edit: Why I need it: I need to fetch all elements of same bucket and do merging of intervals to optimise the intervals falling in a range. Hence, I need collision to happen.