我如何使用收集器将DAO列表转换为 Map<String, List<Pojo>>
daoList看起来像这样:
[0] : id = "34234", team = "gools", name = "bob", type = "old"
[1] : id = "23423", team = "fool" , name = "sam", type = "new"
[2] : id = "34342", team = "gools" , name = "dan", type = "new"
Run Code Online (Sandbox Code Playgroud)
我想对“团队”属性进行分组,并为每个团队提供一个列表,如下所示:
"gools":
["id": 34234, "name": "bob", "type": "old"],
["id": 34342, "name": "dan", "type": "new"]
"fool":
["id": 23423, "name": "sam", "type": "new"]
Run Code Online (Sandbox Code Playgroud)
Pojo看起来像这样:
@Data
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PUBLIC)
public class Pojo{
private String …Run Code Online (Sandbox Code Playgroud) 我正在为公司开发一个网站,并且使用Spring作为后端。现在有一种情况,我需要两次使用一种Utils方法,但是要使用不同的DAO。
为了避免代码重复,我想知道如何使用Java泛型来使该方法可用于两种情况。该方法仅计算两个DAO共有的字段之一。
使用方法:
SeverityCount calculateSeveritiesCount(List<?> events){
if(null == events){
return new SeverityCount();
}
if(events.get(1) instanceof EventDAO){
events = (List<EventDAO>)events;
}
else if (events.get(1) instanceof EventsByAreaDAO) {
events = (List<EventsByAreaDAO>)events;
}
Map<String, Long> severityCountMap = events.stream().collect(
Collectors.groupingBy(
EventDAO::getSeverity, //It should be EventDAO or EventsByAreaDAO. both has severity field.
Collectors.counting())
);
return mapper.convertValue(severityCountMap, SeverityCount.class);
}
Run Code Online (Sandbox Code Playgroud)
活动DAO:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "events")
public class EventDAO {
@Id @Column(name = "uid")
private String uID;
private String date;
private String severity;
}
Run Code Online (Sandbox Code Playgroud)
区域DAO: …
尝试从文件中过滤特定单词时,我遇到了一些问题,并将它们写入新文件.我想要做的只是写'&'之后的单词直到第一个数字.
例如(这是我正在阅读的文件的内容):
& some 12 test1 test2
$ thisword 4 no no no no
Run Code Online (Sandbox Code Playgroud)
对于上面的输入,我想在一个新文件中写入some和thisword.
我的代码工作正常,但它不是只打印那些单词,而是打印垃圾.
int main (argc,argv)
int argc;
char *argv[];
{
int inpfd,outpfd,n;
int i=0;
char tmp[2],buff[BUFFSIZE]; //This is our buffer
//Open the output file of ispell
inpfd = open("outputfile.txt",O_RDONLY);
//Check if open command failed
if(inpfd == -1) {
printf("Failed to open file");
exit(1);
}
//Here we are reading from output file
read(inpfd,buff,999);
buff[999] = '\0';
close(inpfd);
outpfd = open("w.txt",O_WRONLY);
if(outpfd == -1) …Run Code Online (Sandbox Code Playgroud) generics ×2
java ×2
c ×1
collectors ×1
java-8 ×1
java-stream ×1
linux ×1
list ×1
spring ×1
system-calls ×1