如何根据特定列对文件中的重复项进行排序和删除

时间:2018-06-25 13:39:31

标签: java java-8 java-7

我有如下输入文件,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

能请你帮忙吗?

1 个答案:

答案 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];
}