In Java, I have:
Set<Integer> set = new HashSet<Integer>();
callVoidMethod(set);
...
public static void callVoidMethod(Set<Integer> set) {
Set<Integer> superset = new HashSet<Integer>(set);
...
// I just added this loop to show that I'm adding quite a lot
// well, it depends on conditions, sometimes I add nothing,
// but it is unpredictable and do not know if add something
for (int i = 0; i < 1000; i++) {
...
if (conditionSatisfied) superset.add(someValue);
...
}
}
The code above is simplified, the idea is to pass the set by reference into a void method and create a full copy of a set such that we will be able to add some new elements to the copy (superset here) and do not touch the set as we need it untouched when we exit the void method.
My code works with lots of data processing and if there is no faster way to make a copy, then I would like to optimize the HashSet
itself, for instance I do not need Integer
s as keys, but better primitive int
s. Would be a good idea to implement an int[]
array of keys in the MyHashSet
?
If so is possible, I would be interested in using the same idea for improving this:
Map<Integer, ArrayList<Item>> map = new HashMap<Integer, ArrayList<Item>>();
EDIT: I need only speed-performance-optimization. I do not need beautiful-maintainable code and memory.