diff --git a/src/main/java/cz/vsb/fei/efrei/lab04/Hero.java b/src/main/java/cz/vsb/fei/efrei/lab04/Hero.java index 09bb8bf489ff70ac13471ba991bc5cd2d8ada7ea..31f40a2e2462ca3a489d5e24a2fbcd5c8aa0a2e0 100644 --- a/src/main/java/cz/vsb/fei/efrei/lab04/Hero.java +++ b/src/main/java/cz/vsb/fei/efrei/lab04/Hero.java @@ -1,9 +1,8 @@ package cz.vsb.fei.efrei.lab04; -import java.util.Objects; import java.util.Random; -public class Hero implements Fighter{ +public class Hero implements Fighter { private static final Random RANDOM = new Random(); @@ -12,10 +11,8 @@ public class Hero implements Fighter{ private int lives; public Hero(String name, int strenght) { - if(name == null || name.length() < 2 || - strenght < 30) { - throw new IllegalArgumentException( - "Sorry wrong arguments!!"); + if (name == null || name.length() < 2 || strenght < 30) { + throw new IllegalArgumentException("Sorry wrong arguments!!"); } this.name = name; this.strenght = strenght; @@ -27,9 +24,8 @@ public class Hero implements Fighter{ } @Override - public void attackedBy(Fighter fighter) - throws AlreadyDeadException { - if(!isAlive()) { + public void attackedBy(Fighter fighter) throws AlreadyDeadException { + if (!isAlive()) { throw new AlreadyDeadException(this); } System.out.println(fighter.getName() + " attack " + getName() + "!"); diff --git a/src/main/java/cz/vsb/fei/efrei/lab04/WorldOfCollections.java b/src/main/java/cz/vsb/fei/efrei/lab04/WorldOfCollections.java index f33777d7e23238d077bd44be409709f7b335efc2..80c39fee76316d2383c29a91bb6cd2c7a4459dba 100644 --- a/src/main/java/cz/vsb/fei/efrei/lab04/WorldOfCollections.java +++ b/src/main/java/cz/vsb/fei/efrei/lab04/WorldOfCollections.java @@ -5,27 +5,26 @@ import java.util.Collection; import java.util.HashSet; import java.util.LinkedList; import java.util.List; -import java.util.Random; import java.util.Set; public class WorldOfCollections { - private static final Random RANDOM = new Random(); - public static void main(String[] args) { useSetToStoreObjectWithoutHashCodeAndEquals(); useSetToStoreObjectWithEqualsOnlyForName(); useSetToStoreObjectWithEqualsAndHashCode(); useSetToStoreObjectWithEqualsBasedOnNameAndBadHashCode(); useSetToStoreObjectWithEqualsAndConstantHashCode(); - System.out.println("=========================================================================================="); + System.out + .println("=========================================================================================="); performanceForUseSetToStoreObjectWithEqualsAndHashCode(); performanceForUseSetToStoreObjectWithEqualsAndConstantHashCode(); - System.out.println("=========================================================================================="); + System.out + .println("=========================================================================================="); performanceForAddIntoArrayList(); performanceForAddIntoLinkedList(); performanceForInsertIntoArrayList(); performanceForInsertIntoLinkedList(); - + } private static void testObjectAnHashSet(Set<Fighter> setOfFighters, Hero king1, Hero king2) { @@ -42,12 +41,12 @@ public class WorldOfCollections { Set<Fighter> setOfFighters = new HashSet<>(); Hero king1 = new Hero("King", 66); Hero king2 = new Hero("King", 66); - System.out.println("=====Use HashSet to Store Object Without HashCode And Equals================================"); + System.out.println( + "=====Use HashSet to Store Object Without HashCode And Equals================================"); testObjectAnHashSet(setOfFighters, king1, king2); System.out.println("Each object is different (different palces in memory) even look same!"); } - public static void useSetToStoreObjectWithEqualsOnlyForName() { Set<Fighter> setOfFighters = new HashSet<>(); Hero king1 = new HeroWithEquals("King", 66); @@ -70,21 +69,24 @@ public class WorldOfCollections { Set<Fighter> setOfFighters = new HashSet<>(); Hero king1 = new HeroWithEqualsAndBadHashcode("King", 66); Hero king2 = new HeroWithEqualsAndBadHashcode("King", 42); - System.out.println("=====Use HashSet to Store Object With Equals and BAD hashcode================================"); + System.out.println( + "=====Use HashSet to Store Object With Equals and BAD hashcode================================"); testObjectAnHashSet(setOfFighters, king1, king2); System.out.println("It does'n work!"); - System.out.println("Object are equals because of same name, but not found in set because of worng hashcode calculated from strenght!"); + System.out.println( + "Object are equals because of same name, but not found in set because of worng hashcode calculated from strenght!"); } public static void useSetToStoreObjectWithEqualsAndConstantHashCode() { Set<Fighter> setOfFighters = new HashSet<>(); Hero king1 = new HeroWithEqualsAndConstantHashcode("King", 66); Hero king2 = new HeroWithEqualsAndConstantHashcode("King", 66); - System.out.println("=====Use HashSet to Store Object With Equals and constant hashcode================================"); + System.out.println( + "=====Use HashSet to Store Object With Equals and constant hashcode================================"); testObjectAnHashSet(setOfFighters, king1, king2); System.out.println("It's work!"); } - + private static String mesureAndAdd(Collection<Hero> sourceOfHeros, Collection<Hero> tragetOfHeros) { Stopwatch stopwatch = new Stopwatch(); stopwatch.start(); @@ -115,10 +117,10 @@ public class WorldOfCollections { } Set<Hero> setOfHeros = new HashSet<>(); String duration = mesureAndAdd(heros, setOfHeros); - System.out.println("Adding " + countOfHeros + " heros with correct hashcode and equals into set take " + duration); + System.out.println( + "Adding " + countOfHeros + " heros with correct hashcode and equals into set take " + duration); } - public static void performanceForUseSetToStoreObjectWithEqualsAndConstantHashCode() { int countOfHeros = 20000; List<Hero> heros = new ArrayList<>(countOfHeros); @@ -130,7 +132,6 @@ public class WorldOfCollections { System.out.println("Adding " + countOfHeros + " heros with constant hashcode into set take " + duration); } - public static void performanceForAddIntoArrayList() { int countOfHeros = 500000; List<Hero> heros = new ArrayList<>(countOfHeros);