0

I have two list (listA and listB) and I want them to be independent. So I can add an element to the listB without the elements of the listA being modified, to remain the same.

Tryed Collection listB = Collections.unmodifiableCollection(listA);

    ArrayList<String> listA = new ArrayList<>();
    listA.add("alex");
    listA.add("brian");
    listA.add("charles");
    ArrayList<String> listB = new ArrayList<>();
    listB = listA;
    listB.add("williams");
    System.out.println(listA);

run: [alex, brian, charles, williams]

When I run it I wanted to show only theese
run: [alex, brian, charles]
(Without "williams")

6 Answers6

5

When you are doing listB = listA you are just assigning the reference of listA to listB, you are not creating a new Object.
To create a new Object, do a copy of the list as below

List<String> listB = new ArrayList<>(listA);
raviraja
  • 676
  • 10
  • 27
1

In order to add all elements of a List to another, you may simply use the method addAll.

In your case:

listB.addAll(listA);
0
    ArrayList<String> listA = new ArrayList<>();
    listA.add("alex");
    listA.add("brian");
    listA.add("charles");
    ArrayList<String> listB = (ArrayList<String>) listA.clone();
    listB.add("williams");
    System.out.println(listA);
    System.out.println(listB);
Mak
  • 1,068
  • 8
  • 19
0

You can use the SerializationUtils.clone() method.

private static void deepCopy(){
    ArrayList<String> listA = new ArrayList<>();
    listA.add("alex");
    listA.add("brian");
    listA.add("charles");
    ArrayList<String> listB = SerializationUtils.clone(listA);
    listB.add("williams");
    System.out.println(listA);
    System.out.println(listB);

}

Result:

[alex, brian, charles]
[alex, brian, charles, williams]
Randika
  • 31
  • 4
  • and where is this class `SerializationUtils` class defined? a reference link to docs might be encouraged... – Michał Krzywański Aug 27 '19 at 05:55
  • It's include in "commons-lang.jar" .First add that jar as the dependency and after that, import the 'import org.apache.commons.lang3.SerializationUtils' – Randika Aug 27 '19 at 08:12
0

Your problem is how clone List in Java,there are many method to do this:

  • Using a Copy Constructor: Using the ArrayList constructor in Java, a new list can be initialized with the elements from another collection.
List<String> listB = new ArrayList<>(listA);
  • Using the addAll() method: The List class has a method called addAll(), which appends all the elements of one collection to the list.
List<String> listB = new ArrayList<>();
listB.addAll(listA);
List<String> listB = listA.stream().collect(Collectors.toList());
  • Using Gson library to convert list to JSON: Using Google’s Gson library, a list can be converted to JSON. This JSON string can be converted to an object of type List and can be used to initialize the new List.
String jsonListA = gson.toJson(listA);
List<String> listB = gson.fromJson(jsonListA,List.class);
TongChen
  • 1,414
  • 1
  • 11
  • 21
0

You need to copy the original list to a new object. You can stream the first list and collect the elements in a new one:

List<String> listB = listA.stream().collect(Collectors.toList());

Please notice that using a copy constructor (like new ArrayList<>(listA)) or using the addAll method, the contents of both lists will reference the same objects, every amends made in one element will affect both lists.

lmarx
  • 476
  • 4
  • 9