Java Stream collect to Set
import java.util.ArrayList; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; class NamePhoneEmail { String name;//from www.j a va2 s. com 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)); // Now, create a Set by use of collect(). Set<NamePhone> npSet = nameAndPhone.collect(Collectors.toSet()); System.out.println("\nNames and phone numbers in a Set:"); for (NamePhone e : npSet) System.out.println(e.name + ": " + e.phonenum); } }