获取list集合中重复的元素

老规矩,二话不说直接上代码:

package com.poinne17.test;

import org.apache.commons.collections.CollectionUtils;
import org.junit.Test;

import java.util.*;

/**
 * @author:Pionner17
 * @date: 2017/9/3 22:41
 * @email:[email protected]
 * @phone: 17600903381
 * @motto: make a little progress every day!
 * @params: 
 */
public class Demo {
    @Test
    public void test01(){
        //创建一个list并加入元素
        List list = new ArrayList<>();
        list.add("abc");
        list.add("bdc");
        list.add("cbc");
        list.add("cbc");
        list.add("abc");
        for(String str:list){
            System.out.println(str);
        }

        //将list放入set中对其去重
        Set set = new HashSet<>(list);
        System.out.println("---------------------------------------");

        //获得list与set的差集
        Collection rs = CollectionUtils.disjunction(list,set);
        //将collection转换为list
        List list1 = new ArrayList<>(rs);
        for(String str:list1){
            System.out.println(str);
        }

    }
}
运行结果:

"D:\Program Files\Java\jdk1.8.0_131\bin\java" -ea -Didea.launcher.port=7532 "-Didea.launcher.bin.path=E:\IntelliJ IDEA 15.0.2\bin" -Didea.junit.sm_runner -Dfile.encoding=UTF-8 -classpath "E:\IntelliJ IDEA 15.0.2\lib\idea_rt.jar;E:\IntelliJ IDEA 15.0.2\plugins\junit\lib\junit-rt.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;H:\pionner17\wantJ\target\test-classes;H:\pionner17\wantJ\target\classes;C:\Users\asus\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\asus\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\asus\.m2\repository\javax\servlet\jstl\1.2\jstl-1.2.jar;C:\Users\asus\.m2\repository\javax\servlet\javax.servlet-api\3.0.1\javax.servlet-api-3.0.1.jar;C:\Users\asus\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\asus\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\asus\.m2\repository\org\springframework\spring-aop\4.3.0.RELEASE\spring-aop-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-aspects\4.3.0.RELEASE\spring-aspects-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\aspectj\aspectjweaver\1.8.9\aspectjweaver-1.8.9.jar;C:\Users\asus\.m2\repository\org\springframework\spring-beans\4.3.0.RELEASE\spring-beans-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-context\4.3.0.RELEASE\spring-context-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-context-support\4.3.0.RELEASE\spring-context-support-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-core\4.3.0.RELEASE\spring-core-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-expression\4.3.0.RELEASE\spring-expression-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-instrument\4.3.0.RELEASE\spring-instrument-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-instrument-tomcat\4.3.0.RELEASE\spring-instrument-tomcat-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-jdbc\4.3.0.RELEASE\spring-jdbc-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-jms\4.3.0.RELEASE\spring-jms-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-messaging\4.3.0.RELEASE\spring-messaging-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-orm\4.3.0.RELEASE\spring-orm-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-oxm\4.3.0.RELEASE\spring-oxm-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-test\4.3.0.RELEASE\spring-test-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-tx\4.3.0.RELEASE\spring-tx-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-web\4.3.0.RELEASE\spring-web-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-websocket\4.3.0.RELEASE\spring-websocket-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-webmvc\4.3.0.RELEASE\spring-webmvc-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\springframework\spring-webmvc-portlet\4.3.0.RELEASE\spring-webmvc-portlet-4.3.0.RELEASE.jar;C:\Users\asus\.m2\repository\net\sf\json-lib\json-lib\2.4\json-lib-2.4-jdk15.jar;C:\Users\asus\.m2\repository\commons-beanutils\commons-beanutils\1.8.0\commons-beanutils-1.8.0.jar;C:\Users\asus\.m2\repository\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;C:\Users\asus\.m2\repository\commons-lang\commons-lang\2.5\commons-lang-2.5.jar;C:\Users\asus\.m2\repository\net\sf\ezmorph\ezmorph\1.0.6\ezmorph-1.0.6.jar;C:\Users\asus\.m2\repository\javax\mail\mail\1.4\mail-1.4.jar;C:\Users\asus\.m2\repository\javax\activation\activation\1.1\activation-1.1.jar;C:\Users\asus\.m2\repository\org\mybatis\mybatis\3.3.0\mybatis-3.3.0.jar;C:\Users\asus\.m2\repository\org\mybatis\mybatis-spring\1.2.2\mybatis-spring-1.2.2.jar;C:\Users\asus\.m2\repository\mysql\mysql-connector-java\5.1.8\mysql-connector-java-5.1.8.jar;C:\Users\asus\.m2\repository\c3p0\c3p0\0.9.1.2\c3p0-0.9.1.2.jar;C:\Users\asus\.m2\repository\redis\clients\jedis\2.7.3\jedis-2.7.3.jar;C:\Users\asus\.m2\repository\org\apache\commons\commons-pool2\2.3\commons-pool2-2.3.jar;C:\Users\asus\.m2\repository\org\springframework\data\spring-data-redis\1.6.0.RELEASE\spring-data-redis-1.6.0.RELEASE.jar;C:\Users\asus\.m2\repository\org\sitemesh\sitemesh\3.0.0\sitemesh-3.0.0.jar;C:\Users\asus\.m2\repository\org\slf4j\slf4j-api\1.7.7\slf4j-api-1.7.7.jar;C:\Users\asus\.m2\repository\ch\qos\logback\logback-core\1.1.3\logback-core-1.1.3.jar;C:\Users\asus\.m2\repository\ch\qos\logback\logback-classic\1.1.3\logback-classic-1.1.3.jar;C:\Users\asus\.m2\repository\ch\qos\logback\logback-access\1.1.3\logback-access-1.1.3.jar;C:\Users\asus\.m2\repository\org\slf4j\log4j-over-slf4j\1.7.7\log4j-over-slf4j-1.7.7.jar;C:\Users\asus\.m2\repository\org\slf4j\jcl-over-slf4j\1.7.7\jcl-over-slf4j-1.7.7.jar;C:\Users\asus\.m2\repository\org\logback-extensions\logback-ext-spring\0.1.2\logback-ext-spring-0.1.2.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 com.poinne17.test.Demo,test01
abc
bdc
cbc
cbc
abc
---------------------------------------
abc
cbc

Process finished with exit code 0



你可能感兴趣的:(Java基础)