我有如下输入文件,input.txt
companyname,emailid,extenno,lastupdatedgmts
Dell,jcrshan@gmail.com,985553434,2018-06-13 23:30:52.12
Dell,jcrshan@gmail.com,85553434,2018-06-14 23:30:52.12
IBM,ramesh@gmail.com,785553434,2018-06-15 23:30:52.12
UTO,benny@gmail.com,785553434,2018-06-16 23:30:52.12
我想对上述输入文件执行以下操作,
1. sort the above file using emailed (second column) with ascending and lastupdatedgmts (fourth column) with descending
2.remove duplicate based on emailid (second column)
预期输出:
UTO,benny@gmail.com,785553434,2018-06-16 23:30:52.12
Dell,jcrshan@gmail.com,985553434,2018-06-14 23:30:52.12
IBM,ramesh@gmail.com,785553434,2018-06-15 23:30:52.12
能请你帮忙吗?
答案 0 :(得分:0)
public static void main(String[] args) {
String input = "companyname,emailid,extenno,lastupdatedgmts\n"
+ "\n"
+ "Dell,jcrshan@gmail.com,985553434,2018-06-13 23:30:52.12\n"
+ "Dell,jcrshan@gmail.com,85553434,2018-06-14 23:30:52.12\n"
+ "IBM,ramesh@gmail.com,785553434,2018-06-15 23:30:52.12\n"
+ "UTO,benny@gmail.com,785553434,2018-06-16 23:30:52.12\n";
List<String> list = Arrays.asList(input.split("\n"));
list = list.subList(2, list.size()).stream()
.collect(Collectors.toMap(s->key(s), s->s, (s1,s2) -> s2)).values().stream()
.collect(Collectors.toList());
list.sort((s1,s2)->key(s1).compareTo(key(s2)));;
System.out.println(list);
}
private static String key(String s) {
return s.split(",")[1];
}