简体   繁体   English

知道键即可访问Map中的下3个元素值

[英]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. 我有java.util.LinkedHashMap,其中Integer作为键,Character作为值。 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. 除了键的元素值之外,我还想检索接下来的3个元素值,以便可以附加所有4个元素值并形成具有4个字符的字符串。 I am asking for something like how we will do in a java.util.List. 我正在要求类似在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 6。

java.util.SortedMap has the methods subMap(fromKey, toKey) and tailMap(fromKey). java.util.SortedMap具有subMap(fromKey,toKey)和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. Map本身没有迭代器,但是此集合可以,并且您可以使用该迭代器获取下三个键的值。

That being said, I'm not sure LinkedHashMap is the best choice for Map implementation. 话虽这么说,我不确定LinkedHashMap是Map实现的最佳选择。 The elements will be ordered by entry order or last reference order, depending on the type of LinkedHashMap. 根据LinkedHashMap的类型,元素将按输入顺序或最后引用顺序进行排序。

You can use a TreeMap which orders the elements by key using a comparator. 您可以使用TreeMap通过比较器按键对元素进行排序。

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. LinkedHashMap不提供此功能,不幸的是,使用私有内部类和package-private方法来实现该功能,因此您也不能在子类中执行任何操作。

The LinkedMap from Apache commons-collections has exactly the functionality you want (the inherited entryAfter() method). 来自Apache commons-collections的LinkedMap完全具有所需的功能(继承的entryAfter()方法)。 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. 我不这么认为,但是实现自己的LinkedHashSet可以花20分钟左右。

You need two classes, a trivial node class that holds your key, value and a nextNode pointer, and a new "LinkedHashSet" class. 您需要两个类,一个用于保存键,值和nextNode指针的普通节点类,以及一个新的“ LinkedHashSet”类。

Aside from the standard getters, your node class should implement equals and hash to delegate to key.equals and key.hash... 除了标准的获取器之外,您的节点类还应实现equals和hash来委托给key.equals和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). 您的LinkedHashSet应该包含一个HashSet,一个指向第一个节点的指针和一些辅助方法,以便用户永远不必看到HashSet或Node对象(它们都应该打包-不公开)。

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. Node中的重写方法将确保可以在映射中查找值,但是要进行添加,您将必须像从任何链接列表开始一样,从Head节点开始遍历整个列表。

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. 放大LinkedHashSet.getNext(item)就像查找项目的节点并获取它的nextNode一样简单,将项目从下一个节点中拉出并返回。

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): 一件事有些棘手,对于任何类似get(item)的查找,您都必须实际实现以下内容(对不起,我缺少泛型):

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) 只要我写一点代码,这就是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. 无论如何,这是20分钟的努力,而且非常有教育意义-做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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