首先,我有这个对象,我称之为 myObject ;
public class MyObject{
private google.protobuf.Timestamp timestamp;
private String description;
}
然后我有此列表:
List<MyObject> myList = new ArrayList<>();
现在让我们想象 myList
包含500个项目。我想要的是消除同一小时内发生的重复(相同的描述)。
因此,在同一小时内,列表中不应在列表中存在两个不同的项目。如果他们这样做,我们只想保留一个并删除另一个。
示例:
如果列表包含以下两个项目:
06-07-2022T01:30:00,“一些随机说明”
and 06-07-2022T01:35:00,“一些随机描述“
然后我们要删除其中一个,因为它们具有相同的描述并且在同一小时内。
但是,如果我们有这样的话:
06-07-2022T01:30:00,“一些随机说明”
和 06-07-07-2022T03:20:00,“一些随机说明”
然后,我们不想删除任何一个,因为它们不在同一小时内。
我该怎么做?
First of all, I have this object that I call MyObject;
public class MyObject{
private google.protobuf.Timestamp timestamp;
private String description;
}
Then I have this list:
List<MyObject> myList = new ArrayList<>();
Now let's imagine that myList
contains 500 items. What I want, is to eliminate duplicates (identical descriptions) that occur within the same hour.
So two different items with identical descriptions should not both exist in the list within the same hour. If they do, we want to only keep one and delete the other.
Example:
If the list contains the following two items:
06-07-2022T01:30:00, "some random description"
and 06-07-2022T01:35:00, "some random description"
Then we want to delete one of them because they have identical description and are within the same hour.
But if we have this:
06-07-2022T01:30:00, "some random description"
and 06-07-2022T03:20:00, "some random description"
Then we don't want to delete any of them as they are not within the same hour.
How do I do that?
发布评论
评论(4)
根据您在评论中给出的澄清,我使用了
localdateTime
来简化示例条目并检索小时,但是我敢肯定google.protobuf.timestamp 可以转换为适当的日期并提取其小时。
为了根据描述,日期和小时仅保留一个对象,我在POJO中添加了一个助手方法,以获取这些字段的串联,然后按其结果值进行分组以获取
MAP
每个键的位置(描述,日期和小时)只有一个对象关联。最后,我将MAP
的值收集到list
中。POJO类
是用于测试代码
输出
Based on the clarifications you've given in the comments I've used a
LocalDateTime
to simplify the sample entry and retrieve the hour, but I'm sure thatgoogle.protobuf.Timestamp
can be converted to a proper date and extract its hour.To keep only one object according to description, date and hour, I've added a helper method to your POJO to get a concatenation of these fields and then group by their result value in order to get a
Map
where to each key (description, date and hour) there is only one object associated. Lastly, I've collected theMap
's values into aList
.POJO Class
Here is a link to test the code
https://www.jdoodle.com/iembed/v0/sZV
Output
一个安静的简单解决方案将是一个hashmap。您将描述用作钥匙和时间戳作为值。因此,您总是只保存最后一个时间戳将其保存到给定的描述并自动覆盖它。
如果要握住对象,我只会按日期对列表进行排序,然后填写hashmap并转换hashmap以再次列出。它不是最好的表现,但很容易。您可以通过函数 Java对功能样式进行分类
A quiet simple solution would be a HashMap. You use description as key and timestamp as value. So you always save only the last timestamp to given description and overwrite it automaticly.
If you want to hold your Object I would just sort the list by date, then fill in a HashMap and transform the HashMap to List again. It has not the best Performance, but its easy. You can Sort by Date with functional Java sorting a Collection in functional style
您可以定义一个平等计算类的平等(或在
myobject
类中进行,具体取决于其实际代表的内容),并根据平等定义使用它来查找唯一值。在这种情况下,平等将意味着:相同的描述和相同的时间戳,每小时精度。这是一个示例(可能需要一些调整,只是一个概念演示):
You could define an equality calculating class (or do it in the
MyObject
class, depending on what it actually represents) and use it to find unique values based on the equality definition. In this case equality would mean: same description and same timestamp with hourly precision.Here's an example (might need some tweaking, just a concept presentation):
添加
等于
&amp;HashCode
用等于
的MyObject类的方法具有以下逻辑:在这里,基本上,我正在检查2个对象是否具有相同的小时&amp;日期&amp;如果是这样,请忽略另一个对象。
一旦这样做,您就可以使用:
请注意,您可以使用通过编辑器生成的
HashCode
的默认实现。DISTICT()
流的方法正在检查是否有等于
&amp;HashCode
可用于基础流类。注意:您可以将
等于
进行检查日,日期,月,年等,以验证确切的日期。Add
equals
&hashcode
method to your MyObject class withequals
has some logic like below:Here, basically I am checking if 2 objects has same hour & date & if so, just ignore another object.
Once you do that, you can just use :
Please note, you can use default implementation of
hashCode
generated via your editor for this case.distinct()
method of stream is checking if you haveequals
&hashcode
available for underlying streams class.Note: you can extend
equals
to check day , date, month, year etc. for verifying exact date.