What's a good way to do this in Java?
Replace the Set<Long>
and List<Object[]>
by a Map<Long, BigInteger>
. If the ordering is not important, then use HashMap
. If you'd like to sort automagically on keys, then use TreeMap
. If you'd like to maintain insertion order, then use LinkedHashMap
.
E.g.
Map<Long, BigInteger> unorderedMap = new HashMap<Long, BigInteger>();
Map<Long, BigInteger> orderedByKeys = new TreeMap<Long, BigInteger>();
Map<Long, BigInteger> orderedByInsertion = new LinkedHashMap<Long, BigInteger>();
This way you can just use any of the Map
methods to handle key/value pairs. E.g.
Long key = 1L;
BigInteger value = map.get(key);
if (value == null) {
value = new BigInteger(0);
map.put(key, value);
}
You can even get all keys by Map#keySet()
:
Set<Long> keys = map.keySet();
To learn more about maps, consult Sun's own tutorial about the subject.