A simple question, but having trouble finding the answer on google. Since normally I work with SQL, I'm used to having access to set based operations like this:
select Q, qCount
from (
select Q, qCount, min(qCount) over () as minQCount
from (
select Q, count(*) as qCount
from table_A
group by Q
)
)
where qCount = minQCount
This finds all the values of Q from the table that have the lowest cardinality amongst the set of distinct Q values.
Is there an established efficient way of doing that for a Java List? Say:
List<Q> listOfQ //gets populated with different objects of Q
//objects of Q can represent the same value through an overridden equals()
//get each object from Q for which the cardinality of the value it represents
//is the lowest amongst the distinct values in the list
a simple example being:
List<Integer> list = new ArrayList<Integer>();
list.addAll(Arrays.asList(new Integer[] {1,1,1,2,2,3,3,3,4,4,4,4,5,5}));
//want to retrieve {2,2,5,5}
//would also be nice to easily retrieve a map with {k:2 v:2, k:5 v:2}
//this being the equivalent of the SQL query above
Thanks!