java - 修改 ArrayList 中的重复项

标签 java arraylist

我有一个名为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/

相关文章:

java - 为什么 Date 对象不代表我的 JVM 时区中的日期?

Java-在构造函数中使用实例变量循环遍历数组

java - 将整数数组列表转换为 boolean 值数组列表

java - 使用 Arraylist 在 Java 中创建登录系统

java - 遍历元素,获取 url,将 url 放入列表中。 Selenium java

java - 如何在 2 个线程的 ArrayList 中避免 NullPointerException

java - odbc excel java

java - 总和为 A 的 N 个整数的不同组数

java - 如何仅使用注释读取 Controller 中的属性文件? Spring MVC

c# - 如何找到 Bool ArrayList 比较其 Bool 值的结果?