public static void mySort2(List<Student> students){
//判断各属性值重复的次数,每个属性的重复重复次数的记录放入一个map
Map<Integer,Integer> mapAreaId = new HashMap<Integer, Integer>();
Map<Integer,Integer> mapDifficult = new HashMap<Integer, Integer>();
Map<Integer,Integer> mapType = new HashMap<Integer, Integer>();
Map<Integer,Integer> mapTitleType = new HashMap<Integer, Integer>();
for (int i = 0; i < students.size(); i++) {
if(mapAreaId.containsKey(students.get(i).getAreaId())){
mapAreaId.put(students.get(i).getAreaId(),mapAreaId.get(students.get(i).getAreaId())+1 );
}else{
mapAreaId.put(students.get(i).getAreaId(), 1);
}
if(mapDifficult.containsKey(students.get(i).getDifficult())){
mapDifficult.put(students.get(i).getDifficult(),mapDifficult.get(students.get(i).getDifficult())+1 );
}else{
mapDifficult.put(students.get(i).getDifficult(), 1);
}
if(mapType.containsKey(students.get(i).getTitleType())){
mapType.put(students.get(i).getTitleType(),mapType.get(students.get(i).getTitleType())+1 );
}else{
mapType.put(students.get(i).getTitleType(), 1);
}
if(mapTitleType.containsKey(students.get(i).getType())){
mapTitleType.put(students.get(i).getType(),mapTitleType.get(students.get(i).getType())+1 );
}else{
mapTitleType.put(students.get(i).getType(), 1);
}
}
Set<Entry<Integer, Integer>> set1 = mapAreaId.entrySet();
for (Iterator<Entry<Integer, Integer>> it = set1.iterator(); it.hasNext();) {
Map.Entry<Integer, Integer> entry = (Map.Entry<Integer, Integer>) it.next();
System.out.println(entry.getKey() + "--->" + entry.getValue());
}
System.out.println("map1END--------------------------------");
Set<Entry<Integer, Integer>> set2 = mapDifficult.entrySet();
for (Iterator<Entry<Integer, Integer>> it = set2.iterator(); it.hasNext();) {
Map.Entry<Integer, Integer> entry = (Map.Entry<Integer, Integer>) it.next();
System.out.println(entry.getKey() + "--->" + entry.getValue());
}
System.out.println("map2END---------------------------------");
Set<Entry<Integer, Integer>> set3 = mapType.entrySet();
for (Iterator<Entry<Integer, Integer>> it = set3.iterator(); it.hasNext();) {
Map.Entry<Integer, Integer> entry = (Map.Entry<Integer, Integer>) it.next();
System.out.println(entry.getKey() + "--->" + entry.getValue());
}
System.out.println("map3END----------------------------------");
Set<Entry<Integer, Integer>> set4 = mapTitleType.entrySet();
for (Iterator<Entry<Integer, Integer>> it = set4.iterator(); it.hasNext();) {
Map.Entry<Integer, Integer> entry = (Map.Entry<Integer, Integer>) it.next();
System.out.println(entry.getKey() + "--->" + entry.getValue());
}
System.out.println("map4END----------------------------------");
//找出各个map中重复数最多的元素
String maxKeyAreaId = null;
String maxKeyDifficult = null;
String maxKeyType = null;
String maxKeyTitleType = null;
int maxValueAreaId = 0;
int maxValueDifficult = 0;
int maxValueType = 0;
int maxValueTitleType = 0;
Map<String,Integer> newMap = new HashMap<String,Integer>();
Iterator<Entry<Integer,Integer>> it1 = mapAreaId.entrySet().iterator();
Iterator<Entry<Integer,Integer>> it2 = mapDifficult.entrySet().iterator();
Iterator<Entry<Integer,Integer>> it3 = mapType.entrySet().iterator();
Iterator<Entry<Integer,Integer>> it4 = mapTitleType.entrySet().iterator();
for(int i=0;i<mapAreaId.size();i++){
Map.Entry entry =(Map.Entry)it1.next();
int value = Integer.parseInt(entry.getValue().toString());
if(value > maxValueAreaId){
maxValueAreaId = value;
maxKeyAreaId = entry.getKey().toString();
}
}
for(int i=0;i<mapDifficult.size();i++){
Map.Entry entry =(Map.Entry)it2.next();
int value = Integer.parseInt(entry.getValue().toString());
if(value > maxValueDifficult){
maxValueDifficult = value;
maxKeyDifficult = entry.getKey().toString();
}
}
for(int i=0;i<mapType.size();i++){
Map.Entry entry =(Map.Entry)it3.next();
int value = Integer.parseInt(entry.getValue().toString());
if(value > maxValueType){
maxValueType = value;
maxKeyType =entry.getKey().toString();
}
}
for(int i=0;i<mapTitleType.size();i++){
Map.Entry entry =(Map.Entry)it4.next();
int value = Integer.parseInt(entry.getValue().toString());
if(value > maxValueTitleType){
maxValueTitleType = value;
maxKeyTitleType =entry.getKey().toString();
}
}
//将重复数最多的元素放入一个新的map中,准备进行比较
newMap.put("areaId,"+maxKeyAreaId, maxValueAreaId);
newMap.put("difficult,"+maxKeyDifficult, maxValueDifficult);
newMap.put("type,"+maxKeyType, maxValueType);
newMap.put("titleType,"+maxKeyTitleType, maxValueTitleType);
//按出现频率从高到底顺序排列,准备进行赋值
List<Map.Entry<String, Integer>> orderList=new ArrayList<Map.Entry<String, Integer>>(newMap.entrySet());
Collections.sort(orderList, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return (o2.getValue()-o1.getValue());
}
});
//赋值
Student stu = new Student(4,9,6,2);
int allScore = 0;
for (Entry<String, Integer> entry : orderList) {
String[] x = entry.getKey().split(",");
if(x[0].equals("areaId")){
if(Integer.parseInt(x[1]) == stu.getAreaId()){
allScore = allScore + entry.getValue();
}
}
if(x[0].equals("difficult")){
if(Integer.parseInt(x[1]) == stu.getDifficult()){
allScore = allScore + entry.getValue();
}
}
if(x[0].equals("type")){
if(Integer.parseInt(x[1]) == stu.getType()){
allScore = allScore + entry.getValue();
}
}
if(x[0].equals("titleType")){
if(Integer.parseInt(x[1]) == stu.getTitleType()){
allScore = allScore + entry.getValue();
}
}
System.out.println(entry.getKey()+"----->"+entry.getValue());
}
stu.setScore(allScore);
System.out.println(stu.getScore());
}
MySort
最新推荐文章于 2022-09-03 15:46:03 发布