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 students;
private HashMap result;
@Before
public void init() {
students = new ArrayList<>();
result = new HashMap();
for (int i = 0; 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
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> list = new ArrayList>(result.entrySet());
Collections.sort(list, new Comparator>() {
public int compare(Entry o1, Entry o2) {
return (o1.getValue() - o2.getValue());//翻过来就降序
}
});
Iterator> iterator = list.iterator();
while (iterator.hasNext()) {
Entry 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;
}
}
参考并学习了这几个:
拓展知识:
可以了解到hibernate,mybaits做缓存的原理
另外关于几篇comparator和Comparable
上面的这边个说下不错,很明显说出两种不同的情况用法
覆写后的student bean
package org.mo.common.file7;
public class Student implements Comparable {
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);
}
}
}
最后总结就是归根于数据结构问题,
我现在也意识到,你呢?