What is the equivalent of the C++ Pair<L,R> in Java?
In Java, the equivalent of the C++ Pair
class is the AbstractMap.SimpleEntry
class or the AbstractMap.SimpleImmutableEntry
class. Both classes are implementations of the Map.Entry
interface and represent a key-value pair.
Here is an example of how you can use the AbstractMap.SimpleEntry
class to create a pair in Java:
Map.Entry<String, Integer> pair = new AbstractMap.SimpleEntry<>("key", 42);
String key = pair.getKey();
Integer value = pair.getValue();
System.out.println("Key: " + key);
System.out.println("Value: " + value);
In this example, the SimpleEntry
class is used to create a pair with a String
key and an Integer
value. The getKey()
and getValue()
methods are used to retrieve the key and value of the pair.
The AbstractMap.SimpleImmutableEntry
class is similar to the AbstractMap.SimpleEntry
class, but it is immutable, which means that the key and value cannot be modified once the pair is created.
I hope this helps! Let me know if you have any questions.