Giannis
Giannis

Reputation: 5526

Data structure to hold paired values

This is what I am trying to do :

Having a list of channel names where each channel name has several members. Both are Strings.

What I am doing now is having a 2d ArrayList.First value on each ArrayList is the channel name and next are its members. Is there a better way to do this (without creating classes) ?

Can a Map have many values for one key ? So that I have a channel as Key and members as Values to that Key ?

Thanks !

Upvotes: 1

Views: 991

Answers (4)

GETah
GETah

Reputation: 21449

I suggest Map<String, List<T>> where T is the channel member type.

Upvotes: 0

Mairbek Khadikov
Mairbek Khadikov

Reputation: 8109

Guava's Multimap can be helpful for you.

Upvotes: 3

Arnon Rotem-Gal-Oz
Arnon Rotem-Gal-Oz

Reputation: 25929

You can have a Map<String,ArrayList>

Upvotes: 2

fge
fge

Reputation: 121820

Yes, a Java Map can have any types of keys or values.

What you probably want here is a Map<String, Set<String>> (since I assume a channel cannot have the same member twice, right?).

And any really means any: you may even have Maps as keys to Maps.

Upvotes: 3

Related Questions