Skip to content

Instantly share code, notes, and snippets.

@arhitiron
Last active February 7, 2017 14:07
Show Gist options
  • Save arhitiron/50633d97cceacc3817327a6bd8ae3eaa to your computer and use it in GitHub Desktop.
Save arhitiron/50633d97cceacc3817327a6bd8ae3eaa to your computer and use it in GitHub Desktop.
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;
public class PlayingStream {
static class A {
public Date date;
public String rfid;
public A(Date date, String rfid) {
this.date = date;
this.rfid = rfid;
}
public Date getDate() {
return date;
}
public String getRfid() {
return rfid;
}
}
public static void main(String[] args) {
Date date1 = new Date();
Date date2 = new Date(1420070400000L);
Date date3 = new Date(1451606400000L);
Map<LocalDate, Set<String>> phoneBook = (new ArrayList<A>(){
{
add(new A(date1, "test1"));
add(new A(date1, "test2"));
add(new A(date1, "test3"));
add(new A(date2, "test4"));
add(new A(date2, "test5"));
add(new A(date3, "test6"));
}
}).stream()
.collect(Collectors.groupingBy(A::getDate,
Collectors.mapping(A::getRfid, Collectors.toSet())))
.entrySet()
.stream()
.collect(Collectors.toMap(e -> toLocalDate(e.getKey()), Map.Entry::getValue));
System.out.println(phoneBook);
}
public static LocalDate toLocalDate(Date date) {
ZoneId defaultZoneId = ZoneId.systemDefault();
Instant instant = date.toInstant();
return instant.atZone(defaultZoneId).toLocalDate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment