gpt4 book ai didi

java - 修改 ArrayList 中的重复项

转载 作者:行者123 更新时间:2023-12-01 18:19:05 24 4
gpt4 key购买 nike

我有一个名为Feeds 的类,其中包含许多成员变量。其中有String类型的变量date。我实现了 Feed 的 ArrayList 并按顺序添加对象。

我想要做的是搜索具有相同日期String的对象,如果出现次数超过1,则重复日期String将被设置为"" (空)但是您仍然拥有一个完整保存该日期字符串的对象。

类似这样的事情:

Object1 (date : "01-01-2015");
Object2 (date : "01-01-2015");
Object3 (date : "04-01-2015");

//after the required code

Object1 (date : "01-01-2015");
Object2 (date : "");
Object3 (date : "04-01-2015");

List<Feeds> mFeeds = new ArrayList<Feeds>();
//add objects to list
mFeeds.add(...);

for(Feeds f : mFeeds){
//search for objects that have the same date
//skip the first repeatable and make the rest empty ""
}

最佳答案

您可以使用 HashSet<String>查明您是否已经遇到该日期:

Set<String> dups = new HashSet<> ();
for(Feeds f : mFeeds) {
if (dups.contains(f.getDate())
f.setDate(null); // this date already appeared in the list, so set it to null
else
dups.add(f.getDate()); // this is the first occurrence of this date
}

关于java - 修改 ArrayList 中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28085153/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com