package org.mo.common.file7; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map.Entry; import java.util.Random; import org.junit.After; import org.junit.Before; import org.junit.Test; public class ListDemo { private final static int MAX = 20; private final static int MIN = 10; private List<Student> students; private HashMap<Student, Integer> result; @Before public void init() { students = new ArrayList<>(); result = new HashMap<Student, Integer>(); for (int i = 0; i < 10; i++) { Student e = new Student(); Random random = new Random(); int s = random.nextInt(MAX) % (MAX - MIN + 1) + MIN; e.setId(s); e.setName("name" + s); students.add(e); } } @Test public void test() { for (int i = 0; i < students.size(); i++) { if (result.containsKey(students.get(i))) { Integer student = result.get(students.get(i)); result.put(students.get(i), student + 1); }else{ result.put(students.get(i),1); } } } @After public void after() { System.out.println("所有:"); for (Student student : students) { System.out.println(student); } System.out.println("结果:"); List<Entry<Student, Integer>> list = new ArrayList<Entry<Student, Integer>>(result.entrySet()); Collections.sort(list, new Comparator<Entry<Student, Integer>>() { public int compare(Entry<Student, Integer> o1, Entry<Student, Integer> o2) { return (o1.getValue() - o2.getValue());//翻过来就降序 } }); Iterator<Entry<Student, Integer>> iterator = list.iterator(); while (iterator.hasNext()) { Entry<Student, Integer> next = iterator.next(); System.out.println("数量:" + next.getValue() + "-" + next.getKey()); } } }
package org.mo.common.file7; public class Student { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { //if (obj != null) { if (obj instanceof Student) { Student student = (Student) obj; if (this.getName().equals(student.getName()) && this.getId() == student.getId()) { return true; } else { return false; } } else { return false; } //} else { // return false; //} } @Override protected Student clone() { Student student = null; try { student = (Student) super.clone(); } catch (CloneNotSupportedException e) { throw new RuntimeException(e); } return student; } }
参考并学习了这几个:
http://www.oschina.net/code/snippet_271849_15779
http://blog.csdn.net/zhangerqing/article/details/8193118
http://www.cnblogs.com/czpblog/archive/2012/08/06/2625794.html
拓展知识:
http://blog.csdn.net/mydreamongo/article/details/8917843
可以了解到hibernate,mybaits做缓存的原理
另外关于几篇comparator和Comparable
http://www.blogjava.net/fastunit/archive/2008/04/08/191533.html
上面的这边个说下不错,很明显说出两种不同的情况用法
http://bluelzx.iteye.com/blog/200987
http://perso.ensta-paristech.fr/~diam/java/online/notes-java/data/expressions/22compareobjects.html
覆写后的student bean
package org.mo.common.file7; public class Student implements Comparable<Student> { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (obj != null) { if (obj instanceof Student) { Student student = (Student) obj; if (this.getName().equals(student.getName()) && this.getId() == student.getId()) { return true; } else { return false; } } else { return false; } } else { return false; } } @Override protected Student clone() { Student student = null; try { student = (Student) super.clone(); } catch (CloneNotSupportedException e) { throw new RuntimeException(e); } return student; } @Override public int compareTo(Student o) { int cop = this.id - o.id; if (cop != 0) { return cop; } else { return this.name.compareTo(o.name); } } }
最后总结就是归根于数据结构问题,
http://developer.51cto.com/art/201309/410205_all.htm
我现在也意识到,你呢?