ok, i metodi li ho messi così:
codice:
public <T> List<Book> groupJson(File file, TypeToken<T> typeToken) throws IOException {
List<Book> list;
try (JsonReader reader = new JsonReader(new FileReader(file))) {
list = gson.fromJson(reader, typeToken.getType());
}
return list;
}
public Map<String, Long> countByAuthor(List<Book> booksList) {
Map<String, Long> map = booksList.stream().collect(Collectors.groupingBy(Book::getAuthor, Collectors.counting()));
return map;
}
public Map<String, Long> countByEditor(List<Book> booksList) {
Map<String, Long> map = booksList.stream().collect(Collectors.groupingBy(Book::getEditor, Collectors.counting()));
return map;
}
in verità più che il prezzo, ho un altro problema con questa Map:
codice:
ReadJson jsonRead = new ReadJson();
TypeToken<List<Book>> bookToken = new TypeToken<List<Book>>() {
};
List<Book> booksList = jsonRead.groupJson(new File(UrlAndPath.JSON_LIBRI), bookToken);
Map<String, Long> map = jsonRead.countByAuthor(booksList);
Map<String, Long> mapTest = new HashMap<>();
mapTest.put("thomas mann", 1L);
mapTest.put("clive cussler", 3L);
mapTest.put("stephen king", 6L);
XYChart.Series<String, Number> series1 = new XYChart.Series<>();
series1.setName(title);
for (Map.Entry<String, Long> entry : map.entrySet()) {
String tmpString = entry.getKey();
Long tmpValue = entry.getValue();
XYChart.Data<String, Number> d = new XYChart.Data<>(tmpString, tmpValue);
System.out.println(d);
series1.getData().add(d);
}
barChart.setTitle(title);
barChart.getData().addAll(series1);
vado a riempire una Barchart iterando sulla map.
se uso l'oggetto map, vedo gli autori, ma il valore è sempre zero.
se uso l'oggetto mapTest invece funziona.
come se il valore del json lo leggesse in maniera non corretta.
però, se stampo il valore della serie (d) con l'oggetto map, mi esce questo:
codice:
Data[thomas mann,1,null]
Data[giorgio zarelli,1,null]
Data[jack london,1,null]
quindi in verità sembra corretto!