我正在編寫一個試圖獲得最高百分比類別的程序(其中這些類別存儲在Entry對象中)。我現在要做的是將所有類別百分比最高,因爲它是可以occur.So問題的條件是:「我如何存儲這些類別比例最高值?」這是我的代碼看起來像至今:從條目對象中獲取最高百分比值Java
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]?
您可能想要使用排序 –
您可以這樣做1)對收集進行排序2)找到第一個條目的百分比3)找到所有條目具有相同最大百分比 –