简体   繁体   English

从同一个pojo数组创建了两个列表,修改了一个列表,同样的事情也会影响其他列表

[英]Created two Lists from same pojo array, Modifying one List, same thing affects on other list also

I have created two lists object from the same pojo and sorted one of them. 我从同一个pojo创建了两个列表对象,并对其中之一进行了排序。 When I tried to change one list, other lists also got updated. 当我尝试更改一个列表时,其他列表也得到了更新。

List<FilterPojo.Data> filterList = new ArrayList<>();
List<FilterPojo.Data> subFilterList = new ArrayList<>();

If I change the value in filterList , same changes occur in subFilterList 如果我更改值filterList ,发生同样的变化subFilterList

With the limited information that is provided by you, it seems you are creating/populating subFilterList as subList of filterList . 使用您提供的有限信息,您似乎正在将subFilterList创建/填充为subListfilterList When you do that, all changes made in either of the list will be reflected in other. 当您这样做时,在任一列表中所做的所有更改都将反映在其他列表中。

This happens because List.subList() , returns a view of the list, so modifications to the original list will be reflected in the sub-list. 发生这种情况是因为List.subList()返回列表的视图,因此对原始列表的修改将反映在子列表中。 As suggested by others, instead of subList use addAll to populate subFilterList 如其他人所建议的,而不是subList使用addAll填充subFilterList

This could be reference problem. 这可能是参考问题。 Lists maintains their references when items are copied to other list, if you do something like: 将项目复制到其他列表时,列表会保留其引用,如果您执行以下操作:

List<FilterPojo.Data> subFilterList = filterList;

Use addAll method instead, 请改用addAll方法,

subFilterList.clear();
subFilterList.addAll(filterList);

I don't know exactly the context that you are asking. 我不完全了解您所要求的背景。
Your lists are holding the same object. 您的列表包含相同的对象。 For example, in this case p1. 例如,在这种情况下为p1。

Person p1 = new Person();
List<Person> list1 = new ArrayList<Person>();
list1.add(p1);
List<Person> list2 = new ArrayList<Person>();
list2.add(p1);
p1.setName("new name");

Try below 试试下面

        List<String> filterList = new ArrayList<String>();
        List<String> subFilterList = new ArrayList<String>();

        filterList.add("A");
        filterList.add("B");
        filterList.add("C");

        /*subFilterList = filterList; // reference to same object , change will reflect in both
        filterList.add("C");
        System.out.println(filterList);
        System.out.println(subFilterList);*/

        subFilterList.addAll(filterList);   
        filterList.add("C");
        System.out.println(filterList);
        System.out.println(subFilterList);

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

相关问题 从同一个数组创建两个列表,修改一个列表,更改另一个列表 - Created two Lists from same array, Modifying one List, changes the other 在 Java 中创建数组和数组列表是一样的吗? - Is creating an array and an array list the same thing in Java? Kotlin 检查两个列表是否相同,除了一个具有附加元素的列表? - Kotlin check if two lists are the same apart from one list having an additional element? 比较包含相同POJO和更新的两个列表 - Comparing two list which contains same POJO and update 为什么对转换数组生成的列表进行排序也会影响原始数组 - why sort a list generated from converting an array also affects the original array 从列表列表映射到 pojo total - Mapping to pojo total from list of lists ArrayList与单链表是一回事吗? - Is an ArrayList the same thing as a singly-linked list? 搜索一个 GWT ComboBox 影响同一个商店的其他 ComboBox - Search in one GWT ComboBox affects on other ComboBox with the same store 比较两个列表,并将相同的值存储在第三个列表中 - Compare two Lists and same values store in third List issue 从两个列表中过滤具有相同属性的对象并更新它们并将其存储在 Java 8 中的第三个列表中 - Filter Objects having the same property from two Lists and Update them and Store it in a 3rd List in Java 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM