Description
I have a HashMap<ArrayLists<Integer>, <Integer>>
, similar to the following ({Key=Value}
):
{[1]=1, [3]=1, [1, 4, 6]=1, [0, 2, 3, 5, 6]=3, [6]=1}
I need to compare and then modify/remove elements in different ArrayLists
(i.e., elements in the HashMap
), until the following conditions are met:
- Each
ArrayList
element only belongs to one list, the list with the highestValue
. - If
Value = 1
for all lists containing that element, then theArrayList
element belongs to the singleton list. - If an
ArrayList
becomes empty, then it should be removed from theHashMap
.
Thus for the example above, it should end up with the following:
{[1]=1, [4]=1, [0, 2, 3, 5, 6]=3}
I am used to work with arrays of arrays to do stuff like this. This time it would be practical to have the features of HashMap
and ArrayList
, but I am currently not comfortable to do more complicated modification of these data types. I have done several attempts and have had to prevent both ConcurrentModificationException and IllegalStateException, but have yet to succeed fully. I also have a feeling my implementations are getting unnecessary complex, so I would greatly appreciate to see an implementation by someone experienced with things like this.
A Note About The HashMap
The reason I use a HashMap
(feel free to suggest something more appropriate) is that the Value
is a count of how many times the ArrayList
has been "encountered" and added to the HashMap
.
Minimal Example
Minimal example of my latest non-working (IndexOutOfBoundsException
) attempt. Note that the creation of the HashMap
and ArrayList
s is done statically here since in my real program it is done non-deterministically based on file contents.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<List<Integer>, Integer> example = new HashMap<>(7);
List<Integer> list = new ArrayList<>(7);
list.add(1);
example.put(list, 1);
list = new ArrayList<>(7);
list.add(3);
example.put(list, 1);
list = new ArrayList<>(7);
list.add(1);
list.add(4);
list.add(6);
example.put(list, 1);
list = new ArrayList<>(7);
list.add(0);
list.add(2);
list.add(3);
list.add(5);
list.add(6);
example.put(list, 3);
list = new ArrayList<>(7);
list.add(6);
example.put(list, 1);
System.err.println(example);
Map<List<Integer>, Integer> copy = new HashMap<>(example);
for (Map.Entry<List<Integer>, Integer> outer : example.entrySet()) {
for (Map.Entry<List<Integer>, Integer> inner : copy
.entrySet()) {
for (int i : outer.getKey()) {
int oSize = outer.getKey().size();
int iSize = inner.getKey().size();
int oValue = outer.getValue();
int iValue = inner.getValue();
if (!(inner.equals(outer)) && (inner.getKey()
.contains(i))) {
if (oSize == 1) {
if (oValue < iValue) {
outer.getKey().remove(i);
} else {
inner.getKey().remove(i);
}
} else if (iSize == 1) {
if (iValue < oValue) {
outer.getKey().remove(i);
} else {
inner.getKey().remove(i);
}
} else {
if (oValue < iValue) {
outer.getKey().remove(i);
} else {
inner.getKey().remove(i);
}
}
}
}
}
}
}
}