合集

欢马劈雪     最近更新时间:2020-01-02 10:19:05

513
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
联合收藏
/*来 自 nowjava - 时  代  Java*/

//package com.nowjava;

import java.util.ArrayList;


import java.util.Collection;

import java.util.Collections;


import java.util.HashSet;

import java.util.LinkedList;

import java.util.List;


public class Main {

    public static void main(String[] argv) {

        Collection list1 = java.util.Arrays.asList("asdf", "nowjava.com");
        /** 
        来 自 
        nowjava.com - 时代Java
        **/

        Collection list2 = java.util.Arrays.asList("asdf", "nowjava.com");

        System.out.println(conjunctCollections(list1, list2));

    }


    public static <T> List<T> conjunctCollections(

            final Collection<T> list1, final Collection<T> list2) {

        final HashSet<T> s1 = new HashSet<T>(list1);

        s1.retainAll(list2);

        return new ArrayList<T>(s1);

    }


    public static <T> List<T> asList(final Iterable<? extends T> iterable) {

        return (iterable instanceof Collection) ? new LinkedList<T>(

                (Collection<? extends T>) iterable) : new LinkedList<T>() {

            private static final long serialVersionUID = 3109256773218160485L;

            {

                if (iterable != null) {

                    for (final T t : iterable) {

                        add(t);

                    }

                }

            }

        };
展开阅读全文