Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Collections.People;
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- List<Student> studentsList = new ArrayList<>();
- /*An attempt to assign a wrong class*/
- //studentsList.add(new Teacher());
- List<Human> humansList = new ArrayList<>();
- humansList.add(new Teacher("Andrey", 28, "099-334-22-12"));
- humansList.add(new Student("Viacheslav", 33, "063-334-12-18"));
- /*HashSet will allow you to add duplicates, but the duplicated value will replace the first entrance*/
- Set<Human> humanSet = new HashSet<>();
- Teacher teacher01 = new Teacher("Valera", 47, "050-489-22-332");
- Teacher teacher02 = new Teacher("Anna", 37, "066-490-21-456");
- humanSet.add(teacher01);
- humanSet.add(teacher02);
- humanSet.add(teacher01);
- humanSet.forEach(n -> {
- System.out.println(Human.whoIsWho(n));
- System.out.println("Name: " + n.getName());
- System.out.println("Age: " + n.getAge());
- System.out.println("Phone: " + n.getPhone());
- System.out.println('\n');
- });
- /*Comparable interface was implemented in Worker class in order to override
- * compareTo method and be able to use the TreeSort with custom objects.
- * Adding the duplicate in TreeSet won't cause the error message, but will replace an
- * old value with the new one.*/
- Set<Human> humanTreeSet = new TreeSet<>();
- Worker workerDuplicate = new Worker();
- humanTreeSet.add(workerDuplicate);
- humanTreeSet.add(workerDuplicate);
- humanTreeSet.forEach(n -> {
- System.out.println("Worker's Name: " + n.getName());
- System.out.println('\n');
- });
- /*Polymorphism: humanList contains both subtypes: Teacher and Student, as they're inherit
- the base Human class*/
- humansList.forEach(n -> {
- System.out.println(Human.whoIsWho(n));
- System.out.println("Name: " + n.getName());
- System.out.println("Age: " + n.getAge());
- System.out.println("Phone: " + n.getPhone());
- System.out.println('\n');
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement