1

I am actually , looking whether exist any structure or interface in java

1 -> A
1 -> B

2 -> A
2 -> B
2 -> C

which store the details as

 1 -> {A,B}  and 2 -> {A,B,C}

Like , if I use map , it might be return as

 1 -> B and 2 -> C

I am looking for something that can return as

 1 -> {A,B}  and 2 -> {A,B,C}

Thanks !

3
  • 1
    Maybe you could try and use a map of arrays...? Commented Sep 29, 2014 at 22:23
  • You could create a Map<Integer, Character[]> or a Map<Integer, List<Character>> or use String instead of Character, depending on your real use case. Commented Sep 29, 2014 at 22:24
  • 1
    You haven't really provided enough information about the properties of the data to answer the question. For example, are the things you've used 1 and 2 as examples for actually going to be consecutive integers? Commented Sep 29, 2014 at 22:28

3 Answers 3

1

Like map of Arrays? You can use map to store any kind of value, you can use map of lists for example:

Map <Integer, List<YourObject> map = new HashMap<Integer, List<YourObject>;

it should work just fine in your case.

Sign up to request clarification or add additional context in comments.

Comments

1

This is called a multi-map. In Java, a multi-map is easily implemented on top of a regular map, by using a collection as the value type.

Alternatively, you could use a pre-cooked multi-map implementation, say MultiMap from Apache Commons.

Comments

0

You can use Map<Integer, List<String>> (an map whose keys are integers and values are lists of strings)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.