Nick Robertson
Nick Robertson

Reputation: 1047

Create multi-dimensinal ArrayList in java

I want to create an arrayList as following.

id->1512   associated with the values -> 12,45,78
id->1578   associated with the values -> 456,78,87,96

What I have to do? Should I create a 2-d arrayList or can I do that with single dimension arraylist?

Upvotes: 0

Views: 65

Answers (3)

jlordo
jlordo

Reputation: 37813

Use the Guava Library, and you can do this for your associations:

Multimap<Integer, Integer> map = HashMultimap.create();
map.putAll(1512, Arrays.asList(12, 45, 78));
map.putAll(1578, Arrays.asList(456, 78, 87, 96));

Here's an example how you can get the values:

int key = 1512;
for (Integer value : map.get(key)) {
    System.out.println("Associated " + key + " -> " + value);
}

Here's a link to Guava's JavaDoc

Upvotes: 2

Shelef
Shelef

Reputation: 668

You dont need ArrayList. read abut Map

Upvotes: 0

PermGenError
PermGenError

Reputation: 46398

You are looking for something like this:

Map<Integer, List<Integer>>

Upvotes: 5

Related Questions