【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.
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()的更多相关文章
- 【BZOJ2213】[Poi2011]Difference DP
[BZOJ2213][Poi2011]Difference Description A word consisting of lower-case letters of the English alp ...
- 【概率论】1-3:组合(Combinatorial Methods)
title: [概率论]1-3:组合(Combinatorial Methods) categories: Mathematic Probability keywords: Combination 组 ...
- 【DataStructure】Description and usage of queue
[Description] A queue is a collection that implements the first-in-first-out protocal. This means th ...
- 【DataStructure】Description and Introduction of Tree
[Description] At ree is a nonlinear data structure that models a hierarchical organization. The char ...
- 【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 ...
- 【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 ...
- 【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 { ...
- 【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 ...
- 【DataStructure】Some useful methods about linkedList.
/** * Method 1: Delete the input element x * and meanwhile keep the length of array after deleted n ...
随机推荐
- TensorFlow——分布式的TensorFlow运行环境
当我们在大型的数据集上面进行深度学习的训练时,往往需要大量的运行资源,而且还要花费大量时间才能完成训练. 1.分布式TensorFlow的角色与原理 在分布式的TensorFlow中的角色分配如下: ...
- 20. Valid Parentheses[E]有效的括号
题目 Given a string containing just the characters '(',')','[',']','{' and '}',determine if the input ...
- golang图片压缩
package main import ( //"encoding/json" "fmt" //"os" //&qu ...
- lua队列实现
Queue = {} function Queue.newquene() } end function Queue.push(queue, value) queue.count = queue.cou ...
- EditPlus代码自动完成的设置
EditPlus代码自动完成的设置保存在 *.acp 文件中,可以在“工具”->“首选项”->“文件”->“文件类型及语法”中(如下图) 其中“语法文件”保存着进行语法高亮的关键词, ...
- 学习SQL笔记
SQL 语句 语法 AND / OR SELECT column_name(s)FROM table_nameWHERE conditionAND|OR condition ALTER TABLE A ...
- sublime text 3 上安装xdebug
安装完成之后启动xdebug,缺省设置下会显示warning等信息,很不方便. 可以参考 https://github.com/martomo/SublimeTextXdebug/blob/maste ...
- 将电脑特定文件夹保存在U盘中
为什么 各种网盘,借着国家扫黄的阶梯,纷纷取消自己的网盘的服务.但自己有一些不是很大,但又很重要的东西,比如说代码(虽然学的渣) 怎么做 再网上百度,有一些将U盘的文件偷偷拷到电脑的脚本,改一下复制文 ...
- C# switch 语句
switch ("MySql") //选择语句 // case语句 成对 结束 执行到 第一个break { case "SqlServer2000": cas ...
- HDU2516 - 取石子游戏【斐波那契博弈】
基本描述 有一堆个数为n的石子,游戏双方轮流取石子,满足: 先手不能再第一次把所有石子取完: 之后每次可以取的石子数介于1到对手刚取的石子数的2倍之间,包括1和对手取的石子数的2倍. 取最后石子的人 ...