From the comment:
private ArrayList<HashMap> alleAntwoorden;
This is the problem. You're using a raw type map, but you're trying to assign a single entry to the variable Map.Entry<String, Boolean>. This cannot work, because your current map is of type HashMap<Object, Object>. Change the variable alleAntwoorden to:
private List<Map<String, Boolean>> alleAntwoorden;
Mind, that I've also changed the types to their Interface type: Should you always Code To Interfaces In Java.
Answer from Tom on Stack OverflowFrom the comment:
private ArrayList<HashMap> alleAntwoorden;
This is the problem. You're using a raw type map, but you're trying to assign a single entry to the variable Map.Entry<String, Boolean>. This cannot work, because your current map is of type HashMap<Object, Object>. Change the variable alleAntwoorden to:
private List<Map<String, Boolean>> alleAntwoorden;
Mind, that I've also changed the types to their Interface type: Should you always Code To Interfaces In Java.
On the following interfaces:
Map<String, Boolean> map1 = new HashMap<>();
Map<String, Boolean> map2 = new HashMap<>();
List<Map<String, Boolean>> list = new ArrayList<>();
We may iterate with foreach loops:
for (Map<String, Boolean> entry : list) {
for (String key : entry.keySet()) {
Boolean value = entry.get(key);
System.out.println("key = " + key);
System.out.println("value = " + value);
}
}
java - How to iterate HashMap<String, ArrayList<Car>> - Stack Overflow
java - How to iterate Hashmap with containing arraylist - Stack Overflow
java - How to iterate Arraylist<HashMap<String,String>>? - Stack Overflow
Loop through an ArrayList of HashMaps Java - Stack Overflow
Try adding in a
System.out.println(cars.get("bmw"));
to check and see what exactly is in it (in the Map, and the ArrayList of cars).
Iterator<String> itr = carList.iterator();
while (itr.hasNext()) {
String element = itr.next();
System.out.print(element + " ");
}
Inoder to do this carList should not be a null value.
To add values to ArrayList you can use
carList.add("Some value");
for (Map.Entry<String, ArrayList<String>> entry : map.entrySet()) {
String key = entry.getKey();
ArrayList<String> value = entry.getValue();
for(String aString : value){
System.out.println("key : " + key + " value : " + aString);
}
}
You can do like this:
for (Integer i : map.keySet())
for (String s : map.get(i))
System.out.println("key :" + i + " value: " + s);
Simplest is to iterate over all the HashMaps in the ArrayList and then iterate over all the keys in the Map:
TextView view = (TextView) view.findViewById(R.id.view);
for (HashMap<String, String> map : data)
for (Entry<String, String> entry : map.entrySet())
view.append(entry.getKey() + " => " + entry.getValue());
for(HashMap<String, String> map : data){
... deal with map...
}
Here is an example to iterate. I created dummy data to test the code
private void ArrayListAndHashMap()
{
ArrayList<HashMap<String, Integer>> myList = new ArrayList<HashMap<String, Integer>>();
HashMap<String, Integer> data1 = new HashMap<String, Integer>();
data1.put("0",new Integer(1));
data1.put("1",new Integer(2));
data1.put("2",new Integer(3));
data1.put("3",new Integer(4));
HashMap<String, Integer> data2 = new HashMap<String, Integer>();
data1.put("10",new Integer(10));
data1.put("11",new Integer(20));
data1.put("12",new Integer(30));
data1.put("13",new Integer(40));
myList.add(data1);
myList.add(data2);
for (int a =0; a<myList.size();a++)
{
HashMap<String, Integer> tmpData = (HashMap<String, Integer>) myList.get(a);
Set<String> key = tmpData.keySet();
Iterator it = key.iterator();
while (it.hasNext()) {
String hmKey = (String)it.next();
Integer hmData = (Integer) tmpData.get(hmKey);
System.out.println("Key: "+hmKey +" & Data: "+hmData);
it.remove(); // avoids a ConcurrentModificationException
}
}
}
Output is
Key: 3 & Data: 4
Key: 2 & Data: 3
Key: 10 & Data: 10
Key: 1 & Data: 2
Key: 0 & Data: 1
Key: 13 & Data: 40
Key: 11 & Data: 20
Key: 12 & Data: 30
the [] operator can only be used on arrays. List has a get(int index) method to get the element at a given index:
for (String key : myList.get(a).keySet()) {
...
}
Java classes are documented: http://docs.oracle.com/javase/6/docs/api/
If this is something that you've written, then I'd like to suggest an alternate data structure. Instead of Hashmap<String, ArrayList<String>>, use ArrayList<HashMap<String, String>>
so that you have
{Name:Sam, Age:20, City:London},
{Name:Peter, Age:17, City:London},
{Name:Andrea, Age:24, City:Munich},
{Name:Sandra, Age:40, City:London}
If the HashMap of Lists is not something that you've written, and you still need help to figure out how to iterate that, please show us what you have tried.
Though I absolutely agree with @GreyBeardedGeek in his answer, and you should probably change your data structure. I did however have a look at the code needed to change Map<String, List<String>> into List<Map<String, String>>
public static <S, T> List<Map<S, T>> unravel(Map<S, List<T>> org) {
List<Map<S, T>> out = new LinkedList<>();
for (Entry<S, List<T>> entry : org.entrySet()) {
if (entry.getValue() == null || entry.getValue().isEmpty()) {
continue;
}
while (out.size() < entry.getValue().size()) {
out.add(new HashMap<S, T>());
}
for (int i = 0, size = entry.getValue().size(); i < size; i++) {
T value = entry.getValue().get(i);
if (value == null) {
continue;
}
out.get(i).put(entry.getKey(), value);
}
}
return out;
}
It doesn't matter whether you use a for loop, forEach or the Stream API. In all cases, you are iterating over a Map to compare each key against a certain value, which is perverting the concept of maps, to associate the key with a value and provide (usually far better that linear) lookup methods.
Further, you should use a Map<String, List<String>> instead, not referring to an implementation type like ArrayList, and not letting it be null in the first place, instead of having it to check for null later-on.
If you follow theses advice, your code becomes
Map<String, List<String>> xhashMap;
// always initialize the map to a non-null reference
xhashMap.getOrDefault("ax", Collections.emptyList())
.forEach(v -> method1(v, AA.class));
xhashMap.getOrDefault("bx", Collections.emptyList())
.forEach(v -> method1(v, BB.class));
If the map is, as the variable name suggests, a hash map, the two lookups will have O(1) time complexity, but even a TreeMap with O(log(n)) complexity will be better than iterating over the map and compare all keys.
As long as the action consists of a sole method invocation with different parameters, there is not much gain in trying to re-use common code, as the sharing code would be much more complicated.
Yes, we can't write it with Stream API, but it's not much better.
Since you are performing side effects and not collecting results, Stream would essentially be:
xhashMap.entrySet()
.stream()
.forEach(e -> ...);
and unfortunately, contain same logic inside the forEach.
Actually, you can even skip Stream creation at this point because you can perform forEach without creating a Stream:
xhashMap.entrySet()
.forEach(e -> ...);
Really stumped on this one. I have a HashMap object and the values are a list. I have no idea how to access the individual items in the list values.
Something like this:
public HashMap<String, ArrayList> getData()
{
HashMap<String, ArrayList> testData = new HashMap<String, ArrayList>();
ArrayList<String> stringList = new ArrayList<String>();
ArrayList<Integer> integerList = new ArrayList<Integer>();
int testInt1 = 8;
int testInt2 = 12;
String testString1 = "Hello";
String testString2 = "World";
testData.put("integers", integerList);
testData.put("strings", stringList);
testData.get("integers").add(testInt1);
testData.get("integers").add(testInt2);
testData.get("strings").add(testString1);
testData.get("strings").add(testString2);
return testData;
}The HashMap is being returned back to me and I want to access each individual item in each list.
I have a hashmap where each set's key is an Integer and the value is a custom inner class called node.
My for loop currently looks like this (hm is the variable that holds the hashmap):
for(int x=0; x<hm.entrySet().size(); x++){
HashMap.Entry<Integer, node> currSet = hm.entrySet();
}I know I need to do something to the " hm.entrySet() " inside the for-loop in order to access the set which is at index x. How do I do that?
Ideally I would have liked " hm.entrySet().get(x) " (the same syntax as accessing an arraylist) but that doesn't work.
What is the equivalent of " get(x) " in a situation like this?