如何取到我想要的数据

淡青の月 2020-12-29 03:09:09

class Data{
String s1;
String s2;
String s3;
String s4;
String s5;
Type t;
}
enum Type{
OK,GOOD,EXCELLENT;
}

我现在有一个List<Data>,其中有大量的数据,现在要求把其中s1,s2,s3,s4,s5相同的数据按照Type的EXCELLENT>GOOD>OK排序取第一个,剩下的过滤掉,然后返回List<Data>。
这里把s1,s2,s3,s4,s5相同的记为S(n)
假设我有如下数据
(S(0),OK)
(S(0),GOOD)
(S(0),EXCELLENT)
(S(1),OK)
(S(1),EXCELLENT)
(S(2),OK)
(S(2),GOOD)
(S(3),OK)
过滤完应该剩下
(S(0),EXCELLENT)
(S(1),EXCELLENT)
(S(2),GOOD)
(S(3),OK)
可以用流解决吗,有没有比较高效的方法,求助大神。
...全文
1601 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
淡青の月 2020-12-30
  • 打赏
  • 举报
回复
感谢大佬的解答!
KeepSayingNo 2020-12-30
  • 打赏
  • 举报
回复
stream.order(填你的排序方式可以用Data::Type).findFirst()
  • 打赏
  • 举报
回复
有两种方案
一、把数据存入数据库,然后用sql进行过滤查询,sql不会百度一下就行。
二、重写Data类,自己写算法解决。
第二种方案参考如下。

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;
}

}

51,412

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧