How to union ArrayList HashSet with same the elements only using retainAll() in Java

1 Answer

0 votes
package javaapplication1;

import java.util.ArrayList;
import java.util.HashSet;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            ArrayList<String> list = new ArrayList<>();
            list.add("java");
            list.add("c");
            list.add("c++");
            list.add("c#");

            HashSet<String> hash = new HashSet<>();
            hash.add("java");
            hash.add("c");
            hash.add("python");

            hash.retainAll(list);

            hash.stream().forEach((s) -> {
                System.out.println(s);
            });

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
             
run:

java
c
    
 */

 



answered Dec 1, 2016 by avibootz

Related questions

...