一个简单的问题与最低基数的元素,但无法找到对谷歌的答案。因为通常我用SQL的工作,我已经习惯了有机会获得基于集合的操作是这样的:选择从Java列表结构
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
此发现从具有最低基数跻身组不同的Q值的表Q的所有值。
有没有一个确定的有效方法来做Java列表?你说:
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
一个简单的例子是:
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
谢谢!
“基数”是Java的一个奇特的词。谷歌“计数事件”或“频率”,你会有更多的运气。相关SO问题:http://stackoverflow.com/questions/14260134/elegant-way-of-counting-occurrences-in-a-java-collection –