List<String> aa = ....;
Map<String, Integer> map = new HashMap<String,Integer>();
for (int i=0;i<aa.size();i++) {
String temp = aa .get(i);
if(map.containsKey(temp))
map.put(temp, map.get(temp)+1);
else
map.put(temp, 1);
}
List<Integer> sortList = new ArrayList<Integer>();
//由于存在重复次数相同的元素,所以将mapSort设置为Map<Integer,List<String>>,其中List<String>用来存储次数相同的String
Map<Integer,List<String>> mapSort = new HashMap<Integer,List<String>>();
for(Map.Entry<String, Integer> entry: map.entrySet()){
String mapKey = entry.getKey();
Integer mapValue = entry.getValue();
if(mapSort.containsKey(mapValue)){
List<String> list = mapSort.get(mapValue);
list.add(mapKey);
}else{
List<String> list = new ArrayList<String>();
list.add(mapKey);
mapSort.put(mapValue, list);
sortList.add(mapValue);
}
}
//对mapSort进行排序,即对mapSort的key值进行排序
Collections.sort(sortList);
int p = 0; //记录打印次数
String pairValue = "";//获取重复度最多的3个元素
for(int i = sortList.size()-1; i >= 0; i--) {
List<String> list = mapSort.get(sortList.get(i));
for(int j = 0; j < list.size() && p < 3; j++) {
pairValue += list.get(j) + "\t";
p++;
}
}
Map<String, Integer> map = new HashMap<String,Integer>();
for (int i=0;i<aa.size();i++) {
String temp = aa .get(i);
if(map.containsKey(temp))
map.put(temp, map.get(temp)+1);
else
map.put(temp, 1);
}
List<Integer> sortList = new ArrayList<Integer>();
//由于存在重复次数相同的元素,所以将mapSort设置为Map<Integer,List<String>>,其中List<String>用来存储次数相同的String
Map<Integer,List<String>> mapSort = new HashMap<Integer,List<String>>();
for(Map.Entry<String, Integer> entry: map.entrySet()){
String mapKey = entry.getKey();
Integer mapValue = entry.getValue();
if(mapSort.containsKey(mapValue)){
List<String> list = mapSort.get(mapValue);
list.add(mapKey);
}else{
List<String> list = new ArrayList<String>();
list.add(mapKey);
mapSort.put(mapValue, list);
sortList.add(mapValue);
}
}
//对mapSort进行排序,即对mapSort的key值进行排序
Collections.sort(sortList);
int p = 0; //记录打印次数
String pairValue = "";//获取重复度最多的3个元素
for(int i = sortList.size()-1; i >= 0; i--) {
List<String> list = mapSort.get(sortList.get(i));
for(int j = 0; j < list.size() && p < 3; j++) {
pairValue += list.get(j) + "\t";
p++;
}
}