SoFunction
Updated on 2025-03-01

Method to remove the same object elements in the arraylist container

<span class="keyword" style="background-color: rgb(250, 250, 250); font-size: 1em; font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace;">boolean</span><span style="color: black; background-color: rgb(250, 250, 250); font-size: 1em; font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace;"> retainAll(Collection<?> c); </span>

I found the retainAll method online

Then add it in the code

<pre name="code" class="html">List<Employee> <span style="font-family: Arial, Helvetica, sans-serif;">employeeOfIntermediarys </span><span style="font-family: Arial, Helvetica, sans-serif;">= new ArrayList<Employee>();
</span><pre name="code" class="html">List<Employee> <span style="font-family: Arial, Helvetica, sans-serif;">tempList </span><span style="font-family: Arial, Helvetica, sans-serif;">= new ArrayList<Employee>();</span>
if(tempList != null){
&lt;span style="white-space:pre"&gt;	&lt;/span&gt;//Remove the same elements in <span style="font-family: Arial, Helvetica, sans-serif;">tempList as in</span><span style="font-family: Arial, Helvetica, sans-serif;">employeeOfIntermediarys, and merge them	List&lt;Employee&gt; eoiList = new ArrayList&lt;Employee&gt;(employeeOfIntermediarys);
	if ((tempList)) {
		(eoiList);
	}
	(tempList);
}

Then I found that it was not possible. After checking a lot of things, I found that the code was correct, but I just needed to rewrite the equals method.

I don't know why, because I used a custom class book in the small demo I wrote before, as follows, it can be used normally

public class Book {

	private int id;
	private String nameString;
	private String writer;
	
	public Book(){
		id= -1;
		nameString = "";
		writer = "";
	}
List&lt;Book&gt; books = new ArrayList&lt;Book&gt;();
  List&lt;Book&gt; tempList = new ArrayList&lt;Book&gt;();
  
  Book book1 = new Book();
  (1);
  ("dkjdf");
  
  Book book2 = new Book();
  (2);
  ("To increase blessings, fortune, longevity");
  
  Book book3 = new Book();
  (3);
  ("dj place");
  
  Book book4 = new Book();
  (4);
  ("dkjdf");
  
  Book book5 = new Book();
  (5);
  ("To increase blessings, fortune, longevity");
  
  Book book6 = new Book();
  (6);
  ("dj place");
  
  
  (book1);
  (book2);
  (book5);
  (null);
  
  (book1);
  (book4);
  (null);
  (books);
  ((0).booktoString());

whatever, I'm going to find the source code of collection.

After rewriting the equals method in Employee.

as follows:

	@Override
	public boolean equals(Object c){
		if (!(c instanceof Employee)) {
			return false;
		}
		Employee eoi = (Employee) c;
		if (().equals(userGuid)) {
			return true;
		}
		return false;		
	}

The above code will run normally.

The above is the full content of the method I have brought to you to remove the same object elements in the arraylist container. I hope it will be helpful to everyone and support me more~