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. Oracle 11g RAC for LINUX rhel 6.X silent install(静默安装)

    一.前期规划 1.硬件环境 CPU: Intel(R) Xeon(R) CPU E7-4820 v4 @ 2.00GHz  8*10核 内存:512GB OCR:2147*5 MB DATA1:2TB ...

  2. Cracking the Coding Interview 6.2

    There is an 8*8 chess board in which two diagnolly opposite corners have been cut off. You are given ...

  3. Html Ajax上传文件,form表单下载文件

    Html中的代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&quo ...

  4. javascript实现选项卡切换的4种方法

    方法一:for循环+if判断当前点击与自定义数组是否匹配 <html lang="en"> <head> <meta charset="UT ...

  5. SourceInsight3.5 Space 替换Tab

    # SourceInsight3.5 Space 替换Tab 公司要求所有的缩进都要使用空格,而不是Tab.至于使用Tab,还是Space来进行缩进,这在网上有各种各样的讨论,毕竟使用Tab可以节省很 ...

  6. Python 之 基础知识(四)

    一.公共方法(列表.元组.字典以及字符串) 1.内置函数 cmp函数取消可以用比较运算符来代替,但是字典是无序的,故而不可以用比较运算符比较. 2.切片(列表.元组.字符串适用) 3.运算符 列表中直 ...

  7. iOS中容易混淆的知识点(持续更新中)

    1.成员变量和属性的区别 @interface Person : NSObject { NSString *_sex; } @property (nonatomic, copy) NSString * ...

  8. 【Oracle】搭建DG(DataGuard)

    操作系统:OEL 5.6 Oracle 版本:11.2.0.4.0 DataGuard规划说明 DATABASE_ROLE DB_NAME IPADDR Primary lgr 192.168.10. ...

  9. linux抓包命令-tcpdump命令详解

    最近调试支付接口的时候,遇到了一个奇怪的问题:我按照支付接口文档,对接支付通道,当消费业务正常后,调试查余和冲正的时候,支付通道的对接技术告诉我,系统没有我们支付系统的请求报文,数据库和日志中都没有, ...

  10. Swift - what's the difference between metatype .Type and .self?

    Declaration typealias AnyClass = AnyObject.Type .Type The metatype of a class, structure, or enumera ...