【DataStructure】The difference among methods addAll(),retainAll() and removeAll()

In the Java collection framework, there are three similar methods, addAll(),retainAll() and removeAll().  addAll(), the retainAll(), and the removeAll()methods are equivalent to the set theoretic union,  intersection, and complement operators, respectively.
These are illustrated in the following picture.

In my local machine, just for these methods, I made a test for them respectively.

package com.albertshao.ds.set;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.After;
import org.junit.Test;

public class TestCollectionsMethods {

	List<String> listA = null;
	List<String> listB = null;
	List<String> result = new ArrayList<String>();
	List<String> testList = new ArrayList<String>();

	@Test
	public void testAddAll() {
		listA = Arrays.asList("Apple","Blanana","Grape");
		listB = Arrays.asList("Apple","Orange","Pear");
		Collections.addAll(testList,"Apple","Blanana","Grape","Apple","Orange","Pear");
		result.addAll(listA);
		result.addAll(listB);
		assertEquals(testList, result);
	}

	@Test
	public void testRetainAll() {
		listA = Arrays.asList("Apple","Blanana","Grape");
		listB = Arrays.asList("Apple","Orange","Pear");
		Collections.addAll(testList,"Apple");
		result.addAll(listA);
		result.retainAll(listB);
		assertEquals(testList, result);

	}

	@Test
	public void testRemoveAll() {
		listA = Arrays.asList("Apple","Blanana","Grape");
		listB = Arrays.asList("Apple","Orange","Pear");
		Collections.addAll(testList,"Blanana","Grape");
		result.addAll(listA);
		result.removeAll(listB);
		assertEquals(testList, result);
	}

	@After
	public void testPrint()
	{
		System.out.println("-----------------------");
		if(result != null && !result.isEmpty())
		{
			for(String str: result)
			{
				System.out.println(str);
			}
		}
		System.out.println("-----------------------");
	}

}

The result of execution is as follows:

Hope it‘s beneficial to you

【DataStructure】The difference among methods addAll(),retainAll() and removeAll()

时间: 2024-10-12 23:57:48

【DataStructure】The difference among methods addAll(),retainAll() and removeAll()的相关文章

【DataStructure】 Five methods to init the List in java

Do you know how to init list in other way except for new object? The following will give you serveral tips. If having other great idea, you are welcome to share. [java] view plaincopy import java.util.ArrayList; import java.util.Arrays; import java.u

【DataStructure】Some useful methods for arrays

Last night it took me about two hours to learn arrays. For the sake of less time, I did not put emphaises on the practice question, just now when reading the book, I found that some methods referred to arrays are so beneficial to us. So in here make

【DataStructure】Some useful methods about linkedList(三)

Method 4: Gets the value of element number i For example, if list is {22, 33, 44, 55, 66, 77, 88, 99}, then get(list, 2) will return 44. Solution 1: static int get(Node list, int i) { if (i < 0) { throw new IllegalArgumentException(); } for (int j =

【DataStructure】Some useful methods about linkedList(二)

Method 1: Add one list into the other list. For example, if list1is {22, 33, 44, 55} and  list2 is {66, 77, 88, 99},then append(list1, list2)will change list1to {22, 33, 44, 55, 44, 66, 77, 88, 99}. static void append(Node list1, Node list2) { if (li

【DataStructure】Charming usage of Set in the java

In an attempt to remove duplicate elements from list, I go to the lengths to take advantage of  methods in the java api. After investiagting the document of java api, the result is so satisfying that I speak hightly of wisdom of developer of java lan

【DataStructure】A useful util class for reading and writing files

Faced with the upcoming exam, Some useful methods referred to file operation drew tremenous attention. Now I make a summary to reading file. [java] view plaincopy import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; impo

【DataStructure】Descriptioin and usage of List

Statements: This blog was written by me, but most of content  is quoted from book[Data Structure with Java Hubbard] [Description] Alistis a collection of elements that are accessible sequentially: the first element, followed by the second element, fo

【DataStructure】The description of generic collections

In this blog, generic collections will be talked about  in details.  In the past bacause of shortage of generic argument,  less importance has been attached to the this module. Just now after reading the chapter about this knowledge, I gradually real

【dataStructure】 Arrays and Java Source Review

According to the order of data structure book, Arrays should be introduced in the frist time. When reviewing the some information related to arrays, I feel shocked that many useful classes and methods in java language has been ignored. Now set a simp