如何将实体收集为 “Map>”?

huangapple 未分类评论47阅读模式
英文:

How to collect entities as "Map<DateRange, List<Entity>>"?

问题

我需要根据日期范围收集实体记录,基于一个属性的值。如果类型属性的值在日期上是连续相同的,那么应该按日期分组。由于日期是连续的,所以应该进行排序。即使记录的类型属性值不同,其余记录也应该在同一天内。请参考可视化表示。我尝试过以下代码:

Map<LocalDate, List<Entity>> collection = entities.stream()
    .collect(Collectors.groupingBy(Entity::getDate))
    .entrySet().stream()
    .sorted(Map.Entry.comparingByKey())
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));

在我的实现中,我只能按日期进行收集,但我想按DateRange进行收集。我想实现类似于这样的功能:

Map<DateRange, List<Entity>> collection = entities.stream()...// 实现部分

Entity

[
    {
        "id": 1,
        "date": "2020-01-01",
        "type": 5
    },
    {
        "id": 2,
        "date": "2020-01-01",
        "type": 5
    },
    {
        "id": 1,
        "date": "2020-01-02",
        "type": 5
    },
    {
        "id": 2,
        "date": "2020-01-02",
        "type": 5
    },
    .
    .
    .
]

示例

日期范围根据类型属性的值进行更改。例如,如果所有日期的类型为5,那么所有记录应该在一个范围内。假设只有一年的记录,我假设只有两个唯一的id值(id=1、id=2),那么在收集中我应该实现这样的结果:

[{
    "From: 2020-01-01, To: 2020-12-31": [{
            "record1": 
                {
                    "id": 1,
                    "type": "5"
                },
            "record2": 
                {
                    "id": 2,
                    "type": "5"
                }
    }]
}]

另一个示例

如果除了'2020-02-01'之外的所有日期的类型为5,并且在'2020-02-01'上id=1的类型为6,则范围应该如下所示。我仍然假设只有一年的记录,而且只有两个唯一的id值(id=1、id=2)。

[
{
    "From: 2020-01-01, To: 2020-01-31": [{
            "record1": 
                {
                    "id": 1,
                    "type": "5"
                },
            "record2": 
                {
                    "id": 2,
                    "type": "5"
                }
        }],
},
{
    "From: 2020-02-01, To: 2020-02-01": [{
            "record1": 
                {
                    "id": 1,
                    "type": "6"
                },
            "record2": 
                {
                    "id": 2,
                    "type": "5"
                }
        }],
},
{
    "From: 2020-02-02, To: 2020-12-31": [{
            "record1": 
                {
                    "id": 1,
                    "type": "5"
                },
            "record2": 
                {
                    "id": 2,
                    "type": "5"
                }
       }]
}
]
英文:

I need to collect an entity records as from date to date, based on one attribute's value. If the value of type attribute is sequentially same based on date, it should be grouped by date. Since date mentioned as sequentially, should be ordered. Even if the value of type attribute of a record is different, rest of the records also should be under the same day. See the visual representation. I've tried this;

Map&lt;LocalDate, List&lt;Entity&gt;&gt; collection = entities.stream().collect(Collectors.groupingBy(Entity::getDate))
                .entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -&gt; oldValue, LinkedHashMap::new));

In my implementation, I am able to only collect by dates, but I want to collect by DateRange. I want to achieve some thing like this;

Map&lt;DateRange, List&lt;Entity&gt;&gt; collection = entities.stream()...// implementation

Entity

[
    {
        &quot;id&quot;: 1,
        &quot;date&quot;: &quot;2020-01-01&quot;,
        &quot;type&quot;: 5
    },
    {
        &quot;id&quot;: 2,
        &quot;date&quot;: &quot;2020-01-01&quot;,
        &quot;type&quot;: 5
    },
    {
        &quot;id&quot;: 1,
        &quot;date&quot;: &quot;2020-01-02&quot;,
        &quot;type&quot;: 5
    },
    {
        &quot;id&quot;: 2,
        &quot;date&quot;: &quot;2020-01-02&quot;,
        &quot;type&quot;: 5
    },
    .
    .
    .
]

Example

Date range changes based on the value of the type attribute. For example, if type=5 for all dates, so all records should be in one range. Let's say there are only records for one year and I'm assuming there are only two unique id value(id=1, id=2), so in collection I should achieve this;

[{
    &quot;From: 2020-01-01, To: 2020-12-31&quot;: [{
            &quot;record1&quot;: 
                {
                    &quot;id&quot;: 1,
                    &quot;type&quot;: &quot;5&quot;
                },
            &quot;record2&quot;: 
                {
                    &quot;id&quot;: 2,
                    &quot;type&quot;: &quot;5&quot;
                }
    }]
}]

Another example

If the type=5 for all dates except '2020-02-01' and in '2020-02-01' type=6 for the id=1, then ranges should be like the below. I'm still assuming, there are records only for one year and there are only two unique id value(id=1, id=2).

[
{
    &quot;From: 2020-01-01, To: 2020-01-31&quot;: [{
            &quot;record1&quot;: 
                {
                    &quot;id&quot;: 1,
                    &quot;type&quot;: &quot;5&quot;
                },
            &quot;record2&quot;: 
                {
                    &quot;id&quot;: 2,
                    &quot;type&quot;: &quot;5&quot;
                }
        }],
},
{
    &quot;From: 2020-02-01, To: 2020-02-01&quot;: [{
            &quot;record1&quot;: 
                {
                    &quot;id&quot;: 1,
                    &quot;type&quot;: &quot;6&quot;
                },
            &quot;record2&quot;: 
                {
                    &quot;id&quot;: 2,
                    &quot;type&quot;: &quot;5&quot;
                }
        }],
},
{
    &quot;From: 2020-02-02, To: 2020-12-31&quot;: [{
            &quot;record1&quot;: 
                {
                    &quot;id&quot;: 1,
                    &quot;type&quot;: &quot;5&quot;
                },
            &quot;record2&quot;: 
                {
                    &quot;id&quot;: 2,
                    &quot;type&quot;: &quot;5&quot;
                }
       }]
}
]

</details>


huangapple
  • 本文由 发表于 2020年5月29日 23:30:58
  • 转载请务必保留本文链接:https://java.coder-hub.com/62089497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定