标签:blog http io ar os sp for java on
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; 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 < 10000; 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<Map.Entry<Student, Integer>> list = new ArrayList<Map.Entry<Student, Integer>>(result.entrySet()); Collections.sort(list, new Comparator<Map.Entry<Student, Integer>>() { public int compare(Map.Entry<Student, Integer> o1, Map.Entry<Student, Integer> o2) { return (o2.getValue() - o1.getValue()); } }); Iterator<Entry<Student, Integer>> iterator = list.iterator(); while (iterator.hasNext()) { Entry<Student, Integer> next = iterator.next(); System.out.println("数量:" + next.getKey() + "-" + next.getValue()); } } }
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做缓存的原理
标签:blog http io ar os sp for java on
原文地址:http://my.oschina.net/moziqi/blog/355073