问题描述
我需要将键值对输入到数据结构中,这样我们可以按键升序检索它们 - 但是它们可能是许多相同值的键。
$ b因此,如果kv对是{10-a,10-b,9-c,8-d,8-e,8-f,4-g,4-h,2-i}我需要按照以下顺序检索值:a,b,c,d,e,f,g,h,i。在JAVA API中是否有任何数据结构支持这一点?
我尝试使用TreeMap,因为它保持它们的顺序,允许我使用TreeMap.lastKey()检索最高的当前密钥,但是我不知道它覆盖了已经在地图中的任何重复的密钥。我需要一些不会覆盖的东西(类似于HASH),但也允许我按照排序顺序检索它们 - 这是否存在?
谢谢!
不幸的是,您可能不会找到支持多个相同键值的结构。正如Dilum所说,MultiMap或多值地图的几个实现将会很好地运行。
除了Guava的,还有Spring Framework的和Apache Common的。
Spring实现的一个例子是:
import org.springframework.util .LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
public class MultiValueMapExample {
public static void main(String [] args){
// 10-a,10-b,9 -c,8-d,8-e,8-f,4-g,4-h,2-i
MultiValueMap< Integer,String> map = new LinkedMultiValueMap< Integer,String>();
map.add(10,a);
map.add(10,b);
map.add(9,c);
map.add(8,d);
map.add(8,e);
map.add(8,f);
map.add(8,g);
map.add(4,h);
map.add(2,i);
System.out.println(map.toString());
// {10 = [a,b],9 = [c],8 = [d,e,f,g],4 = [h],2 = [i]}
}
}
您可以通过以下Maven依赖关系添加Spring-Core: / p>
<依赖关系>
< groupId> org.springframework< / groupId>
< artifactId> spring-core< / artifactId>
< version> 3.1.1.RELEASE< / version>
< / dependency>
如果您需要帮助获得项目中的任何这些库,请随时与我联系。
更新1
原来没有一种方便的方法来从原始API过滤/排序。我已经包括一个简单的过滤器功能,下面应该做的伎俩。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
public class MultiValueMapExample {
public static void main(String [] args){
// 10-a,10-b,9 -c,8-d,8-e,8-f,4-g,4-h,2-i
MultiValueMap< Integer,String> map = new LinkedMultiValueMap< Integer,String>();
map.add(8,g);
map.add(4,h);
map.add(10,a);
map.add(10,b);
map.add(9,c);
map.add(8,d);
map.add(8,e);
map.add(8,f);
map.add(2,i);
System.out.println(map.toString());
// {8 = [g,d,e,f],4 = [h],10 = [a,b],9 = [c],2 = [i]}
MultiValueMap< Integer,String> filteredMap = filter(5,map);
System.out.println(filteredMap.toString());
// {10 = [a,b],9 = [c],8 = [g,d,e,f],4 = [h],2 = [i]}
}
public static MultiValueMap< Integer,String> filter(int numberOfResults,MultiValueMap< Integer,String> map){
MultiValueMap< Integer,String> result = new LinkedMultiValueMap< Integer,String>();
列表<整数> keys = new ArrayList< Integer>(map.keySet());
Collections.sort(keys,Collections.reverseOrder());
(整数键:键){
if(result.size()< = numberOfResults){
result.put(key,map.get(key)) ;
} else {
break;
}
}
返回结果;
}
}
I need to enter key-value pairs into a data structure that allows me to retrieve them in ascending order of the key--BUT their may be many keys of the same value.
Thus, if the kv-pairs were {10-a, 10-b, 9-c, 8-d, 8-e, 8-f, 4-g, 4-h, 2-i} I would need to retrieve the values in the order: a, b ,c, d, e, f, g, h, i. Are there any data structures in the JAVA API that supports this?
I tried using a TreeMap because it kept them in order which allowed me to use TreeMap.lastKey() to retrieve the highest current key, but I didn't know that it overwrote any duplicate keys that were already in the map. I need something that doesn't overwrite (similar to a HASH), but also allows me to retrieve them in a sorted order--does this exist?
Thank you!
Unfortunately you probably will not find a structure that supports multiple values of the same keys. As Dilum said, there are several implementations of "MultiMap" or "Multi-Valued Maps" that would work well.
In addition to Guava's TreeMultiMap, there's also the Spring Framework's MultiValueMap and Apache Common's MultiValueMap.
An example of the Spring implementation would be:
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
public class MultiValueMapExample {
public static void main(String[] args) {
// 10-a, 10-b, 9-c, 8-d, 8-e, 8-f, 4-g, 4-h, 2-i
MultiValueMap<Integer, String> map = new LinkedMultiValueMap<Integer, String>();
map.add(10, "a");
map.add(10, "b");
map.add(9, "c");
map.add(8, "d");
map.add(8, "e");
map.add(8, "f");
map.add(8, "g");
map.add(4, "h");
map.add(2, "i");
System.out.println(map.toString());
// {10=[a, b], 9=[c], 8=[d, e, f, g], 4=[h], 2=[i]}
}
}
You could use this by adding Spring-Core via the following Maven dependency:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
If you need help getting any of these libs in your project, feel free to comment / contact me.
Update 1
Turns out there's not a convenient way to filter / sort from the raw API's. I've included a simple filter function below that should do the trick.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
public class MultiValueMapExample {
public static void main(String[] args) {
// 10-a, 10-b, 9-c, 8-d, 8-e, 8-f, 4-g, 4-h, 2-i
MultiValueMap<Integer, String> map = new LinkedMultiValueMap<Integer, String>();
map.add(8, "g");
map.add(4, "h");
map.add(10, "a");
map.add(10, "b");
map.add(9, "c");
map.add(8, "d");
map.add(8, "e");
map.add(8, "f");
map.add(2, "i");
System.out.println(map.toString());
// {8=[g, d, e, f], 4=[h], 10=[a, b], 9=[c], 2=[i]}
MultiValueMap<Integer, String> filteredMap = filter(5, map);
System.out.println( filteredMap.toString() );
// {10=[a, b], 9=[c], 8=[g, d, e, f], 4=[h], 2=[i]}
}
public static MultiValueMap<Integer, String> filter(int numberOfResults, MultiValueMap<Integer, String> map){
MultiValueMap<Integer, String> result = new LinkedMultiValueMap<Integer, String>();
List<Integer> keys = new ArrayList<Integer>(map.keySet());
Collections.sort(keys, Collections.reverseOrder());
for(Integer key : keys){
if( result.size() <= numberOfResults ){
result.put(key, map.get(key));
}else{
break;
}
}
return result;
}
}
这篇关于Java数据结构类似于TreeMap + Hash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!