简体   繁体   中英

Removing item from a list inside hashmap

I have an List<String> in a HashMap as value. The key is a String . When I loop through the HashMap and remove items from the list of a particular key it updates the lists mapped to all the keys in the map.

The code is below

public class ListClass {
public static void main(String[] args) {
    ListClass lc = new ListClass();
    for(Map.Entry<String, List<String>> entry : lc.postProcessList().entrySet()) {
        System.out.println("Date : "+entry.getKey());
        List<String> data = entry.getValue();
        for (int i = 0; i < data.size(); i++) {
            System.out.println("    Value : "+data.get(i));
        }
    }
} 

private Map<String,List<String>> postProcessList() {
    Map<String,List<String>> map = new HashMap<String, List<String>>();
    populateMap(map);
    for(Map.Entry<String, List<String>> entry : map.entrySet()) {
        String dateKey = entry.getKey();
        System.out.println("Date key : "+dateKey);
        List<String> data = entry.getValue();
        System.out.println("List before modification : "+data);
        for (int i = 0; i < data.size(); i++) {
            String dateNoTime = data.get(i).split(" ")[0];
            if(!dateNoTime.equalsIgnoreCase(dateKey)) {
                System.out.println("Removing : "+data.get(i));
                data.remove(i);
            }   
        }
        System.out.println("List after modification: "+data+"\n\n");
    }
    return map;
}

private Map<String,List<String>> populateMap(Map<String,List<String>> map) {
    List<String> list = new ArrayList<String>();
    list.add("2015-01-13 09:30:00");
    list.add("2015-01-12 05:45:10");
    list.add("2015-01-13 06:22:12");
    list.add("2015-01-12 01:52:40");
    list.add("2015-01-12 02:23:45");    
    map.put("2015-01-12", list);
    map.put("2015-01-13", list);
    return map;
}
}

In the above code, the Map has key which is a string and holds date values. List, which is the value of the map, holds date and time.

First key is "2015-01-12" and I'm trying to remove the items in the list that are not "2015-01-12". Now the list in both the keys get's updated. Why is that so?

You are adding the same list instance to the map against both keys. Therefore when you retrieve it using key "2015-01-12" and edit it, you'll see the changes in the entire map.

You need to clone the list using something like list.clone() or new ArrayList<String>(list) before you add it to each key.

eg

ArrayList<String> list = new ArrayList<String>();
list.add("2015-01-13 09:30:00");
list.add("2015-01-12 05:45:10");
list.add("2015-01-13 06:22:12");
list.add("2015-01-12 01:52:40");
list.add("2015-01-12 02:23:45");    

map.put("2015-01-12", (List<String>)list.clone());
map.put("2015-01-13", (List<String>)list.clone());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM