1

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?

4 Answers4

1

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);
Ritt
  • 3,181
  • 3
  • 22
  • 51
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"));
janos
  • 120,954
  • 29
  • 226
  • 236
  • 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? – user5425022 Oct 08 '15 at 21:13
  • You haven't told about this requirement earlier. But fine, see my updated post. – janos Oct 08 '15 at 21:19
0

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?

Community
  • 1
  • 1
M.P. Korstanje
  • 10,426
  • 3
  • 36
  • 58
0

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");
hic1086
  • 755
  • 3
  • 10