如何按最新日期从arraylist中删除重复项

gfttwv5a  于 2021-06-29  发布在  Java
关注(0)|答案(3)|浏览(293)

我需要根据最新日期删除重复项。

class Stat {

    private String country;
    private String state;
    private String fullname;
    Date indepdate;

    public Stat(String country,String state,String fullname,int indepdate){
        this.country=country;
        this.state=state;
        this.fullname=fullname;
        this.indepdate=indepdate;
    }

   //also getters and setters

    public String toString() {
         return "("+country+","+state+","+fullname+","+indepdate+")";
    } 
}
ArrayList<Stat> stats  =new  ArrayList();

Stats.add(new Stat("USA", "Florida", "John Jones", 5/1/2020));
Stats.add(new Stat("USA", "Florida", "Jeff Cane", 4/1/2016));
Stats.add(new Stat("USA", "California", "Lisa Smith", 3/1/2000));
Stats.add(new Stat("Germany", "Florida", "Tom Joseph", 5/1/2019));
Stats.add(new Stat("Germany", "Florida", "Chris Richard", 5/1/2018));
Stats.add(new Stat("Germany", "California", "Nancy Diaz", 4/3/2015));

我需要删除重复的国家,只保留最新日期的国家。
列表应如下所示:

USA, florida, John James, 5/1/2020
Germany,Florida,Tom Joseph, 5/1/2019

到目前为止,我有以下几点:

Map<String, Map<String, List<Stat>> 
           groupByCountryAndCity = stats.
             stream().
               collect(
                    Collectors.
                        groupingBy(
                            Stat::getCountry,
                            Collectors.
                                groupingBy(
                                     Stat::getIndepdate     
                                          )
                                   )
                     );
roqulrg3

roqulrg31#

你可以用 LocalDate 为了 indepdate 现场。

import java.time.LocalDate;

public class Stat {

    private String country;
    private String state;
    private String fullname;
    private LocalDate indepdate;

    // ...
}

然后使用 LocalDate.of() 方法: LocalDate.of(2020, 5, 1) 试试这个:

List<Stat> output = stats.stream()
        .collect(Collectors.toMap(Stat::getCountry, Function.identity(),
                BinaryOperator.maxBy(Comparator.comparing(Stat::getIndepdate)),
                () -> new TreeMap<>(Comparator.comparing(String::toString).reversed())))
        .values().stream().collect(Collectors.toList());

输出:

[(USA,Florida,John Jones,2020-05-01), (Germany,Florida,Tom Joseph,2019-05-01)]
mrphzbgm

mrphzbgm2#

您可以使用以下方法 Collectors 方法: groupingBy(Function<> classifier, Collector<> downstream) collectingAndThen(Collector<> downstream, Function<> finisher) maxBy(Comparator<> comparator) ```
List stats = Arrays.asList(
new Stat("USA", "Florida", "John Jones", "5/1/2020"),
new Stat("USA", "Florida", "Jeff Cane", "4/1/2016"),
new Stat("USA", "California", "Lisa Smith", "3/1/2000"),
new Stat("Germany", "Florida", "Tom Joseph", "5/1/2019"),
new Stat("Germany", "Florida", "Chris Richard", "5/1/2018"),
new Stat("Germany", "California", "Nancy Diaz", "4/3/2015") );

Map<String, Stat> result = stats.stream()
.collect(Collectors.groupingBy(Stat::getCountry,
Collectors.collectingAndThen(
Collectors.maxBy(Comparator.comparing(Stat::getIndepdate)),
Optional::get)));

result.values().forEach(System.out::println);

class Stat {
private final String country;
private final String state;
private final String fullname;
private final LocalDate indepdate;

public Stat(String country, String state, String fullname, String indepdate){
    this.country = country;
    this.state = state;
    this.fullname = fullname;
    this.indepdate = LocalDate.parse(indepdate, DateTimeFormatter.ofPattern("M/d/u"));
}

public String getCountry() {
    return this.country;
}
public String getState() {
    return this.state;
}
public String getFullname() {
    return this.fullname;
}
public LocalDate getIndepdate() {
    return this.indepdate;
}

@Override
public String toString() {
    return "("+country+","+state+","+fullname+","+indepdate+")";
}

}

输出

(USA,Florida,John Jones,2020-05-01)
(Germany,Florida,Tom Joseph,2019-05-01)

rnmwe5a2

rnmwe5a23#

yu可以使用收集器提供的流的减少。像这样的东西:

stats.stream().collect(
        Collectors.groupingBy(Stat::getCountry,reducing(Comparator.comparing(Stat::getIndepDate))));

相关问题