-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
302 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Collections / Code snippets |
160 changes: 160 additions & 0 deletions
160
04-collections-clean-code/snippets/src/collections/CollectionsExamples.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package collections; | ||
|
||
import comparators.Car; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
public class CollectionsExamples { | ||
|
||
private static void exploreCollections() { | ||
// 1. Sorting | ||
List<Integer> nums = new ArrayList<>(); | ||
nums.add(4); | ||
nums.add(9); | ||
nums.add(0); | ||
nums.add(7); | ||
nums.add(-1); | ||
|
||
System.out.println(nums); // [4, 9, 0, 7, -1] | ||
Collections.sort(nums); | ||
System.out.println(nums); // [-1, 0, 4, 7, 9] | ||
Collections.sort(nums, Collections.reverseOrder()); | ||
System.out.println(nums); // [9, 7, 4, 0, -1] | ||
|
||
// 2. Searching: indexOf(), binarySearch() | ||
System.out.println(nums.indexOf(4)); // 2 | ||
Collections.sort(nums); | ||
int index = Collections.binarySearch(nums, 4); | ||
System.out.println(index); // 2 | ||
|
||
// 3. Shuffling | ||
Collections.shuffle(nums); // will randomly shuffle list elements | ||
|
||
// 4. copy(), fill(), reverse(), swap() | ||
List<String> from = new ArrayList<>(); | ||
from.add("foo"); | ||
from.add("bar"); | ||
List<String> to = new LinkedList<>(); | ||
to.add("only"); | ||
to.add("third"); | ||
to.add("remains"); | ||
|
||
Collections.copy(to, from); | ||
System.out.println(to); // [foo, bar, remains] | ||
|
||
Collections.fill(to, "a"); | ||
System.out.println(to); // [a, a, a] | ||
|
||
List<String> list = new ArrayList<>(); | ||
list.add("1"); | ||
list.add("2"); | ||
list.add("3"); | ||
|
||
Collections.reverse(list); | ||
System.out.println(list); // [3, 2, 1] | ||
|
||
Collections.swap(list, 0, 1); | ||
System.out.println(list); // [2, 3, 1] | ||
|
||
// 5. Statistics: min(), max(), frequency() | ||
List<Integer> numList = List.of(4, 9, 0, 7, -1, 7); | ||
|
||
System.out.println(Collections.min(numList)); // -1 | ||
System.out.println(Collections.max(numList)); // 9 | ||
System.out.println(Collections.frequency(numList, 7)); // 2 | ||
|
||
// 6. Collection static factory methods | ||
List<String> listOfWords = List.of("Java", "23", "rulez"); | ||
|
||
int thirteen = 13; | ||
Set<Integer> setOfInts = Set.of(1, 2, 3, 5, 8, thirteen, 8 + thirteen); | ||
System.out.println(setOfInts); | ||
|
||
Map<String, Integer> cities = Map.of( | ||
"Brussels", 1_139_000, | ||
"Cardiff", 341_000 | ||
); | ||
|
||
cities = Map.ofEntries( | ||
Map.entry("Brussels", 1_139_000), | ||
Map.entry("Cardiff", 341_000) | ||
); | ||
|
||
// 7. Since Java 19: static factory methods for creating hash-based collections | ||
Set<String> names = HashSet.newHashSet(2); | ||
names.add("Anna"); | ||
names.add("Jonas"); | ||
names.add("Anna"); | ||
names.add("Jonas"); | ||
names.add("Jonas"); | ||
System.out.println(names); | ||
|
||
} | ||
|
||
private static void findDistinctWords(String[] words) { | ||
Set<String> distinctWords = new TreeSet<>(); | ||
for (String word : words) { | ||
distinctWords.add(word); | ||
} | ||
|
||
System.out.printf("%d distinct words: %s%n", distinctWords.size(), distinctWords); | ||
} | ||
|
||
private static void filterWords(Collection<String> words) { | ||
for (Iterator<String> it = words.iterator(); it.hasNext(); ) { | ||
if (it.next().contains("a")) { | ||
it.remove(); | ||
} | ||
} | ||
} | ||
|
||
private static Map<String, List<Car>> carsByBrandMap(Collection<Car> cars) { | ||
Map<String, List<Car>> carsByBrand = new HashMap<>(); | ||
for (Car car : cars) { | ||
String brand = car.getBrand(); | ||
// if (carsByBrand.containsKey(brand)) { | ||
// carsByBrand.get(brand).add(car); | ||
// } else { | ||
// carsByBrand.put(brand, new ArrayList<>(List.of(car))); | ||
// } | ||
|
||
carsByBrand.putIfAbsent(brand, new ArrayList<>()); | ||
carsByBrand.get(brand).add(car); | ||
} | ||
return carsByBrand; | ||
} | ||
|
||
public static void main(String... args) { | ||
exploreCollections(); | ||
findDistinctWords(new String[] {"car", "cat", "dog", "cat", "dog"}); // 3 distinct words: [car, cat, dog] | ||
List<String> words = new ArrayList<>(Arrays.asList("car", "cat", "dog")); | ||
filterWords(words); | ||
System.out.println(words); // [dog] | ||
|
||
Car c1 = new Car(200, "red", "Ferrari"); | ||
Car c2 = new Car(100, "green", "Mercedes"); | ||
Car c3 = new Car(50, "blue", "BMW"); | ||
Car c4 = new Car(150, "black", "Audi"); | ||
Car c5 = new Car(250, "blue", "Audi"); | ||
Car c6 = new Car(120, "black", "Mercedes"); | ||
|
||
var carsByBrand = carsByBrandMap(List.of(c1, c2, c3, c4, c5, c6)); | ||
|
||
for (String brand : carsByBrand.keySet()) { | ||
System.out.println(brand + "--> " + carsByBrand.get(brand)); | ||
} | ||
|
||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
04-collections-clean-code/snippets/src/comparators/Car.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package comparators; | ||
|
||
public class Car implements Comparable<Car> { | ||
|
||
private int topSpeed; | ||
private String color; | ||
private String brand; | ||
|
||
public int getTopSpeed() { | ||
return topSpeed; | ||
} | ||
|
||
public String getColor() { | ||
return getColor(); | ||
} | ||
|
||
public String getBrand() { | ||
return brand; | ||
} | ||
|
||
public Car(int topSpeed, String color, String model) { | ||
this.topSpeed = topSpeed; | ||
this.color = color; | ||
this.brand = model; | ||
} | ||
|
||
@Override | ||
public int compareTo(Car other) { | ||
// Let's sort the cars by top speed in ascending order. | ||
// The method returns a negative integer, zero, or a positive integer when this car's top speed | ||
// is less than, equal to, or greater than the top speed of the specified object. | ||
// Integer.compare(other.topSpeed, this.topSpeed); | ||
return Integer.compare(this.topSpeed, other.topSpeed); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[topSpeed=" + this.topSpeed + ", color=" + this.color + ", model=" + this.brand + "]"; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
04-collections-clean-code/snippets/src/comparators/CarByNameComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package comparators; | ||
|
||
import java.util.Comparator; | ||
|
||
public class CarByNameComparator implements Comparator<Car> { | ||
|
||
@Override | ||
public int compare(Car first, Car second) { | ||
// compare the car brand names lexicographically | ||
return first.getBrand().compareTo(second.getBrand()); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
04-collections-clean-code/snippets/src/comparators/CarByTopSpeedDescComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package comparators; | ||
|
||
import java.util.Comparator; | ||
|
||
public class CarByTopSpeedDescComparator implements Comparator<Car> { | ||
|
||
@Override | ||
public int compare(Car first, Car second) { | ||
// Let's sort the cars by top speed in descending order. | ||
// Returns a negative integer, zero or a positive integer when the first car's top speed | ||
// is greater than, equal to, or less than the top speed of the specified object. | ||
return Integer.compare(second.getTopSpeed(), first.getTopSpeed()); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
04-collections-clean-code/snippets/src/comparators/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package comparators; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
public class Main { | ||
|
||
public static void main(String... args) { | ||
|
||
Car first = new Car(200, "red", "Ferrari"); | ||
Car second = new Car(100, "green", "Mercedes"); | ||
Car third = new Car(50, "blue", "BMW"); | ||
Car fourth = new Car(150, "black", "Audi"); | ||
|
||
// Lists | ||
List<Car> cars = new ArrayList<>(); | ||
|
||
cars.add(first); | ||
cars.add(second); | ||
cars.add(third); | ||
cars.add(fourth); | ||
|
||
Collections.sort(cars); | ||
System.out.println("Sorting the cars according to their natural order: by topSpeed, ascending:"); | ||
for (Car car : cars) { | ||
System.out.println(car); | ||
} | ||
|
||
Collections.sort(cars, new CarByNameComparator()); | ||
System.out.println("\nCustom sorting the cars by brand name lexicographically:"); | ||
for (Car car : cars) { | ||
System.out.println(car); | ||
} | ||
|
||
Collections.sort(cars, new CarByTopSpeedDescComparator()); | ||
System.out.println("\nCustom sorting the cars by topSpeed in descending order:"); | ||
for (Car car : cars) { | ||
System.out.println(car); | ||
} | ||
|
||
// Map | ||
Map<Car, Integer> carStorage = new HashMap<>(); | ||
carStorage.put(first, 3); | ||
carStorage.put(second, 4); | ||
carStorage.put(third, 10); | ||
carStorage.put(fourth, 25); | ||
|
||
System.out.println("\nIterating the carStorage hash map:"); | ||
for (Map.Entry<Car, Integer> entry : carStorage.entrySet()) { | ||
System.out.println(entry.getKey() + " " + entry.getValue()); | ||
} | ||
|
||
System.out.println("\nSorting the cars storage map by the key's natural order: topSpeed, ascending:"); | ||
Map<Car, Integer> sortedStorage = new TreeMap<>(); | ||
sortedStorage.putAll(carStorage); | ||
for (Map.Entry<Car, Integer> entry : sortedStorage.entrySet()) { | ||
System.out.println(entry.getKey() + " " + entry.getValue()); | ||
} | ||
|
||
System.out.println("\nCustom sorting the cars storage map by the key's by brand name lexicographically:"); | ||
Map<Car, Integer> sortedStorageCustom = new TreeMap<>(new CarByNameComparator()); | ||
sortedStorageCustom.putAll(carStorage); | ||
for (Map.Entry<Car, Integer> entry : sortedStorageCustom.entrySet()) { | ||
System.out.println(entry.getKey() + " " + entry.getValue()); | ||
} | ||
} | ||
|
||
} |