我正在编写一个程序,试图获得一个类别的最高百分比(这些类别存储在一个 Entry 对象中)。我现在要做的是存储所有百分比最高的类别,因为这是一个条件这可能会发生。所以问题是:“我怎样才能存储那些百分比值最高的类别?”这是我的代码到目前为止的样子:
double biggestPercentage = 0;
String biggestPercentageCategory = null;
ArrayList<String> biggestPercentageCategories = new ArrayList<String>();
double percentage = 0;
System.out.println("\n");
System.out.println("Get user's most interested category:");
for(int i=0;i<entrySet.size();i++){
allEntries = entrySet.get(i);
if(allEntries.getValue() != 0){
double calculatePercentage = allEntries.getValue();
percentage = Double.parseDouble(df.format((calculatePercentage*100)/totalNumberOfContentForEachUser1));
System.out.println("Percentage of " + allEntries.getKey() + ": " + percentage +"%");
if(percentage > biggestPercentage){
biggestPercentage = percentage;
biggestPercentageCategory = allEntries.getKey();
}
}
if(percentage == biggestPercentage){
biggestPercentageCategories.add(allEntries.getKey());
}
}
HashMap<String,String> hashmap = new HashMap();
if(percentage > biggestPercentage){
hashmap.put(userName, biggestPercentageCategory);
}
else if(percentage == biggestPercentage){
hashmap.put(userName, biggestPercentageCategories.toString());
}
for(Entry<String, String> str : hashmap.entrySet()){
System.out.println("\n");
System.out.println(str);
}
输出如下所示:
获取用户最感兴趣的类别:
Percentage of Entertainment_Culture: 12.5%
Percentage of War_Conflict: 12.5%
Percentage of Sports: 25.0%
Percentage of Hospitality_Recreation: 12.5%
Percentage of Human Interest: 12.5%<br>
Percentage of Education: 25.0%
dryice1993=[Entertainment_Culture, War_Conflict, Sports, Education]
其中“dryice1993”只是此人的用户名。
但我想要的预期输出是:
dryice1993=[Sports, Education]?