Reputation: 668
I would like to implement a fast "group by" like feature in Java.
I have a List<List<String>>
and I want to iterate through based on groupping on different indexes.
For example:
A1 B1 C1 value_1 A1 B1 C2 value_2 A1 B2 C1 value_3 A1 B2 C2 value_4
I want some aggregations using groupping on first and third column. And I want it to be fast - avoid calculating every sum at every query. The values from the "table" are changing constantly. Any thoughts?
Upvotes: 2
Views: 558
Reputation: 133577
Some considerations: first of all you will need a custom Comparator
for every order you want to sort your items into. Let's assume your objects are Foo
instances then you'll have
class FirstComparator implements Comparator<Foo> {
public int compareTo(Foo o1, Foo o2) {
...
}
}
class SecondComparator implements Comparator<Foo> {
public int compareTo(Foo o1, Foo o2) {
...
}
}
and so on.
Then you will be able to easily sort the collection by using Collections.sort(fooList, yourComparator)
.
The problem here is that you want many custom sorting orders and you want the to be dynamically updated. Best think I suggest here is to have many different collections that contain the same items, already in order.
You can do it by having, for example:
TreeMap<Foo> firstOrder = new TreeMap<Foo>(new FirstComparator<Foo>());
TreeMap<Foo> secondOrder = new TreeMap<Foo>(new SecondComparator<Foo>());
now, when you add an item to the set you should just add it to both collections and they will be automatically already ordered, you won't have to call sort on them and will be dynamically updated when you add or remove elements. The only additional weight is that you will twice the references to the objects, so you are trading space for speed.
Mind that this won't work if column values change after being added to the trees, because this will need a re-sort of the whole TreeMap
which must be invoked explicitly. If you change the comparator value on an existing element in the map you will just invalidate it.
Upvotes: 1
Reputation: 5173
Really sounds like the easiest (least programming) approach would be to use a SQL database. You could use an in-memory SQLite database. The best Java library for SQLite comes from Xerial.org.
Upvotes: 1
Reputation: 64632
Java is not well suited for that task. I'd rather go with an in memory SQL database. First dump the values to a table and then retrieve the rows with a select
SQL statement grouping, ordering or summing by different columns.
Upvotes: 0