Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. package Collections.People;
  2.  
  3. import java.util.*;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         List<Student> studentsList = new ArrayList<>();
  9.  
  10.         /*An attempt to assign a wrong class*/
  11.         //studentsList.add(new Teacher());
  12.  
  13.         List<Human> humansList = new ArrayList<>();
  14.  
  15.         humansList.add(new Teacher("Andrey", 28, "099-334-22-12"));
  16.         humansList.add(new Student("Viacheslav", 33, "063-334-12-18"));
  17.  
  18.         /*HashSet will allow you to add duplicates, but the duplicated value will replace the first entrance*/
  19.         Set<Human> humanSet = new HashSet<>();
  20.         Teacher teacher01 = new Teacher("Valera", 47, "050-489-22-332");
  21.         Teacher teacher02 = new Teacher("Anna", 37, "066-490-21-456");
  22.  
  23.         humanSet.add(teacher01);
  24.         humanSet.add(teacher02);
  25.         humanSet.add(teacher01);
  26.         humanSet.forEach(n -> {
  27.             System.out.println(Human.whoIsWho(n));
  28.             System.out.println("Name: " + n.getName());
  29.             System.out.println("Age: " + n.getAge());
  30.             System.out.println("Phone: " + n.getPhone());
  31.             System.out.println('\n');
  32.         });
  33.  
  34.         /*Comparable interface was implemented in Worker class in order to override
  35.          * compareTo method and be able to use the TreeSort with custom objects.
  36.          * Adding the duplicate in TreeSet won't cause the error message, but will replace an
  37.          * old value with the new one.*/
  38.         Set<Human> humanTreeSet = new TreeSet<>();
  39.         Worker workerDuplicate = new Worker();
  40.         humanTreeSet.add(workerDuplicate);
  41.         humanTreeSet.add(workerDuplicate);
  42.  
  43.         humanTreeSet.forEach(n -> {
  44.             System.out.println("Worker's Name: " + n.getName());
  45.             System.out.println('\n');
  46.         });
  47.  
  48.         /*Polymorphism: humanList contains both subtypes: Teacher and Student, as they're inherit
  49.         the base Human class*/
  50.         humansList.forEach(n -> {
  51.             System.out.println(Human.whoIsWho(n));
  52.             System.out.println("Name: " + n.getName());
  53.             System.out.println("Age: " + n.getAge());
  54.             System.out.println("Phone: " + n.getPhone());
  55.             System.out.println('\n');
  56.         });
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement