Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract ArrayList from HashMap and loop through it in Java?

I have set up a HashMap like so:

Map<String, ArrayList<String>> theAccused = new HashMap<String, ArrayList<String>>();

... and I populate this by storing for every name (key), a list of names (value). So:

ArrayList<String> saAccused = new ArrayList<String>();
// populate 'saAccused' ArrayList
...
// done populating
theAccused.put(sAccuser, saAccused);

So now, I want to look through all of the entries in the HashMap and see if (for each 'sAccuser'), the list 'saAccused' contains a certain name. This is my failed attempt so far:

Set<String> setAccusers = theAccused.keySet();
Iterator<String> iterAccusers = setAccusers.iterator();
iterAccusers.next();
ArrayList<String> saTheAccused;

// check if 'sAccuser' has been accused by anyone before
for (int i = 0; i < theAccused.size(); i++) {
    saTheAccused = theAccused.get(iterAccusers);

    if (saTheAccused.contains(sAccuser)) {

    }
    iterAccusers.next();
}

... however I'm not sure how the Set and Iterator classes work :/ The problem is that I don't have the "values"... the names... the 'sAccuser's... for the HashMap available.

In a nutshell, I want to iterate through the HashMap and check if a specific name is stored in any of the lists. So how can I do this? Let me know if you need me to go into further detail or clear up any confusion.

Thanks.

like image 461
Hristo Avatar asked Aug 09 '10 21:08

Hristo


People also ask

Can we iterate ArrayList using for loop in Java?

The enhanced for loop (sometimes called a "for each" loop) can be used with any class that implements the Iterable interface, such as ArrayList .

Can you loop through a HashMap in Java?

In Java HashMap, we can iterate through its keys, values, and key/value mappings.

Can HashMap contains ArrayList?

Since every element in the hashmap contains a key/value pair, it is not possible to store this key/value pair in a single ArrayList.


2 Answers

In a nutshell, I want to iterate through the HashMap and check if a specific name is stored in any of the lists. So how can I do this?

There's two ways of iterating through the map that might be of interest here. Firstly, you can iterate through all of the mappings (i.e. pairs of key-value relations) using the entrySet() method, which will let you know what the key is for each arraylist. Alternatively, if you don't need the key, you can simply get all of the lists in turn via the values() method. Using the first option might look something like this:

for (Map.Entry<String, ArrayList<String>> entry : theAccused.entrySet())
{
   String sListName = entry.getKey();
   ArrayList<String> saAccused = entry.getValue();
   if (saAccused.contains(sAccuser))
   {
      // Fire your logic for when you find a match, which can
      // depend on the list's key (name) as well
   }
}

To answer the broader questions - the Set interface simply represents an (unordered) collection of non-duplicated values. As you can see by the linked Javadoc, there are methods available that you might expect for such an unordered collection. An Iterator is an object that traverses some data structure presenting each element in turn. Typical usage of an iterator would look something like the following:

Iterator<?> it = ...; // get the iterator somehow; often by calling iterator() on a Collection
while (it.hasNext())
{
   Object obj = it.next();
   // Do something with the obj
}

that is, check whether the iterator is nonexhausted (has more elements) then call the next() method to get that element. However, since the above pattern is so common, it can be elided with Java 5's foreach loop, sparing you from dealing with the iterator itself, as I took advantage of in my first example.

like image 84
Andrzej Doyle Avatar answered Sep 28 '22 02:09

Andrzej Doyle


Something like this?

for (List<String> list : theAccused.values()) {
    if (list.contains("somename")) {
        // found somename
    }
}
like image 22
quantumSoup Avatar answered Sep 28 '22 02:09

quantumSoup