How to store an array of pairs in Java?
Solution 1
Create your own class to represent a pair and add a constructor that takes two arguments:
public class MyPair
{
private final Double key;
private final Double value;
public MyPair(Double aKey, Double aValue)
{
key = aKey;
value = aValue;
}
public Double key() { return key; }
public Double value() { return value; }
}
See this answer for reasons why a Pair
does not exist in Java: What is the equivalent of the C++ Pair<L,R> in Java?
Solution 2
You don't want to use Entry it's an INTERFACE, not a CLASS. That interface is used by an implementations of Set when you call entrySet() on a class that implements Map. It basically lets you manipulate the implemented Map as though it were a Set.
What you would do (but can't) is this. If you try to do this you'll see a compiler error along the lines of "Cannot instantiate the type Map.Entry". That's because Map.Entry is an interface, not a class. An interface doesn't contain any real code, so there's no real constructor to run here.
Entry<Double, Double> pair = new Entry<Double, Double>();
If you look at the below docs you can clearly see at the top that it's a "Interface Map.Entry" which means it's an interface. http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Map.Entry.html
What you should do instead of trying to instantiate an interface, which is impossible, is create your own class called Pair. Something like this. Remember to change the package if you use the below code.
package org.mike.test;
public class Pair {
private double x = 0.0;
private double y = 0.0;
public Pair(double x, double y)
{
this.x = x;
this.y = y;
}
public Pair()
{
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
After writing your Pair class your code will now look like this.
package org.mike.test;
import java.util.ArrayList;
import org.mike.test.Pair; //You don't need this if the Pair class is in the same package as the class using it
public class tester {
/**
* @param args
*/
public static void main(String[] args) {
ArrayList<Pair> values = new ArrayList<Pair>();
Pair pair = new Pair();
// set pair values:
pair.setY(3.6);
pair.setX(3.6);
values.add(pair);
}
}
Solution 3
Couldn't you just use
public class MyClass<A,B> extends ArrayList{
private A first;
private B second;
public MyClass(A first, B second){
super();
this.first = first;
this.second = second;}
}
and then add some form of add method, along with a first and second accessor & mutator method? I'm sort of new to programming, but this way would seem like it might work, and be accessible to things other than just the DOUBLE, (in case down the road you want to use other types, like Integer, or even String).
Solution 4
You could use a map to solve this.
Solution 5
Is Entry
a class you defined? You instantiate it with new
.
Entry<Double, Double> pair = new Entry<Double, Double>(d1, d2);
Note I am assuming you defined a constructor that takes 2 doubles, and you have references for d1
and d2
.
I suggest you NOT use the Map.Entry
class. The semantics for that class are such that the values are a key and a value, fitting with the way Maps work.
![Benjamin Crouzier](https://i.stack.imgur.com/oZR4v.jpg?s=256&g=1)
Benjamin Crouzier
I am a former developer (rails/react/aws/postgres), and I now study AI and cognitive science full-time. Currently living in Paris. Github profile: https://github.com/pinouchon Blog: http://pinouchon.github.io/
Updated on July 05, 2022Comments
-
Benjamin Crouzier almost 2 years
I am new to Java, I want to store an array of pair of doubles. My code looks like this:
import java.util.ArrayList; import java.util.Map.Entry; List<Entry<Double, Double>> values = new ArrayList<>(); Entry<Double, Double> pair; // set pair values: // pair.setKey(0.5); // this method does not exists // pair.setValue(3.6); values.add(pair);
How can I initialize the pair variable? Is there a better structure to store my array of pair of doubles?
-
amit over 12 yearstrue if each "key" is unique.
Map
doesn't allow duplicate keys -
rompetroll over 12 yearslook at the imports: "import java.util.Map.Entry;"
-
Benjamin Crouzier over 12 yearsI am surprised there is not something that I can use in the standard java library for such a simple thing
-
Benjamin Crouzier over 12 yearsTwo pairs can have the same left number, ie: [(2, 6.3), (2, 8.4)]
-
hmjd over 12 yearsSee this answer for reasons why no such
Pair
exists: stackoverflow.com/questions/156275/… -
Luiggi Mendoza over 12 yearsIf you will have a
Double
as a key, you must look this stackoverflow.com/q/1074781/1065197 -
Benjamin Crouzier over 12 yearsI get the following error:
Cannot instantiate the type Map.Entry<Double,Double>
-
Leif almost 8 yearsNice, and give that you gave us a Pair(double, double) constructor, I'd simplify and remove the setX/Y() calls and use Pair pair = new Pair(3.6, 3.6);
-
Dmytro about 7 yearsnote that this solution increases project complexity greatly as you might have 100 different classes that are effectively pairs, but each has distinct type; requiring custom marshalling between the types, which is both error prone, looks ugly, and expensive(as you can't blit from one to another, and cache hostility). Its ok if all pairlikes implemented their own marshallers, this solves all problems except performance and different versions of pair having differently named base interface, which can be solved by not having an interface and using reflection, but now you have other problems.
-
Mohan about 4 yearsYou can't instantiate the interface