I have class containing multiple HashMaps. The values in these HashMaps are the same but the keys are different. I have to remove the same element from all Maps that contain it. The method that removes these elements takes a Collection as argument, iterates over it and removes elements from multiple HashMaps.
Here is the code:
private Map<Position, Place> map1 = new HashMap<Position, Place>();
private Map<String, List<Place>> map2 = new HashMap<String, List<Place>>();
private Map<Category, List<Place>> map3 = new HashMap<Category, List<Place>>();
public void removePlaces2(Collection<Place> places) {
Iterator<Place> iter = places.iterator();
while (iter.hasNext()) {
Place p = iter.next();
Position pos = p.getPosition();
String name = p.getName();
Category cat = p.getCategory();
map1.remove(pos);
List<Place> list1 = map2.get(name);
list1.remove(p);
if (list1.isEmpty()) {
map2.remove(name);
}
if (cat != null) {
List<Place> list2 = map3.get(cat);
list2.remove(p);
}
this.remove(p);
modified = true;
}
revalidate();
}
The method throws an ConcurrentModificationException at the line Place p = iter.next();. (But not every time.) I'm at a loss as to how to avoid it. If I use iter.remove(p) that will only remove the element from the argument to the method: Collection<Place> places. And this is not what I want.
The question is how can I avoid this exception while removing the element from multiple maps?
Notice: I am not iterating over the maps from which the element is to be removed.
If you are passing map1, map2, or map3 as an argument to removePlaces2(), create a copy instead:
removePlaces2(new LinkedList<Place>(map1.values()));
If the exception persists try using thread safe versions of the maps:
private Map<Position, Place> map1 = Collections.synchronizedMap(new HashMap<Position, Place>());
private Map<String, List<Place>> map2 = Collections.synchronizedMap(new HashMap<String, List<Place>>());
private Map<Category, List<Place>> map3 = Collections.synchronizedMap(new HashMap<Category, List<Place>>());
Related
I am trying to add a Key(String), Value(HashMap) to another HashMap. I somehow keep jumbling up the syntax and logic here. How do I do that? I have a kmap here initialized and then I want to add a key which is a string, and a value which is another HashMap<String, List<Integer>>)
These can be seen in the parameters below:
static HashMap<String, HashMap<String, List<Integer>>> kmap = new HashMap<String, HashMap<String, List<Integer>>>();
public synchronized static void AddMapToList_ofMAP_(HashMap<String, List<Integer>> value, String key) {
if (!kmap.containsKey(key)) {
kmap.put(key, new HashMap<String, List<Integer>>());
}
HashMap<String, List<Integer>> q = kmap.get(key);
q.put(key, value);
}
It's WAY simpler than you're making it seem. You don't need a separate method, just invoke the put(key, value) method on your kmap. Assuming you have a Map<String, List<Integer>> in a variable named value, it's just:
kmap.put(key, value);
That's all. Just one line.
In your parameters you have got a HashMap called value. You are then trying to add that to the HashMap inside the HashMap but the value in that needs to be a List of integers.
Fix:
static HashMap<String, HashMap<String, List<Integer>>> kmap = new HashMap<String, HashMap<String, List<Integer>>>();
public synchronized static void AddMapToList_ofMAP_(
List<Integer> value, String key) {
if (!kmap.containsKey(key)) {
kmap.put(key, new HashMap<String, List<Integer>>());
}
HashMap<String, List<Integer>> q = kmap.get(key);
q.put(key, value);
}
Also, a possible way to make this better is using an Object. I'm not sure how the code and what your putting in but an object could work.
I'm also seeing you get the HashMap by the key but you also put that key in the HashMap (The one inside), surely you could just have 1 HashMap there.
I'm not sure what exactly you are trying to achieve here. Here's what possibly you may want to do. Feel free to write more test cases and optimize the code. However this will give you a based structure to work on.
public class Stackoverflow {
static HashMap<String, HashMap<String, List<Integer>>> kmap = new HashMap<String, HashMap<String, List<Integer>>>();
public synchronized static void addIntegerToKmap(String kmapKey, String intMapKey, Integer value) {
if (!kmap.containsKey(kmapKey)) {
Map<String, List<Integer>> intMap = new HashMap<String, List<Integer>>();
HashMap<String, List<Integer>> stringListHashMap = new HashMap<String, List<Integer>>();
List<Integer> integerList = new ArrayList<Integer>();
integerList.add(value);
stringListHashMap.put(intMapKey, integerList);
kmap.put(kmapKey, stringListHashMap);
}
else {
HashMap<String, List<Integer>> stringListHashMap = kmap.get(kmapKey);
List<Integer> integerList = stringListHashMap.get(intMapKey);
if (integerList != null && !integerList.isEmpty()) {
integerList.add(value);
}
else {
integerList = new ArrayList<Integer>();
integerList.add(value);
stringListHashMap.put(intMapKey, integerList);
}
}
}
public static void main(String args[]) {
addIntegerToKmap("A", "A1", 1);
addIntegerToKmap("A", "A1", 2);
addIntegerToKmap("A", "A2", 12);
addIntegerToKmap("A", "A2", 22);
addIntegerToKmap("B", "B1", 1);
addIntegerToKmap("A", "A1", 3);
}
}
The logic is not clear but maybe you want this
q.put(key, value.get(key));
instead of this:
q.put(key, value);
My last attempt was poorly explained, so recreated with hopefully a clearer explanation.
I have a Collection that has the following data structure.
LinkedHashMap<String, String> currentHashMap = new LinkedHashMap<String, String>();
currentHashMap.put("id","12345");
currentHashMap.put("firstName","John");
currentHashMap.put("lastName","Doe");
haystack.add(currentHashMap);
*repeat for roughly 250,000 more entries
I need to search the HashMaps within each list entry, check if the key exists, and if it has the corresponding value. If it does, I want to return all the values in that particular hashmap
This is the code that I'm currently using.
private TreeSet<String> searchWithinCollection(List<LinkedHashMap<String, String>> haystack, String needle, String needleKey) {
TreeSet<String> returnValueSet = new TreeSet<>();
for (LinkedHashMap<String, String> mappedData : haystack) {
System.out.println(mappedData.values());
for (Entry<String, String> specificData : mappedData.entrySet()) {
if (needleKey.equals(specificData.getKey()) && needle.equals(specificData.getValue())) {
//where I want to collect the values within the current hashmap, as it's satisfied the search criteria
//the current code would only return the current key/balue pair, even if all values were collected outside the for loop. Instead, I'd want to collect "12345", "John", "Doe" if needleKey = id and needle = 12345.
}
}
}
return returnValueSet;
}
What I'd want is if a key in the current collection equaled "id", and it's value equaled "12345", then I could returned all values within that particular collection (and to use the example above, would be "12345", "John", "Doe").
The best I can do is return the first key/value, which isn't very helpful obviously. Attempting to capture all values in mappedData outside the second for loop brought no result (either with creating a new instance of a Collection, clone, or what have you.). I also found nothing that could manually advance the pointer in the for each loop to manually capture all values in the Collection.
EDIT: If it helps, where there is a System.out.println(mappedData.values()); I can get the values I'm expecting, but only the id in the example is return if values() is accessed within the if statement.
You should not iterate over all the entries in a hashmap (currently, the loop for (Entry<String, String> specificData : mappedData.entrySet()) in your code. This defeats the purpose of maintaining a hashmap.
Instead, the inner loop should become
String potentialNeedle = mappedData.get(needleKey);
if (needle.equals(potentialNeedle))
returnValueSet.addAll(mappedData.values());
You're using the map as if it was a list of entries, which completely defeats the purpose of a map: quickly access to a value for a given key:
for (Map<String, String> mappedData : haystack) {
String value = mappedData.get(needleKey);
if (needle.equals(value)) {
returnValueSet.addAll(mappedData.values());
}
}
Maybe you should take redesign in account to improve performance. it could be smart to have a map for each field you want to search. instead of your haystack create your data-structure like that:
public class PersonDataManager {
private final Map<Integer, Map<String, String>> baseData;
private final Map<String, List<Integer>> firstNameLookupMap;
private final Map<String, List<Integer>> lastNameLookupMap;
public PersonDataManager(){
this.baseData = new HashMap<>();
this.firstNameLookupMap = new HashMap<>();
this.lastNameLookupMap = new HashMap<>();
}
public void addPerson(Integer id, String firstName, String lastName){
//try to find existing person to update:
Map<String, String> personMap = baseData.get(id);
if(personMap == null){
personMap = new HashMap<>();
baseData.put(id, personMap);
}
personMap.put("firstName", firstName);
personMap.put("lastName", lastName);
//add to lookup-maps
addLookupName(firstNameLookupMap, id, firstName);
addLookupName(lastNameLookupMap, id, lastName);
}
private static void addLookupName(Map<String, List<Integer>> nameMap, Integer id, String name){
//get existing list of the name:
List<Integer> idList = nameMap.get(name);
if(idList == null){
idList = new ArrayList<>();
}
if(!idList.contains(id)){
idList.add(id);
}
}
private List<Map<String, String>> searchByName(Map<String, List<Integer>> nameMap, String name){
List<Integer> matchingIds = nameMap.get(name);
List<Map<String, String>> result = new ArrayList<>();
if(matchingIds != null){
for(Integer id : matchingIds){
result.add(searchById(id));
}
}
return result;
}
public Map<String, String> searchById(Integer id){
return baseData.get(id);
}
public List<Map<String, String>> searchByFirstName(String name){
return searchByName(firstNameLookupMap, name);
}
public List<Map<String, String>> searchByLastName(String name){
return searchByName(lastNameLookupMap, name);
}
}
This way you can easily get a hashmap of a person using its id. if you need to search by firstname or lastname you can utilize the additional maps to get the matching person-ids. hope this helps
EDIT: Just implemented a class which does exactly what i think you might need. have fun ;)
I don't know how can best describe my problem but here it is, I'm trying to remove the same names(values) from HashMap<String, String> map = new HashMap<String, String>();
for example if this map contain names like
map.put("Vivaldi","Antonio");
map.put("Belucci", "Monica");
map.put("Gudini", "Harry");
map.put("Verdo", "Dhuzeppe");
map.put("Maracci", "Bruno");
map.put("Carleone", "Vito");
map.put("Bracco", "Luka");
map.put("Stradivari", "Antonio");
I want to remove all entries with the value "Antonio" from it by using method removeTheFirstNameDuplicates, I looked in Google for a couple of days and all examples are close to what I want but not really what I need.
My thoughts are, I need something that will check a map and if it contains the same values in it then remove the duplicate. But how can I do this?
You can do it with the following method which only iterates over the map once:
private static void removeTheFirstNameDuplicates(final Map<String, String> map) {
final Iterator<Entry<String, String>> iter = map.entrySet().iterator();
final HashSet<String> valueSet = new HashSet<String>();
while (iter.hasNext()) {
final Entry<String, String> next = iter.next();
if (!valueSet.add(next.getValue())) {
iter.remove();
}
}
}
The add() method on HashSet will return false if a value has already been added to the set. The method above uses this to detect that a duplicate has been found and then removes the duplicate from the HashMap by using the remove() method on the iterator.
It is worth noting that, depending on the Map implementation you use, the iteration order may not be guaranteed so which duplicate you remove is also not guaranteed.
If you were to use a TreeMap rather than a HashMap you would be certain to iterate over the map alphabetically by key e.g. Berluccio, Bracco, Carleone … Verdo. You would then always keep Stradivari and remove Vivaldi.
Try this
ArrayList<String> values = new ArrayList<String>();
ArrayList<String> keys = new ArrayList<String>();
java.util.Iterator<Entry<String, String>> iterate = map.entrySet()
.iterator();
while (iterate.hasNext()) {
Entry mapEntry = iterate.next();
String key = (String) mapEntry.getKey();
String value = (String) mapEntry.getValue();
values.add(value);
keys.add(key);
}
for (int i = 0; i < values.size(); i++) {
if (Collections.frequency(values, values.get(i)) > 1) {
map.remove(keys.get(i));
}
}
System.out.println(map.toString());
Let's say I got a Map<String, String> and I wanna remove all the entries that value contains foo. What is the best way to do it, in terms of optimization/memory/etc.? The four syso below are printing the same result, that is to say {n2=bar}.
public static void main(String[] args) {
Map<String, String> in = new HashMap<String, String>();
in.put("n1", "foo");
in.put("n2", "bar");
in.put("n3", "foobar");
// 1- create a new object with the returned Map
Map<String, String> in1 = new HashMap<String, String>(in);
Map<String, String> out1 = methodThatReturns(in1);
System.out.println(out1);
// 2- overwrite the initial Map with the returned one
Map<String, String> in2 = new HashMap<String, String>(in);
in2 = methodThatReturns(in2);
System.out.println(in2);
// 3- use the clear/putAll methods
Map<String, String> in3 = new HashMap<String, String>(in);
methodThatClearsAndReadds(in3);
System.out.println(in3);
// 4- use an iterator to remove elements
Map<String, String> in4 = new HashMap<String, String>(in);
methodThatRemoves(in4);
System.out.println(in4);
}
public static Map<String, String> methodThatReturns(Map<String, String> in) {
Map<String, String> out = new HashMap<String, String>();
for(Entry<String, String> entry : in.entrySet()) {
if(!entry.getValue().contains("foo")) {
out.put(entry.getKey(), entry.getValue());
}
}
return out;
}
public static void methodThatClearsAndReadds(Map<String, String> in) {
Map<String, String> out = new HashMap<String, String>();
for(Entry<String, String> entry : in.entrySet()) {
if(!entry.getValue().contains("foo")) {
out.put(entry.getKey(), entry.getValue());
}
}
in.clear();
in.putAll(out);
}
public static void methodThatRemoves(Map<String, String> in) {
for(Iterator<Entry<String, String>> it = in.entrySet().iterator(); it.hasNext();) {
if(it.next().getValue().contains("foo")) {
it.remove();
}
}
}
The best way is methodThatRemoves because:
In terms of memory consumption: it doesn't create a new map so doesn't add memory overhead.
In terms of CPU use: iterator has O(1) complexity for calling next or removing the current element.
The most efficient way methodThatRemoves, because it
Uses almost no memory
Creates no objects except the (lightweight) iterator
Is extremely fast (doesn't use any map lookups)
I would not make a copy first though, unless you have an unmodifiable map or you need to preserve the original.
For me the best is the one with the Iterator - methodThatRemoves, because you don't create an intermediate Map and don't use put method.
By the way the first one : methodThatReturns can be faster because put complexity is O(1) whereas remove is O(n) in worst case but it will use more memory because you have 2 different instances of Map.
I would personally go with methodThatRemoves because you are only performing a loop operation and checking for "foo" equality. The others do that as well as Object map creation and map clear/put operations. So you clearly have 1 method doing less.
Also if you want to reduce memory usage you are better off not creating an extra HashMap just to remove 1 or more entries. This is assuming you don't mind the extra computation to iterate the map.
If you really want to go more in depth, you should evaluate this using a profiler or some sort.
Given the class:
public class CategoryValuePair
{
String category;
String value;
}
And a method:
public Map<String,List<String>> convert(CategoryValuePair[] values);
Given that in values we can receive many entries with the same category, I want to convert these into a Map grouped on category.
Is there a quick / efficient way to perform this conversion?
As far as I know there is not easier way than iterating on values, and then putting the values in the map (like some predefined method).
Map<String, List<String>> map = new HashMap<String, List<String>>();
if (values != null) {
for (CategoryValuePair cvp : values) {
List<String> vals = map.get(cvp.category);
if (vals == null) {
vals = new ArrayList<String>();
map.put(cvp.category, vals);
}
vals.add(cvp.value);
}
}
I changed the map values from String[] to List<String> since it seems easier to me to use that so you don't have to hassle with array resizing.
To make it in fewer lines of code, use Google Collections:
public Map<String, Collection<String>> convert(CategoryValuePair[] values) {
Multimap<String, String> mmap = ArrayListMultimap.create();
for (CategoryValuePair value : values) {
mmap.put(value.category, value.value);
}
return mmap.asMap();
}
If you don't want to allow duplicate values, replace ArrayListMultimap with HashMultimap.
With lambdaj you just need one line of code to achieve that result as it follows:
group(values, by(on(CategoryValuePair.class).getCategory()));
Just for the sake of implementation... The method returns Map and also checks for duplicates in the arrays... though performance wise its heavy ...
public Map<String,String[]> convert(CategoryValuePair[] values)
{
Map<String, String[]> map = new HashMap<String, String[]>();
for (int i = 0; i < values.length; i++) {
if(map.containsKey(values[i].category)){
Set<String> set = new HashSet<String>(Arrays.asList(map.get(values[i].category)));
set.add(values[i].value);
map.put(values[i].category, set.toArray(new String[set.size()]));
}else {
map.put(values[i].category, new String[]{values[i].value});
}
}
return map;
}