/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package collectionexample2; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; /** * * @author denis */ public class CollectionExample2 { /** * @param args the command line arguments */ public static void main(String[] args) { unique(); //simple(); } private static void unique(){ System.out.println("--------------------ArrayList-------------------------"); List arrayList = new ArrayList<>(); arrayList.add(new User("Andrey", 15)); arrayList.add(new User("Andrey", 20)); arrayList.add(new User("Andrey", 15)); arrayList.add(new User("Andrey", 16)); System.out.println("contains=" + arrayList.contains(new User("Andrey", 15))); arrayList.forEach(System.out::println); { System.out.println("--------------------LinkedList-------------------------"); List collection = new LinkedList<>(arrayList); collection.forEach(System.out::println); } System.out.println("--------------------HashSet-------------------------"); { System.out.println("--------------------LinkedList-------------------------"); Set collection = new HashSet<>(arrayList); collection.forEach(System.out::println); } System.out.println("--------------------TreeSet-------------------------"); { System.out.println("--------------------LinkedList-------------------------"); SortedSet collection = new TreeSet<>(arrayList); collection.forEach(System.out::println); } System.out.println("--------------------HashMap-------------------------"); { Map mapColl = new HashMap<>(); mapColl.put(1, new User("Andrey", 15)); mapColl.put(10, new User("Andrey", 15)); mapColl.put(5, new User("Andrey", 20)); mapColl.put(9, new User("Andrey", 16)); System.out.println("contains=" + mapColl.containsValue(new User("Andrey", 15))); mapColl.entrySet().forEach(e -> System.out.println(e)); } System.out.println("--------------------LinkedHashMap-------------------------"); { Map mapColl = new LinkedHashMap<>(); mapColl.put(1, new User("Andrey", 15)); mapColl.put(10, new User("Andrey", 15)); mapColl.put(5, new User("Andrey", 20)); mapColl.put(9, new User("Andrey", 16)); System.out.println("contains=" + mapColl.containsValue(new User("Andrey", 15))); mapColl.entrySet().forEach(e -> System.out.println(e)); } } private static void simple(){ System.out.println("--------------------ArrayList-------------------------"); List arrayList = new ArrayList<>(); arrayList.add("Once"); arrayList.add("Two"); arrayList.add("Two"); arrayList.add("Two"); arrayList.add("Three"); arrayList.add("Four"); arrayList.add("Five"); arrayList.add("Five"); arrayList.forEach(System.out::println); System.out.println("--------------------LinkedList-------------------------"); List linkedList = new LinkedList<>(arrayList); linkedList.forEach(System.out::println); System.out.println("--------------------HashSet-------------------------"); Set hashSet = new HashSet<>(arrayList); hashSet.forEach(System.out::println); System.out.println("--------------------TreeSet-------------------------"); SortedSet sorted = new TreeSet<>(arrayList); sorted.forEach(System.out::println); System.out.println("--------------------HashMap-------------------------"); Map mapColl = new HashMap<>(); mapColl.put(1, "Once"); mapColl.put(10, "Once"); mapColl.put(5, "Two"); mapColl.put(9, null); mapColl.put(0, "Five"); mapColl.entrySet().forEach(e -> System.out.println(e)); System.out.println("--------------------LinkedHashMap-------------------------"); Map hashMap = new LinkedHashMap<>(); hashMap.put(1, "Once"); hashMap.put(10, "Once"); hashMap.put(5, "Two"); hashMap.put(9, null); hashMap.put(0, "Five"); hashMap.entrySet().forEach(e -> System.out.println(e)); } }