gpt4 book ai didi

java - Java 8 是否可以一步创建一个列表或任何其他分组、排序和计数的数据结构?

转载 作者:行者123 更新时间:2023-12-02 01:27:16 25 4
gpt4 key购买 nike

我有一个具有以下结构的文件:

a, City1, 2013-09-05 14:08:15b, City2, 2015-10-08 16:10:15c, City2, 2010-09-05 14:08:15d, City1, 2011-09-05 14:08:15

It's comma separated values and lines separated with end of line character.

I need to create a data structure in Java 8 where I have the rows grouped by city and within each such group sort by date in ascending order and counting the amount of rows within each group.

I tried:

  1. Create a List<Row> from the file
  2. Create a Map<String, List<Row>> which is grouped by city and sort by date withing each group
  3. Create a Map<String, Long> for grouping by city and amount of rows

This is code I've tried:

public PhotoResponse processFile()  {
//read each line of the file and create a new object PhotoIn for each one
List<PhotoIn> lista = null;
try {
lista = Files.lines(Paths.get(file))
.map(line -> line.split(","))
.map(photo -> new PhotoIn(photo[0].substring(0, photo[0].lastIndexOf(".")), photo[0].substring(photo[0].lastIndexOf(".") + 1 ), photo[1].trim(), parseDate(photo[2]), index++))
.collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
return generateOutput(lista);
}


private PhotoResponse generateOutput(List<PhotoIn> photos) {

//Grouping photos by city
Map<String, List<PhotoIn>> photosByCity = photos.stream().collect(Collectors.groupingBy(PhotoIn::getCity));

//Sorting photos by date into each city
photosByCity.values().forEach(list -> list.sort(Comparator.comparing(PhotoIn::getDate)));

//Grouping photos by city and amount
Map<String, Long> numeroPorCiudades = photos.stream().collect(Collectors.groupingBy(PhotoIn::getCity, Collectors.counting()));

List<PhotoOut> photoOutList = new ArrayList<PhotoOut>();

photosByCity.forEach((key, list) -> {
String digits = Integer.toString(Long.toString(numeroPorCiudades.get(key)).length());
counter = 1;
list.forEach(photoIn -> {
photoOutList.add(new PhotoOut(photoIn.getName() + "." + photoIn.getExtension(), key + String.format("%0" + digits + "d", counter++) + "." + photoIn.getExtension(), photoIn.getIndex()));
});
});
return sortOutput(photoOutList);
}

我正在解决这个问题,但我正在寻找一种更好、更有效的方法来使用 Java 8 来解决这个问题。是否可以一步完成这 3 个步骤?。我需要的是将所有信息分组到一个数据结构中。

最佳答案

使用该模型:

class Model {
private String title;
private String city;
private LocalDateTime date;
}

可以这样完成:

List<Model> list = getFromFile();

Comparator<Model> comparator = Comparator.comparing(Model::getDate);

//grouping the list by City with lists sorted by date
Map<String, List<Model>> map = list.stream()
.collect(
Collectors.groupingBy( //grouping the list by City with lists
m -> m.getCity(),
Collectors.collectingAndThen(Collectors.toList(), l->{
l.sort(comparator);
return l;
})
)
);

//getting another map with counts
Map<String, Object> countMap = map.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().size()));

关于java - Java 8 是否可以一步创建一个列表或任何其他分组、排序和计数的数据结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56723810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com