17

How do I store a set of paired numbers in java? Do I use lists or arrays or maybe something else?

eg. [ (1,1) , (2,1) , (3,5)]

saviok
  • 497
  • 1
  • 6
  • 14
  • 1
    You can use all the above, but there is no way of guessing from what you have said. – Peter Lawrey Apr 19 '12 at 18:31
  • @Erwald: use `[desc](link)` to post link in comments – amit Apr 19 '12 at 18:32
  • @Erwald Its one solution but it might not be the best for what the OP is doing. – Peter Lawrey Apr 19 '12 at 18:32
  • [Related](http://stackoverflow.com/questions/9568403/how-to-store-an-array-of-pairs-in-java) – Rob Hruska Apr 19 '12 at 18:37
  • [Related](http://stackoverflow.com/questions/521171/a-java-collection-of-value-pairs-tuples) – Rob Hruska Apr 19 '12 at 18:37
  • Well the best solution will depend on how the numbers are being accessed. Are duplicates pairs allowed? If not, is the first value in a pair always unique? Will you be adding and removing, essentially? Do you want to have them sorted in some way? Are negative integers allowed? Using a Pair class as some have suggested is an option but I need the answers to those questions to give a better response. – Jesus is Lord Apr 19 '12 at 18:38
  • Object Oriented Programming - Rings a bell? – Kalpak Gadre Apr 19 '12 at 18:38
  • [Related](http://stackoverflow.com/questions/2670982/using-tuples-in-java) – Rob Hruska Apr 19 '12 at 18:39

6 Answers6

15

There are a few options:

Write a custom IntPair class

class IntPair {
  // Ideally, name the class after whatever you're actually using 
  // the int pairs *for.*
  final int x;
  final int y;
  IntPair(int x, int y) {this.x=x;this.y=y;}
  // depending on your use case, equals? hashCode?  More methods?
}

and then create an IntPair[] or a List<IntPair>.

Alternately, create a two-dimensional array new int[n][2], and treat the rows as pairs.

Java doesn't have a built-in Pair class for a few reasons, but the most noticeable is that it's easy enough to write a class that has the same function, but has much more enlightening, helpful names for the class, its fields, and its methods.

If we knew more about what you're actually using this for, we might be able to provide more detailed suggestions -- for all we know, a Map could be appropriate here.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
1

If you're using JavaFX, you can use the class Pair.

import javafx.util.Pair;

int x = 23;
int y = 98;
        
Pair<Integer, Integer> pair1 = new Pair<>(6, 7);
Pair <Integer, Integer> pair2 = new Pair<>(x, y);
fuggerjaki61
  • 822
  • 1
  • 11
  • 24
1

Way 1 : Using javafx.util.Pair class

Pair<Integer> myPair1 = new Pair<Integer>(10,20);
Pair<Integer> myPair2 = new Pair<Integer>(30,40);
HashSet<Pair<Integer>> set = new HashSet<>(); // Java 8 and above
set.add(myPair1);
set.add(myPair2);

Way 2: Using int[] of size 2

int[] myPair1 = new int[] {10,20}; // myPair1[0]=10 , myPair[1] = 20
int[] myPair2 = new int[] {30,40};
HashSet<int[]> set = new HashSet<>(); // Java 8 and above

Way 3 : Converting pair into single number

int myPair1 = 10 * 1000 + 20; 
// int first = myPair1 / 1000; second = myPair2 % 1000;
int myPair2 = 30 * 1000 + 40;
HashSet<Integer> set = new HashSet<>();
set.add(myPair1);
set.add(myPair2);

Way 4 : Using ArrayList instead of int[] in way 2

Way 5 : Custom class that uses HashSet and Pair internally

Jay Teli
  • 530
  • 1
  • 11
  • 19
0
class Pair<T> {
    T p1, p2;
    Pair(T p1, T p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

Pair<Integer> pair = new Pair<Integer>(1,2);

int i1 = pair.p1;
int i2 = pair.p2;

You can also put in getters, setters, equals, hashcode, etc.

Skip Head
  • 7,580
  • 1
  • 30
  • 34
  • 2
    I would made `p1`,`p2` `private final` and also override `equals()` and `hashCode()` – amit Apr 19 '12 at 18:34
0

If you can live with low level structures and desperately need compact form of "literal" form of "set of pairs" -- this happens to me in unit test, when I need a set of fixtures -- you can simply use an array of arrays:

int[][] squares = {
    { 1, 1 },
    { 2, 4 },
    { 3, 9 }
};

But keep in mind that there is no semantic to such a type -- it all depends on proper use, compiler won't give you a warning if you type squares[0][1] when you really wanted squares[1][0].

pwes
  • 2,040
  • 21
  • 30
0

If you're needing to avoid duplicates then a HashSet would be a good choice but it not then an ArrayList would work.

Class IntPair(){
  int i;
  int j;
}
HashSet<IntPair> set = new HashSet<IntPair>();

or

ArrayList<IntPair> list = new ArrayList<IntPair>();
ChadNC
  • 2,528
  • 4
  • 25
  • 39
  • If you want to avoid duplicates you will need to also override equals and hashCode. http://stackoverflow.com/a/7520464/3215004 – gmatht Sep 16 '15 at 06:37