java-get对象的arraylist中最常用的元素

pxy2qtax  于 2021-06-30  发布在  Java
关注(0)|答案(4)|浏览(248)

我有一个 ArrayList 看起来是这样的:

ArrayList<TravelData> listOfTravels;

以及 TravelData 对象包含以下元素

int id;
String groupName;
String guideName;

以下是初始化arraylist的方法:

listOfTravels.add(new TravelData(1,"group a", "Ross"));
listOfTravels.add(new TravelData(2,"group a", "Chandler"));
listOfTravels.add(new TravelData(3,"group a", "Monica"));
listOfTravels.add(new TravelData(4,"group b", "Phoebe"));
listOfTravels.add(new TravelData(5,"group b", "Rachel"));
listOfTravels.add(new TravelData(6,"group c", "Joey"));
listOfTravels.add(new TravelData(7,"group c", "Rachel"));
listOfTravels.add(new TravelData(8,"group d", "Chandler"));

我想得到名单上最常见的三个组和指南。在本例中:

Top 3 groups: "group a", "group b", "group c"
Top 3 guides: "Rachel", "Chandler", "Ross"

(导游的第三名可能是其他人,因为罗斯、莫妮卡、菲比和乔伊的出场次数相同)。
我找到了很好的答案,比如如何获取列表中最常见的元素,但它只对 ArrayList 只显示第一个最常用的元素。

yzxexxkh

yzxexxkh1#

这是流的一个很好的用例。我们的想法是,首先要改变 List<TravelData>Map<String, Integer> 从组到辅助线的数目(或从辅助线到组的数目),然后按数字对Map排序,只取Map的键(组/辅助线),并将其限制为前三个元素。
我假设traveldata拥有 String getGroup() 以及 String getGuide() :
在示例中使用java11类型推断

final comparator = Comparator.comparing(Map::getValue)
                             .reversed();

final top3Groups = listOfTravels.stream().collect(Collectors.groupingBy(TravelData::getGroup(),
                                                                        Collectors.counting())
                                .entrySet()
                                .stream().sorted(comparator)
                                         .map(Map::getKey)
                                         .limit(3)
                                         .collect(Collectors.toList());

final top3Guides = listOfTravels.stream().collect(Collectors.groupingBy(TravelData::getGuide(),
                                                                        Collectors.counting())
                                .entrySet()
                                .stream().sorted(comparator)
                                         .map(Map::getKey)
                                         .limit(3)
                                         .collect(Collectors.toList());
ar7v8xwq

ar7v8xwq2#

这是我的解决方案。

// comparator for reverse sorting
Comparator<Map.Entry<String, Long>> reversed = Map.Entry.comparingByValue();
Comparator<Map.Entry<String, Long>> entryComparator = Collections.reverseOrder(reversed);

List<String> collect = listOfTravels.stream()
        .collect(Collectors.groupingBy(TravelData::getGuideName, Collectors.counting()))
        .entrySet()
        .stream()
        .sorted(entryComparator)
        .map(Map.Entry::getKey)
        .limit(3)
        .collect(Collectors.toList());
System.out.println(collect);
ki0zmccv

ki0zmccv3#

你可以创造一个 hashMap 结构 groups 在哪里 keygroupName 值将是出现的次数。
那么,就 sort 这个 hashMap 价值结构 descending 然后得到结果。

dxpyg8gm

dxpyg8gm4#

试试这个,它会帮你的。

public class TravelData {
int id;
String groupName;
public String getGroupName() {
    return groupName;
}

public String getGuideName() {
    return guideName;
}

String guideName;

public TravelData(int id, String groupName, String guideName) {
    this.id = id;
    this.groupName = groupName;
    this.guideName = guideName;
}

public static void getcommonGroups(ArrayList<TravelData>  list){
    Map<String, Integer> wordMap = new HashMap<String, Integer>();

    for(TravelData td:list){
        if(wordMap.containsKey(td.getGroupName())){
            wordMap.put(td.getGroupName(), wordMap.get(td.getGroupName())+1);
        } else {
            wordMap.put(td.getGroupName(), 1);
        }
    }

    List<Map.Entry<String, Integer>> sortedList = sortByValue(wordMap);
    for(Map.Entry<String, Integer> entry:sortedList){
        System.out.println(entry.getKey()+" ===="+entry.getValue());
    } }

  public static List<Map.Entry<String, Integer>> sortByValue(Map<String, Integer> 
  wordMap)  {

    Set<Map.Entry<String, Integer>> set = wordMap.entrySet();
    List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(set);
    Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
    {
        public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
        {
            return (o2.getValue()).compareTo( o1.getValue() );
        }
    } );
    return list;
}

public static  void main(String args[]){
    ArrayList<TravelData> listOfTravels=new ArrayList() ;
    listOfTravels.add(new TravelData(1,"group a", "Ross"));
    listOfTravels.add(new TravelData(2,"group a", "Chandler"));
    listOfTravels.add(new TravelData(3,"group a", "Monica"));
    listOfTravels.add(new TravelData(4,"group b", "Phoebe"));
    listOfTravels.add(new TravelData(5,"group b", "Rachel"));
    listOfTravels.add(new TravelData(6,"group c", "Joey"));
    listOfTravels.add(new TravelData(7,"group c", "Rachel"));
    listOfTravels.add(new TravelData(8,"group d", "Chandler"));
    getcommonGroups(listOfTravels);
}}

相关问题