Java Stream collect to HashSet
import java.util.ArrayList; import java.util.HashSet; import java.util.stream.Stream; class NamePhoneEmail { String name;// ww w. ja v a 2s. c om String phonenum; String email; NamePhoneEmail(String n, String p, String e) { name = n; phonenum = p; email = e; } } class NamePhone { String name; String phonenum; NamePhone(String n, String p) { name = n; phonenum = p; } } public class Main { public static void main(String[] args) { // A list of names, phone numbers, and e-mail addresses. ArrayList<NamePhoneEmail> myList = new ArrayList<>(); myList.add(new NamePhoneEmail("CSS", "555-5555", "css@demo2s.com")); myList.add(new NamePhoneEmail("HTML", "555-4444", "html@demo2s.com")); myList.add(new NamePhoneEmail("Java", "555-3333", "java@demo2s.com")); // Map just the names and phone numbers to a new stream. Stream<NamePhone> nameAndPhone = myList.stream().map((a) -> new NamePhone(a.name, a.phonenum)); HashSet<NamePhone> npSet = nameAndPhone.collect(HashSet::new, HashSet::add, HashSet::addAll); System.out.println("\nNames and phone numbers in a Set:"); for (NamePhone e : npSet) System.out.println(e.name + ": " + e.phonenum); } }