How to create a value pair within a Java Map

Asked

Viewed 727 times

5

It is possible to create a Map in Java using the concept of pair that exists in C++? I have tried to use it in the form below, however, I cannot assign values to my map.

Map<String, Entry<Entry<Integer,Integer>,String> > sub = new HashMap<>();

How can I fix this?

  • 1

    Incredible as it may seem, to this day there is no generic data structure in Java capable of storing a pair of values... >:( But second that answer in Soen it is possible to use Map.Entry for this purpose - you just instantiate one of its concrete implementations (AbstractMap.SimpleEntry or AbstractMap.SimpleImmutableEntry) instead of the interface itself. A perhaps more user friendly option (if available) is the javafx.util.Pair

  • Good idea @mgibsonbr! Mto thanks!!

1 answer

5


Java doesn’t really have one Pair proper.

You can implement your own class that handles the desired data pair. Normally this class is an interface implementation Map.Entry<K,V>. An example of implementation can be seen in that reply in the OS:

import java.util.Map;

final class MyEntry<K, V> implements Map.Entry<K, V> {
    private final K key;
    private V value;

    public MyEntry(K key, V value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public K getKey() {
        return key;
    }

    @Override
    public V getValue() {
        return value;
    }

    @Override
    public V setValue(V value) {
        V old = this.value;
        this.value = value;
        return old;
    }
}

I put in the Github for future reference.

Of course nothing prevents you from creating a class that implements the pair as you wish without implementing this interface.

Or you can use the AbstractMap.SimpleEntry<K,V>. Could use it like this:

Map<String, Map.Entry<Map.Entry<Integer, Integer>, String>> sub = new HashMap<>();

You would have to create each object of these, go nesting to put on the map:

 Map.Entry<Integer, Integer> par1 = new AbstractMap.SimpleEntry<>(0, 1);
 Map.Entry<Map.Entry<Integer, Integer>, String> par2 = new AbstractMap.SimpleEntry<>(par1, "txt");
 sub.put("chave", par2);
  • Now I understand, that way you approached @bigown was very clear! This way it worked in my code. Mto thanks!

  • I don’t know how much you know about programming but it is good to make it clear that I have created intermediate variables just to facilitate the visualization, you can obviously create the pair object and use it directly on the map, as occurs in (almost) all language.

  • I’m starting in Java not long ago, I’m still adapting the language. This part of data structure is still crawling, so I am full of doubts. This section that you showed, compiled in my program. It may seem silly my question now, but for me to individually access the value of my second String Map sub, would it be through an iterator? How does it work? sub.values(). iterator();

  • 1

    No question is silly, there are only questions asked wrong, which is not the case with yours. Ask what you need here. It will take some time for you to adapt to the language. Usually not using Java iterators as in C++, they are even used but are more automatic in most situations. This is already another question but the basic access I think is sub.get("chave").getValue(). I’m not sure if this is it because I’ve never used it. I don’t know if this way you’re trying to work out works, but I doubt it’s the simplest. Or I don’t understand what you’re after :)

  • Apache makes available in the package apache-lang a utility for this, the class Pair<L,R>. Very useful and lazy when you need to return only 2 values from some database query framework, such as Mybatis. Out of curiosity, they also provide the triple. There are other libraries I’ve seen that provide up to 5-upla.

  • C# has all kinds of tuples by reference and by value, so you can’t compare the two languages :D

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.