51,412
社区成员
发帖
与我相关
我的任务
分享
class Data{
String s1;
String s2;
String s3;
String s4;
String s5;
Type t;
}
enum Type{
OK,GOOD,EXCELLENT;
}
public class Data {
String s1;
String s2;
String s3;
String s4;
String s5;
Type t;
int res;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.s1 == null) ? 0 : this.s1.hashCode());
result = prime * result + ((this.s2 == null) ? 0 : this.s2.hashCode());
result = prime * result + ((this.s3 == null) ? 0 : this.s3.hashCode());
result = prime * result + ((this.s4 == null) ? 0 : this.s4.hashCode());
result = prime * result + ((this.s5 == null) ? 0 : this.s5.hashCode());
this.res = result;
result = prime * result + ((t == null) ? 0 : t.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Data other = (Data) obj;
if (this.s1 == null && other.s1 != null) {
return false;
} else if (!this.s1.equals(other.s1)) {
return false;
}
if (this.s2 == null && other.s2 != null) {
return false;
} else if (!this.s2.equals(other.s2)) {
return false;
}
if (this.s3 == null && other.s3 != null) {
return false;
} else if (!this.s3.equals(other.s3)) {
return false;
}
if (this.s4 == null && other.s4 != null) {
return false;
} else if (!this.s4.equals(other.s4)) {
return false;
}
if (this.s5 == null && other.s5 != null) {
return false;
} else if (!this.s5.equals(other.s5)) {
return false;
}
return true;
}
}
enum Type {
OK, GOOD, EXCELLENT;
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class DataTest {
public static void main(String[] args) {
List<Data> list = new ArrayList<Data>();
for (int i = 0; i < 100; i++) {
Data data = new Data();
data.s1 = String.valueOf(i);
data.s2 = String.valueOf(i);
data.s3 = String.valueOf(i);
data.s4 = String.valueOf(i);
data.s5 = String.valueOf(i);
if (i % 3 == 0) {
data.t = Type.EXCELLENT;
} else if (i % 3 == 1) {
data.t = Type.GOOD;
} else {
data.t = Type.OK;
}
list.add(data);
}
for (int i = 0; i < 100; i++) {
Data data = new Data();
data.s1 = String.valueOf(i);
data.s2 = String.valueOf(i);
data.s3 = String.valueOf(i);
data.s4 = String.valueOf(i);
data.s5 = String.valueOf(i);
if (i % 3 == 0) {
data.t = Type.OK;
} else if (i % 3 == 1) {
data.t = Type.GOOD;
} else {
data.t = Type.EXCELLENT;
}
list.add(data);
}
List<Data> listToMap = removeDuplicateData(list);
Map<Integer, Data> map = new HashMap<Integer, Data>();
for (Data d : listToMap) {
if (map.containsKey(d.res)) {
Data d2 = map.get(d.res);
int c = compareTo(d, d2);
if (c == 1) {
map.put(d.res, d);
}
} else {
map.put(d.res, d);
}
}
List<Data> result = getResult(map);
System.out.println(result.size());
for (Data d : result) {
System.out.println(d.s1 + " | " + d.t.name());
}
}
/**
* 移除重复数据
*
* @param list
* @return
*/
private static List<Data> removeDuplicateData(List<Data> list) {
Set<Data> set = new HashSet<Data>();
set.addAll(list);
List<Data> listnewList = new ArrayList<Data>(set);
return listnewList;
}
/**
* 比较两个类的大小
*
* @param data1
* @param data2
* @return
*/
public static int compareTo(Data data1, Data data2) {
if(data1.t == null && data2.t == null || data1.t == data2.t){
return 0;
}else if(data1.t == null && data2.t != null){
return -1;
}else if(data1.t != null && data2.t == null){
return 1;
}else {
if (data1.t == Type.EXCELLENT) {
return 1;
}
if (data2.t == Type.EXCELLENT) {
return -1;
}
if (data1.t == Type.GOOD) {
return 1;
}
if (data2.t == Type.GOOD) {
return -1;
}
return 0;
}
}
public static List<Data> getResult(Map<Integer, Data> map) {
List<Data> list = new ArrayList<Data>();
for (Map.Entry<Integer, Data> m : map.entrySet()) {
list.add(m.getValue());
}
return list;
}
}