0

I have this situation:

List<MyClass> list_1 = new ArrayList<>();
List<MyClass> list_2 = new ArrayList<>();
list_1.add(elements...);
list_2.add(same elements...);
mixList(list_1);  //mixing the inner order between the elements
mixList(list_2);
MyClass mClass = list_1.get(5);  //for example place 5 
//TODO: How to remove above element who is exist
//in list_2 also, from list_2?

Note - also it is same elements, but it's not same objects:

//same elements, different objects
MyClass m1 = new MyClass("name", 1);
MyClass m2 = new MyClass("name", 1);
michael
  • 3,835
  • 14
  • 53
  • 90

1 Answers1

1

Problem 1 :

//same elements, different objects
MyClass m1 = new MyClass("name", 1);
MyClass m2 = new MyClass("name", 1);

To recognise them, you should implement equals() method.

To start with : How to override equals method in java

Problem 2 :

You can remove same items by writing

list2.removeAll(list1);

note that, you have to implements your equals() method to make it work.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307