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.

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3hiMDg0MTkwMTExNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

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()的更多相关文章

  1. 【BZOJ2213】[Poi2011]Difference DP

    [BZOJ2213][Poi2011]Difference Description A word consisting of lower-case letters of the English alp ...

  2. 【概率论】1-3:组合(Combinatorial Methods)

    title: [概率论]1-3:组合(Combinatorial Methods) categories: Mathematic Probability keywords: Combination 组 ...

  3. 【DataStructure】Description and usage of queue

    [Description] A queue is a collection that implements the first-in-first-out protocal. This means th ...

  4. 【DataStructure】Description and Introduction of Tree

    [Description] At ree is a nonlinear data structure that models a hierarchical organization. The char ...

  5. 【DataStructure】One of queue usage: Simulation System

    Statements: This blog was written by me, but most of content  is quoted from book[Data Structure wit ...

  6. 【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 emph ...

  7. 【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 { ...

  8. 【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 ...

  9. 【DataStructure】Some useful methods about linkedList.

    /** * Method 1: Delete the input element x  * and meanwhile keep the length of array after deleted n ...

随机推荐

  1. 使用autofac在mvc5下依赖注入

    把遇到的问题汇总一下: 一.安装mvc5版本 命令:pm> Install-Package Autofac 结果安装的Autofac.Integration.Mvc(版本为4.0),所引用的依赖 ...

  2. C#使用SqlTransaction事务回滚与SqlBulkCopy批量插入数据

    C#中批量处理数据,有时候因为一条记录导致整个批量处理失败.这时候肯能会导致数据不全等问题,这时候我们可以使用SqlTransaction来进行事务回滚,即是要么全部成功要么全部不成功.如下代码 // ...

  3. 5个对话框和FileStream:文件流

    1.private void button1_Click(object sender, EventArgs e) { colorDialog1.ShowDialog();//显示颜色选择器 panel ...

  4. axis2 1.7.1使用教程

    写在前面 本文只说Axis2的用法. 1.下载与部署 需要下载两个文件: 下载地址:http://mirrors.cnnic.cn/apache/axis/axis2/java/core/1.7.1/ ...

  5. go基础笔记

    1.slice:作为参数传递时,传递的是地址,当append时,在新的内存地址分配数据,但是没有返回给原的slice,只能通过返回值的方式赋值给slice2.func(a []int):传递,可以3. ...

  6. How to share memory between services and user processes?

    除了必要的InitializeSecurityDescriptor和SetSecurityDescriptorDacl, 内存映射文件名必须GLOBAL开头.

  7. (转载)TNSPING命令

    Oracle Net 工具(命令)tnsping,是一个OSI会话层的工具,它用来: 1)验证名字解析(name resolution,当然是oracle自己的网络服务名) 2)远程的listener ...

  8. 【Five-Minute Share】“请先了解所使用的工具 ,磨刀不误砍柴工”

    数据是应用系统的血液,没有数据的系统应用价值是非常有限的.经过多年的观察发现,身边很多的程序开发人员在开发应用系统的时候,都是按照标准SQL语法及应用方法去进行数据库设计,并进行应用开发的,没有任何的 ...

  9. swift Self

    'Self' is the type of a protocol/class/struct/enum.And the 'self' is a instance of a class/struct/en ...

  10. trigger事件就是继承某一个类的事件.

    <html><head><script type="text/javascript" src="/jquery/jquery.js" ...