0

I have various ArrayLists nested in one HashMap.

How can I check if my array list nested in the hashmap exists?

public static HashMap<Integer, ArrayList<Integer>> transactionMap = new HashMap<Integer, ArrayList<Integer>>();

public static void initializeLists() {
    transactionMap.put(7, new ArrayList<Integer> (Arrays.asList(1050, 1125)));
    transactionMap.put(305, new ArrayList<Integer>(Arrays.asList(1125)));
    transactionMap.put(1125, new ArrayList<Integer>(Arrays.asList(250, 252, 251)));
    transactionMap.put(1050, new ArrayList<Integer>(Arrays.asList(251, 252)));
    transactionMap.put(1124, new ArrayList<Integer>(Arrays.asList(250)));
    transactionMap.put(1049, new ArrayList<Integer>(Arrays.asList(251, 252)));
}

This post here describes how to check directly in an Array List using #contains(Object), which does not fit for HashMap.

This one is close but it would mean I would have to give a name to all my lists (and there could be many). I would like to avoid having to nest a map inside a map if possible unless needed. Does anyone have any points to the best solution?

Community
  • 1
  • 1
Aurax22
  • 111
  • 3
  • 13
  • Why don't you simply use a ``Map`` instead? And btw. your example isn't valid java code. – Binkan Salaryman Nov 24 '15 at 15:56
  • If you are able to use Guava, you can use a Multimap, specifically an [ArrayListMultimap](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/ArrayListMultimap.html), and then just use [`multimap.containsValue`](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/ArrayListMultimap.html#containsValue(java.lang.Object)). Otherwise, you'd need to iterate through all values, calling `contains` on each until you find one. – Andy Turner Nov 24 '15 at 15:57
  • use containsValue method – Tommy Nov 24 '15 at 15:58
  • These lists can be simplified as new `Arrays. asList(...)` – lance-java Nov 24 '15 at 16:01
  • 1
    @LanceJava That's not the same. If you don't copy the list into a new array list, you can't make structural modifications to the list. – Andy Turner Nov 24 '15 at 16:02
  • How do you calculate the `key` value in your `transactionMap`, is it transaction ID ? – MaxZoom Nov 24 '15 at 16:03
  • @AndyTurner you are correct. I'm assuming the map is not mutated after initialization. – lance-java Nov 24 '15 at 16:04
  • @MaxZoom It is not a calculated, it is just a transaction ID. – Aurax22 Nov 24 '15 at 16:06
  • @Tommy This returns a collection of values, I do not believe it checks for a particular value (I tried...) – Aurax22 Nov 24 '15 at 16:07

1 Answers1

2

If your question is "how do I check if a specific list is a value in the map" then you can do it as follows:

if (transactionMap.containsValue(Arrays.asList(1, 2, 3)) {
    ...
}

If your question is "how do I check if a specific integer appears somewhere in a list as a value in the map" then you can do the following (using Java 8):

if (transactionMap.values().stream().anyMatch(l -> l.contains(17)) {
    ...
}
sprinter
  • 27,148
  • 6
  • 47
  • 78
  • Each key in my map has a different ArrayList :/ I need to be able to deep dive into the arraylist of the key in the Map. – Aurax22 Nov 24 '15 at 16:30
  • @Aurax22 No actually your key is a simple integer - not a list. The values are lists. – sprinter Nov 24 '15 at 16:36
  • I'm guessing you mean 'deep dive into the arraylist of the values in the map'. I've added a solution for that to my answer. – sprinter Nov 24 '15 at 16:42
  • Using your last tip, it works perfectly, thanks a lot. I'll try and be more specific on my questions next time also! I am a beginner but this seems like a "fancy" way to do it, I am surprised there is no simple way (unless the simple way was to use a nested map). – Aurax22 Nov 24 '15 at 16:45