简体   繁体   English

Java-我有一个 ArrayList 包含对象,一次出现后我希望将 object 添加到另一个列表

[英]Java-I have an ArrayList contains objects, after one occurrence I want the object is added to another list

i have this ArrayList ID that contains objects:我有这个包含对象的 ArrayList ID:

0
10
10
9
8
3
11
6
10
1
7
13
7
7

After one occurrence of integer, i want that integer is added to another list (example visited) and the checking process stop.在一次出现 integer 之后,我希望将 integer 添加到另一个列表(访问示例)并且检查过程停止。 How to solve this?如何解决这个问题? Must I change the type ArrayList ID to another java collection?我必须将类型 ArrayList ID 更改为另一个 java 集合吗?

I think you are asking for all the unique values to be in another collection.我认为您要求所有独特的价值都在另一个集合中。 If so, do this:如果是这样,请执行以下操作:

    List<Integer> list = Arrays.asList(0, 10, 10, 9, 8, 3, 11, 6, 10, 1, 7, 13, 7, 7);
    Set<Integer> set = new HashSet<Integer>(list);
    System.out.println(set); // Outputs "[0, 1, 3, 6, 7, 8, 9, 10, 11, 13]"

This works because:这是有效的,因为:

  1. Sets are guaranteed to contain only unique values保证集合只包含唯一值
  2. Adding the same value again has no effect再次添加相同的值没有效果
  3. Passing a collection to the constructor of a collection adds all elements from the parameter to the new collection将集合传递给集合的构造函数会将参数中的所有元素添加到新集合
import java.util.HashSet;

public class Test {

    private static final HashSet<Integer> onetimevisit = new HashSet<Integer>();
    private static final HashSet<Integer> alreadyVisited = new HashSet<Integer>();

    public static void addVisitor(Integer visitorId){
        if(onetimevisit.contains(visitorId))
            alreadyVisited.add(visitorId);
        else
            onetimevisit.add(visitorId);
    }
}

If I am reading your question correctly, you want a collection containing unique values?如果我正确阅读了您的问题,您想要一个包含唯一值的集合吗? If that is the case, you should store these integers in a Set instead of a List to begin with.如果是这种情况,您应该将这些整数存储在 Set 而不是 List 中。 Sets contain unique values, and adding the same element multiple times has no effect.集合包含唯一的值,多次添加相同的元素没有效果。 If order is important in your collection, you can use an ordered Set, eg a LinkedHashSet.如果顺序在您的集合中很重要,您可以使用有序集合,例如 LinkedHashSet。

If you cannot change the ArrayList to a Set (because it is passed to your code in that form), you can build a set from the ArrayList, eg如果您无法将 ArrayList 更改为 Set(因为它以该形式传递给您的代码),您可以从 ArrayList 构建一个集合,例如

Set<Integer> uniqueCollection = new HashSet<Integer>(myArrayList);

... which will only hold unique values out of your List. ...它只会保留您列表中的唯一值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以有一个类型为object的ArrayList,其中包含指向Java中对象的指针的列表吗? - Can I have an ArrayList of type object that has a list of pointers to objects in Java? 如何基于Java中对象的一个​​成员变量从数组列表中删除另一个数组列表中存在的对象? - How do I remove objects from an arraylist which are present in another arraylist based on one member variable of the object in Java? JAVA-我不想点击我的应用程序使其他应用程序失去焦点 - JAVA-I don't want clicking on my app make other apps lose focus 检查对象列表是否包含Java中的另一个对象 - Check if a list of objects contains another object in Java Java数组列表 <Object> 如果对象具有相同的属性值,则要合并 - Java Arraylist<Object> want to merge the objects if they have the same property value 您好,我有一个列表视图,其中包含要使用onitemclick方法打开另一个列表的元素列表 - hi i have a list view that contains list of elements i want to use onitemclick method to open another list 迭代我在列表中的另一个 Object 中的对象列表 - Iterating over List of Objects inside another Object i have in the List 为什么我的 ArrayList 的 object 在我添加对象后显示为空? - Why is my ArrayList of object showing empty when i have added objects to it? 如何在 Android Studio (Java) 中序列化一个还包含两个 ArrayList 对象的对象? - How do I serialize an object that also contains two ArrayList of objects in Android Studio (Java)? 我有一个 object 公司和员工对象列表,我想创建一个公司对象列表,其中将包含单个员工 Object - I have an object Company and List of Employee objects, I want to create a List of Company objects which will contain the single Employee Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM