如何根据类别从列表(复杂数据类型)中获得前10条记录



我有一个类结构:

class TopTenAccount{
    Long account;
    Long src;
    Long dest;
    Long count;
}

我有这个对象的列表。

List<TopTenAccount> topTenAccount;

现在基于计数,我想要的只有10条记录(标准:"帐户+src+dest"应该是相同的每条记录)。

基本上我想要每个类别(帐户+src+dest)的前10个记录。

使用集合。使用自定义比较器进行排序

                   Collections.sort(list,new Comapartor<TopTenAccount> {
      //override compateTo method with your compare logic
        });

然后循环并获取列表中的前10个对象

        for(int i=0;i<10 && i < list.size();i++) {
             System.out.println(list.get(i));
        }

使用基于count字段的自定义排序。如果这是你需要的

topTenAccount.sort((x,y)=>{
int temp = x.count - y.count;
if(temp!=0)
return temp;
});

将根据count按升序对集合进行排序。下一步是执行循环并获取集合中的前10/后10项。

最新更新