1

This below code checks id exist in ArrayList or not & then add it to map.

List<Map<Object, Object>> myListOfMaps = new ArrayList<Map<Object, Object>>();
ArrayList<String> al1 = new ArrayList<String>();
al1.add("92");
al1.add("94");  
al1.add("91");  
al1.add("98");
Map<Object,Object> map = new HashMap<Object,Object>();
ArrayList<String> result = new ArrayList<String>();               
String id = "95,98,94";
for(int i=0;i<al1.size();i++)
{
    if(id.contains(al1.get(i)))
    {
        result.add("true");
        map.put("Access", true);                
    }            
    else 
    {
        result.add("false");
        map.put("Access", false);               
    }
    myListOfMaps.add(map);               

}
  for (int i = 0 ; i < myListOfMaps.size() ; i++) {
    Map<Object, Object> myMap = myListOfMaps.get(i);          
    for (Entry<Object, Object> entrySet : myMap.entrySet()) {                                
        System.out.println("Key = " + entrySet.getKey() + " , Value = " + entrySet.getValue());
    }
}          
System.out.println(result);

output

Key = Access , Value = true
Key = Access , Value = true
Key = Access , Value = true
Key = Access , Value = true
[false, true, false, true]

arraylist result is fine but mapvalues returning true only.Please help me how to add k,v to map based on condition so that mapresult looks like this [{Access=false},{Access=true},{Access=false},{Access=true}].

Wictor Chaves
  • 957
  • 1
  • 13
  • 21
user1909711
  • 39
  • 1
  • 3
  • See the second item on the top answer: https://stackoverflow.com/questions/19843506 – Jorn Vernee Nov 20 '17 at 14:11
  • you are just putting one key and is overwriting the same key with different value. 98 is last one where you traverse and its in the list, hence you insert in map with key as "Access" and value as true and hence you get true everytime – SMA Nov 20 '17 at 14:12

3 Answers3

1

A hashmap is a key-value data structure. The way you store items is based on the key.

This being said, if you add a key Access to the map with the value false and print the map it will show : Access: false.

If you add again Access with the value true is not going to add a new key, is going to replace the previous value so it will print Access: true

So instead of adding the key Access to the map, add the id. map.put(al1.get(i), true);

Robert D. Mogos
  • 900
  • 7
  • 14
  • While the explanation for the "why" is correct (though not complete; why is it printing the same `entrySet` 4 times?), the solution does not meet the output requirements of OP. – Sync Nov 21 '17 at 08:09
0

You ahve to declare a new Map every time in your loop

//...
al1.add("98");
 //Map<Object,Object> map = new HashMap<Object,Object>();
ArrayList<String> result = new ArrayList<String>();               
String id = "95,98,94";
for(int i=0;i<al1.size();i++)
{
    Map<Object,Object> map = new HashMap<Object,Object>();
    if(id.contains(al1.get(i)))
    {
        result.add("true");
        map.put("Access", true);                
    }            
    else 
    {
        result.add("false");
        map.put("Access", false);               
    }
    myListOfMaps.add(map);               

}
//...
Stéphane Ammar
  • 1,454
  • 10
  • 17
0

This code will help you:

List<Map<String, Boolean>> myListOfMaps = new ArrayList<>();
List<String> al1 = new ArrayList<>();
al1.add("92");
al1.add("94");
al1.add("91");
al1.add("98");

String id = "95,98,94";

List<String> result = new ArrayList<>();
for (int i = 0; i < al1.size(); i++) {
    Map<String, Boolean> map = new HashMap<>();
    if (id.contains(al1.get(i))) {
        result.add("true");
        map.put("Access", true);
    } else {
        result.add("false");
        map.put("Access", false);
    }
    myListOfMaps.add(map);
}
myListOfMaps.forEach(System.out::println);
System.out.println(result);
Törpetestű
  • 192
  • 10