简体   繁体   中英

Accessing the next 3 element values in a Map knowing the key

I have java.util.LinkedHashMap with Integer as Key and Character as Value. I know the key of the element i want to access. In addition to the element value for the key, i also want to retrieve the next 3 element values so that i can append all the 4 element values and form a string with 4 chars. I am asking for something like how we will do in a java.util.List. Is this feasible by any means in a Map/ordered map? Please suggest any other data structure that can help me achieve this. I am using Java 6.

java.util.SortedMap has the methods subMap(fromKey, toKey) and tailMap(fromKey).

You can use the first one if you know the last key you want, but in your case since you want a fixed number of elements, you should try

SortedMap<Integer, Character> tail = l.tailMap(yourKey);
int cnt = 0;
List<Char> result = new ArrayList<Character>();
for (Iterator<Entry<Integer, Character>> it = tail.iterator(); it.hasNext() && cnt < 3; cnt++) {
  reasult.add(it.next());
}

Note that if the values of the map were not immutable, changes in the list would be reflected in the map and vice versa, since I added the reference of the element to the list(to avoid this you should add a copy of the object in the list).

Maps have a way at getting collections to represent both the key set and the entry set. In this case you'd want to get the key set. Map itself doesn't have an iterator, but this set will, and you can use that iterator to get the values for the next 3 keys.

That being said, I'm not sure LinkedHashMap is the best choice for Map implementation. The elements will be ordered by entry order or last reference order, depending on the type of LinkedHashMap.

You can use a TreeMap which orders the elements by key using a comparator.

LinkedHashMap does not offer that ability, and unfortunately, uses private inner classes and package-private methods for its implementation, so you cannot do anything in a subclass either.

The LinkedMap from Apache commons-collections has exactly the functionality you want (the inherited entryAfter() method). Too bad it's not typesafe...

I don't believe so, but implementing your own LinkedHashSet that can do this will take you like twenty minutes.

You need two classes, a trivial node class that holds your key, value and a nextNode pointer, and a new "LinkedHashSet" class.

Aside from the standard getters, your node class should implement equals and hash to delegate to key.equals and key.hash...

Your LinkedHashSet should contain a HashSet, a pointer to the first node and a few facilitating methods so that it's user never has to see the HashSet or the Node objects (they should both be package--not public).

Simply build the linked list using your nodes just as you would any other linked list, but at the same time add the node to the hashmap. The overridden methods in Node will ensure that the values can be looked up in the map, but in order to add you are going to have to traverse the entire list starting from the Head node just as you would with any linked list.

After that just implement forwarding methods for add, remove and whatever else you need. Implmenting a LinkedHashSet.getNext(item) is as easy as looking up the item's node and getting it's nextNode, pulling the item out of the next node and returning it.

The one thing that's slightly tricky, for any lookup like get(item) you will have to actually implement something like this (Excuse my lack of generics):

public Object get(Item item) {
    node=new Node(item);
    hashSet.get(node);
    return node.itemValue();
}

Note that you have to encase the item in a node even though it's a lookup--the Hash will require that in order to operate correctly.

As long as I'm writing a little code, here's the code for getNext(item)

public Object getNext(Item item) {
    node=new Node(item);
    hashSet.get(node);
    return node.getNextNode().itemValue();
}

Anyway, it's a 20 minute effort and very educational--just do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM