0

My Class:

public class Proposal {
  Date createDate;
  // ...
}

We have two objects java.util.Date and know for sure that the objects of this collection are between these two dates. How to divide these objects by day between these dates, so that I can get a list of these objects by date?

SDJ
  • 4,083
  • 1
  • 17
  • 35

2 Answers2

3

Something like this?

public Map<String, Set<Proposal>> groupProposals(Iterable<Proposal> proposals) {
    Map<String, Set<Proposal>> map = new HashMap<>();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    for (Proposal p : proposals) {
        String key = sdf.format(p.getCreateDate());
        if (!map.containsKey(key)) {
            map.put(key, new HashSet<>());
        }
        map.get(key).add(p);
    }
    return map;
}
Jeff I
  • 396
  • 1
  • 2
  • 13
  • 1
    working with streams and mapping lately can remove For loop and just return `proposals.stream().collect(Collectors.groupingBy(p->sdf.format(p.getCreateDate()),Collectors.mapping(i-> i, Collectors.toSet())));` – mavriksc Jan 15 '19 at 22:20
0

Something like this to get one of the lists. List<Object> inDateList = list.stream().filter(o-> startDate< o.createDate && o.createDate< endDate).collect(Collectors.toList());

then List<Object> outDateList = new ArrayList<>(list); outDateList.removeAll(inDateList);

EDIT Just to clarify on my note above.

public Map<String, Set<Proposal>> groupProposals(Iterable<Proposal> proposals) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return proposals.stream()
//GroupingBy creates the Map<Key, Collection<Something>>
.collect(Collectors.groupingBy(p->sdf.format(p.getCreateDate()),//Creates the Key and buckets
        Collectors.mapping(i-> i, Collectors.toSet()))); //what kind of buckets do you want. 
}
mavriksc
  • 1,130
  • 1
  • 7
  • 10
  • You did not understand. I need to break this list by day. Suppose date1 = 01/01/2010; date2 = 01/01/2011. I need to divide this list into 365 (days). – sadrutdin.zainukov Jan 15 '19 at 21:11