Question

I want a tire map like blow:

Map<String,Object> map = new TireMap();
map.put("com","1");
map.put("com.aa","2");
map.put("com.aa.bb","3");

map.get("com");// return ["1","2","3"]
map.get("com.a"); //return ["2","3"]
map.get("com.aa"); //return ["2","3"]
// the key maybe full key or just key prefix

How to implement a map like this? Or is there already exits a map in Java API or open source?

It's very like innodb in mysql.

PS: Performance is very important. The storage items will be more than 1000W.

Was it helpful?

Solution

I would try out TreeMap from java.util. This is a map which does what u need. It manages the keys based on a natural order (defined by a comparator in the key class). The methods tailMap and headMap gives you a map with keys you need.

Example

public static void main(String[] args) {
    TreeMap<String, Object> map = new TreeMap<String, Object>();

    map.put("com","1");
    map.put("com.aa","2");
    map.put("com.aa.bb","3");

    System.out.println(map.get("com"));
    System.out.println(map.tailMap("com").values()); // ["1","2","3"]

    System.out.println(map.get("com.aa")); //return ["2","3"]
    System.out.println(map.tailMap("com.aa").values());
}

OTHER TIPS

The most used map's implementation in java is hashmap or TreeMap (performance depends of what you make with the map (adding data, read, is the data needed to be sorted, ...)), if it was ok just make else you have to make to implément directly map (don't extends HashMap cause you extends all methods and just respecify the method defined in your implementation.

read the doc to know the better implementation of map who's already exist in java or just use a classical implementation to begin to develop your application with

Map<String,Object> map = new TreeMap(); 

and if you want to change the implementation you have just to make what you want who implements mapsand change your code in $

Map<String,Object> map = new YourMapImplementation();

ps Joshi : Be carrefull, in your code, your key is an object and dont have to be comparable, it can be provide some bug if he make a mistake

Here's what i implemented to solve the problem, but i am not sure about performance, that needs to be found out.

class TireMap extends HashMap<String, Object> {

@Override
public Object get(Object key) {
    // TODO Auto-generated method stub
    List<Object> listOfObejcts = new ArrayList<Object>();

    String keyString = (String) key;

    for(String s : this.keySet()){

        if(s.startsWith(keyString))
            listOfObejcts.add(super.get(s));
    }

    return listOfObejcts;
}
}


 public class Test 
 {
    public static void main(String args[]) {

    Map<String, Object> map = new TireMap();

    map.put("com", "1");
    map.put("com.aa", "2");
    map.put("com.bb", "3");


    System.out.println(map.get("com"));
    System.out.println(map.get("com.aa"));`
 }

This returns me ["1","2","3"] and ["2","3"] respectively. Please check this out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top