找到你要的答案

Q:Data structure to hold pairs of values with key

Q:数据结构保持对值与键

Does there exist a data structure that can hold pairs of values, like this:

{ "1", {{ "1", "2" }, { "3", "4" }, { "5", "6" }, ... }},
{ "2", {{ "1", "2" }, { "3", "4" }, { "5", "6" }, ... }},
...

As far as I know, HashMaps don't have this capability?

是否存在一个数据结构,可以容纳对值,像这样:

{ "1", {{ "1", "2" }, { "3", "4" }, { "5", "6" }, ... }},
{ "2", {{ "1", "2" }, { "3", "4" }, { "5", "6" }, ... }},
...

据我所知,Hash地图s不具备这种能力吗?

answer1: 回答1:

This seems to fit your requirement:

Map<String, Set<Map.Entry<String, String>>> map = new HashMap<>();

Set<Map.Entry<String, String>> value1 = new HashSet<>();
value1.add(new AbstractMap.SimpleEntry<>("1", "2"));
value1.add(new AbstractMap.SimpleEntry<>("3", "4"));
value1.add(new AbstractMap.SimpleEntry<>("5", "6"));
map.put("1", value1);

Set<Map.Entry<String, String>> value2 = new HashSet<>();
value2.add(new AbstractMap.SimpleEntry<>("1", "2"));
value2.add(new AbstractMap.SimpleEntry<>("3", "4"));
value2.add(new AbstractMap.SimpleEntry<>("5", "6"));
map.put("2", value2);

System.out.println(map);

Output:

{1=[1=2, 5=6, 3=4], 2=[1=2, 5=6, 3=4]}

Or this:

Map<String, Map<String, String>> map = new HashMap<>();

Map<String, String> value1 = new HashMap<>();
value1.put("1", "2");
value1.put("3", "4");
value1.put("5", "6");
map.put("1", value1);

Map<String, String> value2 = new HashMap<>();
value2.put("1", "2");
value2.put("3", "4");
value2.put("5", "6");
map.put("2", value2);

System.out.println(map);

Output:

{1={1=2, 3=4, 5=6}, 2={1=2, 3=4, 5=6}}

Your follow-up question:

How would I get the pair given the key in a pair. For example, if I had the key 5, how could I get the pair 5=6 from the first element?

Here you go:

System.out.println(map.get("1").get("5"));

这似乎符合你的要求:

地图<String, Set<地图.Entry<String, String>>> map = new Hash地图<>();

Set<地图.Entry<String, String>> value1 = new HashSet<>();
value1.add(new Abstract地图.SimpleEntry<>("1", "2"));
value1.add(new Abstract地图.SimpleEntry<>("3", "4"));
value1.add(new Abstract地图.SimpleEntry<>("5", "6"));
map.put("1", value1);

Set<地图.Entry<String, String>> value2 = new HashSet<>();
value2.add(new Abstract地图.SimpleEntry<>("1", "2"));
value2.add(new Abstract地图.SimpleEntry<>("3", "4"));
value2.add(new Abstract地图.SimpleEntry<>("5", "6"));
map.put("2", value2);

System.out.println(map);

输出:

{1=[1=2, 5=6, 3=4], 2=[1=2, 5=6, 3=4]}

或这:

地图<String, 地图<String, String>> map = new Hash地图<>();

地图<String, String> value1 = new Hash地图<>();
value1.put("1", "2");
value1.put("3", "4");
value1.put("5", "6");
map.put("1", value1);

地图<String, String> value2 = new Hash地图<>();
value2.put("1", "2");
value2.put("3", "4");
value2.put("5", "6");
map.put("2", value2);

System.out.println(map);

输出:

{1={1=2, 3=4, 5=6}, 2={1=2, 3=4, 5=6}}

你的后续问题:

我怎么才能得到一对钥匙。例如,如果我有钥匙5,我怎么能得到对5 = 6从第一个元素?

干得好:

System.out.println(map.get("1").get("5"));
answer2: 回答2:

As pointed out above in the comments, what you need is a HashMap of key as String and value as List

Try something like this

    HashMap<String,List<String>> hm = new HashMap<String,List<String>>();

    List<String> l = new ArrayList<String>();
    l.add("a");
    l.add("b");
    l.add("c");

    hm.put("a",l);

As pointed out above in the comments, what you need is a Hash地图 of key as String and value as List

尝试这样的东西

    Hash地图<String,List<String>> hm = new Hash地图<String,List<String>>();

    List<String> l = new ArrayList<String>();
    l.add("a");
    l.add("b");
    l.add("c");

    hm.put("a",l);
answer3: 回答3:

No. Java doesn't have Tuple classes nor does any library provide them. The general reasoning is that it obfuscates what the pair of elements actually represents.

https://code.google.com/p/guava-libraries/wiki/IdeaGraveyard
Tuples for n >= 2

Tuple types are awful obfuscators. Tuples obfuscate what the fields actually mean (getFirst and getSecond are almost completely meaningless), and anything known about the field values. Tuples obfuscate method signatures: ListMultimap> is much less readable than ListMultimap.

Also: What is the equivalent of the C++ Pair<L,R> in Java?

Now you can abuse Map.SimpleEntry but that won't make it clear what your code is doing. So ask yourselves what the pair of numbers means and implement that as a class. Are { "1", "2" } coordinates? Is it a point? Is it a relation? What is it?

不,java没有元组类也没有任何图书馆提供。一般的推理是,混淆什么元素对实际上代表。

https://code.google.com/p/guava-libraries/wiki/IdeaGraveyard
Tuples f或 n >= 2

Tuple types are awful obfuscat或s. Tuples obfuscate what the fields actually mean (getFirst and getSecond are almost completely meaningless), and anything known about the field values. Tuples obfuscate method signatures: ListMultimap> is much less readable than ListMultimap.

另外:对C++对& lt等效是什么;L、R & gt;在java?

Now you can abuse 地图.SimpleEntry but that won't make it clear what your code is doing. So ask yourselves what the pair of numbers means and implement that as a class. Are { "1", "2" } co或dinates? Is it a point? Is it a relation? What is it?

answer4: 回答4:

You can use the library javatuples --> javatuples you can create a pair using tuples in several of which these 2 ways:

Pair<String,String> pair=new Pair<String,String>("0","1");

or

   Pair<String,String> pair=Pair.with("0","1");

You can use the library javatuples --> javatuples you can create a pair using tuples in several of which these 2 ways:

Pair<String,String> pair=new Pair<String,String>("0","1");

   Pair<String,String> pair=Pair.with("0","1");
java