Returns the intersection of two sets without modifying the original sets. - Java Collection Framework

Java examples for Collection Framework:Set

Description

Returns the intersection of two sets without modifying the original sets.

Demo Code


//package com.java2s;

import java.util.HashSet;

import java.util.Set;

public class Main {
    /**// w  ww.  j  a  va  2s .c o m
     * Returns the intersection of two sets without modifying the original sets.<p>
     * 
     * @param <A> the type of objects contained in the sets
     * 
     * @param first the first set
     * @param second the second set
     * 
     * @return the intersection of both sets
     */
    public static <A> Set<A> intersection(Set<A> first, Set<A> second) {

        HashSet<A> result = new HashSet<A>(first);
        result.retainAll(second);
        return result;
    }
}

Related Tutorials