Question

I should store the following data, where the first integer is unique:

<118,15>
<429,15>
<625,30>

Is HashSet<Integer,Integer> the good option to store this?

Était-ce utile?

La solution

Set is for uniqueness, although there is nothing for key -> value thing in Set collections. So,

HashSet<Integer,Integer>

does not exists at all.

Probably Map is what you're looking for, as soon as you have a unique Integer to use as key.

Example:

Map<Integer, Integer> map = new HashMap<>();
map.put(118, 15); //the key is 118, value is 15
map.put(429, 15);
map.put(625, 30);
map.get(118);     //get the value for the key 118, so it returns 15

It you can't guarantee the uniqueness of any of Integers, you probably can use:

List<Integer[]> ints = new ArrayList<>();
ints.add(new Integer[] {185, 15});
ints.add(new Integer[] {429, 15});
ints.add(new Integer[] {625, 15});

EDIT

As you explained through review, the first element is unique, so you're probably ok with the Map interface, as it is the main purpose of it.

Autres conseils

HashSet is applicable when there's structure like key -> value, where keys are unique.

If you want just to store pairs of integers, you have to create your own data type, something like:

public class MyTuple /* here should be adequate name */ {
    int first, second; // there too
    public int getFirst() {
        return first;
    }
    public int getSecond() {
        return second;
    }
    public MyTuple(int first, int second) {
        this.first = first;
        this.second = second;
    }
}

If you're a bit lazy, you can use Map.Entry for this purpose, but it's not correct in Java world, you should have one class for one purpose and it should refrect it's designation.

Some libraries have their own implementation of Pair class, for example JavaFX, TestNG have some.

And all of these should be wrapped in array or List, something like:

List<MyTuple> tuples = new ArrayList<>();

It depends on usage. Assuming the type is int [] (array of size 2):

-If you just want to store, use one of the List implementations (your choice will basically be between LinkedList and Vector depending on the insertion pattern)

-If you need to iterate in an orderly fasion, a custom TreeSet would work:

TreeSet<int []> set = new TreeSet<int[]>(new Comparator<int []>()
{
    public int compare(int[] o1, int[] o2) {return o1[0] != o2[0] ? o1[0]-o2[0] : o1[1]-o2[1];}
});

-If there are many lookups, use a HashSet

Apache Commons Lang, has an ImmutablePair, that you can put in a Set if you want. Whether Set is useful in this case, depends on what you want to do with the data. Sets are Iterable, but cannot be read (can only be checked for), also worth mentioning is that by default, in a Set <100, 13> and <100, 14> are not the same item (ie, if you add both, you will have 2 items with "key" 100). Furthermore, if you care about ordering, I would probably use a TreeSet/TreeMap instead.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top