Hashtable key within integer interval

后端 未结 7 1893
北恋
北恋 2020-11-30 11:17

I don\'t know if this is possible but i\'m trying to make an Hashtable of where Interval is a class with 2 integer / long values, a start and an end and i wanted to make so

7条回答
  •  清歌不尽
    2020-11-30 12:01

    No need to reinvent the wheel, use a NavigableMap. Example Code:

    final NavigableMap map = new TreeMap();
    map.put(0, "Cry Baby");
    map.put(6, "School Time");
    map.put(16, "Got a car yet?");
    map.put(21, "Tequila anyone?");
    map.put(45, "Time to buy a corvette");
    
    System.out.println(map.floorEntry(3).getValue());
    System.out.println(map.floorEntry(10).getValue());
    System.out.println(map.floorEntry(18).getValue());
    

    Output:

    Cry Baby
    School Time
    Got a car yet?

提交回复
热议问题