【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 ...
随机推荐
- codeforces 712A. Memory and Crow
2019-05-18 08:48:27 加油,加油,坚持!!! 这道题我没有想出公式推导,只是按照模拟题来做,第5个样例超时 样例超时,方法错误 https://www.cnblogs.com/ECJ ...
- Codeforces Round #455
Generate Login 第二个单词肯定只取首字母 Solution Segments 从1开始的线段和在n结束的线段各自凑一凑,剩下的转化为规模为n-2的子问题. Solution Python ...
- [Offer收割]编程练习赛35
有歧义的号码 #include<stdio.h> #include<string.h> #include<stdlib.h> int cmp(const void ...
- Python 之 基础知识(五)
一.变量 1.引用 id() 函数传参 与 返回值 都是传递保存的数据的引用 2.可变和不可变类型(变量的引用地址只在赋值语句后变化) 不可变类型 内存中的数据不允许被修改 数字类型 int,bool ...
- 一个不错的学习android的网站
http://androiddoc.qiniudn.com/guide/topics/ui/overview.html,最近想学下android的开发,找了一下网上的资料,中文的说的觉得太概括,看不太 ...
- 逐步理解Java中的线程安全问题
什么是Java的线程安全问题? 线程安全就是多线程访问时,采用了加锁机制,当一个线程访问该类的某个数据时,进行保护,其他线程不能进行访问直到该线程读/写完,其他线程才可使用.不会出现数据不一致或者数据 ...
- (转)基于MVC4+EasyUI的Web开发框架形成之旅--MVC控制器的设计
http://www.cnblogs.com/wuhuacong/p/3284628.html 自从上篇<基于MVC4+EasyUI的Web开发框架形成之旅--总体介绍>总体性的概括,得到 ...
- swift pragma mark
众所周知,大家在OC中对代码进行逻辑组织 用的是#pragma mark - ,生成分隔线 用#pragma mark 函数说明,来生成一个函数的说明X 但在swift中,这个语法就不支持了,毕竟它是 ...
- C++基础 (10) 第十天 C++中类型转换 异常 栈解旋 io操作
1之前内容的回顾 C语言中的类型转换(int)a 强转可读性太差了 C++把()拆分成了四种转换方式 static_cast static_cast在编译器编译阶段就进行转换了 2.dynamic_ ...
- SurgingFunction