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. Class工具类

    Class工具类,提供操作class类的方法,源码如下: import java.io.File; import java.io.FileFilter; import java.io.IOExcept ...

  2. lhgdialog.js弹出框

    官方学习网址: http://www.lhgdialog.com/ 个人认为它的样式不太好调,除此之外它也是一款实用的弹出框,专业的用来提示文字,消息,按钮添加function().ifame: 以下 ...

  3. 利用javascript(自定义事件)记录尺寸可变元素的尺寸变化过程

    1.效果图 2.源码 <%@ page contentType="text/html;charset=UTF-8" language="java" %&g ...

  4. Android RecyclerView、ListView实现单选列表的优雅之路.

    一 概述: 这篇文章需求来源还是比较简单的,但做的优雅仍有值得挖掘的地方. 需求来源:一个类似饿了么这种电商优惠券的选择界面: 其实就是 一个普通的列表,实现了单选功能, 效果如图:  (不要怪图渣了 ...

  5. 线性表结构的Java实现

    一.线性表的抽象数据类型表述 线性表的结构简单,长度允许动态增长或搜索:可以对线性表中的任何数据元素进行访问和查找:允许进行数据的插入和删除操作:求线性表中的指定数据的前驱和后继:合并线性表以及拆分线 ...

  6. vue首次进入微信没有标题问题

    首先实在路由改变的时候可以有标题的    首次进入路由不显示标题,查到很多,最有解决可以自定义标签, 后者引入一个包vue-wechat-title,我就是用的后者,前面的没有式过 上地址  http ...

  7. [Intermediate Algorithm] - Spinal Tap Case

    题目 将字符串转换为 spinal case.Spinal case 是 all-lowercase-words-joined-by-dashes 这种形式的,也就是以连字符连接所有小写单词. 提示 ...

  8. SDL2源代码分析

    1:初始化(SDL_Init()) SDL简介 有关SDL的简介在<最简单的视音频播放示例7:SDL2播放RGB/YUV>以及<最简单的视音频播放示例9:SDL2播放PCM>中 ...

  9. H3C三层交换机S5500初始配置+网络访问策略

    DHCP中继配置命令 dhcp relay address-check enable 命令用来使能DHCP 中继的地址匹配检查功能. undo dhcp relay address-check ena ...

  10. VS Code中html 如何查找标签(5)

    1  添加几个标签 <body> <span>第一个span标签</span> <p>这是第一个p标签</p> <span>第二 ...