diff --git a/Chapter03/BONUS_1_ConvertYearMonthToDate/README.md b/Chapter03/BONUS_1_ConvertYearMonthToDate/README.md new file mode 100644 index 00000000..d654d971 --- /dev/null +++ b/Chapter03/BONUS_1_ConvertYearMonthToDate/README.md @@ -0,0 +1,2 @@ +# Converting `Date` to `YearMonth` +Write a program that converts an `Date` to `YearMonth` and vice-versa. diff --git a/Chapter03/BONUS_1_ConvertYearMonthToDate/pom.xml b/Chapter03/BONUS_1_ConvertYearMonthToDate/pom.xml new file mode 100644 index 00000000..fd4c512d --- /dev/null +++ b/Chapter03/BONUS_1_ConvertYearMonthToDate/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + com.app + BONUS_1_ConvertYearMonthToDate + 1.0-SNAPSHOT + jar + + UTF-8 + 13 + 13 + + BONUS_1_ConvertYearMonthToDate + \ No newline at end of file diff --git a/Chapter03/BONUS_1_ConvertYearMonthToDate/src/main/java/modern/challenge/Converters.java b/Chapter03/BONUS_1_ConvertYearMonthToDate/src/main/java/modern/challenge/Converters.java new file mode 100644 index 00000000..29ccc9c0 --- /dev/null +++ b/Chapter03/BONUS_1_ConvertYearMonthToDate/src/main/java/modern/challenge/Converters.java @@ -0,0 +1,33 @@ +package modern.challenge; + +import java.time.YearMonth; +import java.time.ZoneId; +import java.util.Date; + +public class Converters { + + private Converters() { + throw new AssertionError("Cannot be instantiatied"); + } + + public static YearMonth toYearMonth(Date date) { + + if (date == null) { + throw new IllegalArgumentException("The given date cannot be null"); + } + + return YearMonth.from(date.toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDate()); + } + + public static Date toDate(YearMonth ym) { + + if (ym == null) { + throw new IllegalArgumentException("The given year-month cannot be null"); + } + + return Date.from(ym.atDay(1) + .atStartOfDay(ZoneId.systemDefault()).toInstant()); + } +} diff --git a/Chapter03/BONUS_1_ConvertYearMonthToDate/src/main/java/modern/challenge/MainApplication.java b/Chapter03/BONUS_1_ConvertYearMonthToDate/src/main/java/modern/challenge/MainApplication.java new file mode 100644 index 00000000..f4b019e3 --- /dev/null +++ b/Chapter03/BONUS_1_ConvertYearMonthToDate/src/main/java/modern/challenge/MainApplication.java @@ -0,0 +1,13 @@ +package modern.challenge; + +import java.time.YearMonth; +import java.util.Date; + +public class MainApplication { + + public static void main(String[] args) { + + System.out.println("Date to YearMonth: " + Converters.toYearMonth(new Date())); + System.out.println("YearMonth to Date: " + Converters.toDate(YearMonth.now())); + } +} diff --git a/Chapter05/BONUS_2_ConvertIterableToList/README.md b/Chapter05/BONUS_2_ConvertIterableToList/README.md new file mode 100644 index 00000000..cf84b634 --- /dev/null +++ b/Chapter05/BONUS_2_ConvertIterableToList/README.md @@ -0,0 +1,2 @@ +# Converting `Iterable` to `List` +Write a program that converts an `Iterable` to `List`. diff --git a/Chapter05/BONUS_2_ConvertIterableToList/pom.xml b/Chapter05/BONUS_2_ConvertIterableToList/pom.xml new file mode 100644 index 00000000..ef363031 --- /dev/null +++ b/Chapter05/BONUS_2_ConvertIterableToList/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + com.app + BONUS_2_ConvertIterableToList + 1.0-SNAPSHOT + jar + + UTF-8 + 13 + 13 + + BONUS_2_ConvertIterableToList + \ No newline at end of file diff --git a/Chapter05/BONUS_2_ConvertIterableToList/src/main/java/modern/challenge/Converters.java b/Chapter05/BONUS_2_ConvertIterableToList/src/main/java/modern/challenge/Converters.java new file mode 100644 index 00000000..8fb4844d --- /dev/null +++ b/Chapter05/BONUS_2_ConvertIterableToList/src/main/java/modern/challenge/Converters.java @@ -0,0 +1,97 @@ +package modern.challenge; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Spliterator; +import java.util.Spliterators; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +public class Converters { + + private Converters() { + throw new AssertionError("Cannot be instantiatied"); + } + + public static List iterableToList1(Iterable iterable) { + + if (iterable == null) { + return Collections.emptyList(); + } + + List result = new ArrayList<>(); + iterable.forEach(result::add); + + return result; + } + + public static List iterableToList2(Iterable iterable) { + + if (iterable == null) { + return Collections.emptyList(); + } + + List result = StreamSupport.stream(iterable.spliterator(), false) + .collect(Collectors.toList()); + + return result; + } + + public static List iterableToList3(Iterable iterable) { + + if (iterable == null) { + return Collections.emptyList(); + } + + List result = new ArrayList<>(); + iterable.iterator().forEachRemaining(result::add); + + return result; + } + + public static List iterableToList4(Iterable iterable) { + + if (iterable == null) { + return Collections.emptyList(); + } + + List result + = StreamSupport.stream(Spliterators. + spliteratorUnknownSize(iterable.iterator(), Spliterator.ORDERED), false) + .collect(Collectors.toList()); + + return result; + } + + public static List iterableToList5(Iterable iterable) { + + if (iterable == null) { + return Collections.emptyList(); + } + + List result = new ArrayList<>(); + for (T elem : iterable) { + result.add(elem); + } + + return result; + } + + public static List iterableToList6(Iterable iterable) { + + if (iterable == null) { + return Collections.emptyList(); + } + + List result = new ArrayList<>(); + Iterator iterator = iterable.iterator(); + while (iterator.hasNext()) { + result.add(iterator.next()); + } + + return result; + } + +} diff --git a/Chapter05/BONUS_2_ConvertIterableToList/src/main/java/modern/challenge/MainApplication.java b/Chapter05/BONUS_2_ConvertIterableToList/src/main/java/modern/challenge/MainApplication.java new file mode 100644 index 00000000..a6252ed2 --- /dev/null +++ b/Chapter05/BONUS_2_ConvertIterableToList/src/main/java/modern/challenge/MainApplication.java @@ -0,0 +1,19 @@ +package modern.challenge; + +import java.util.Arrays; + +public class MainApplication { + + public static void main(String[] args) { + + // let's consider the next Iterable + Iterable iterable = Arrays.asList("ana", "george", "mark"); + + System.out.println("iterableToList1(): " + Converters.iterableToList1(iterable)); + System.out.println("iterableToList2(): " + Converters.iterableToList2(iterable)); + System.out.println("iterableToList3(): " + Converters.iterableToList3(iterable)); + System.out.println("iterableToList4(): " + Converters.iterableToList4(iterable)); + System.out.println("iterableToList5(): " + Converters.iterableToList5(iterable)); + System.out.println("iterableToList6(): " + Converters.iterableToList6(iterable)); + } +} diff --git a/Chapter05/BONUS_3_ConvertListVtoMapKListV/README.md b/Chapter05/BONUS_3_ConvertListVtoMapKListV/README.md new file mode 100644 index 00000000..9e830873 --- /dev/null +++ b/Chapter05/BONUS_3_ConvertListVtoMapKListV/README.md @@ -0,0 +1,3 @@ +# Convert `List` into `Map>` + +Write a program that converts `List` into `Map>`. diff --git a/Chapter05/BONUS_3_ConvertListVtoMapKListV/pom.xml b/Chapter05/BONUS_3_ConvertListVtoMapKListV/pom.xml new file mode 100644 index 00000000..bac0d2a9 --- /dev/null +++ b/Chapter05/BONUS_3_ConvertListVtoMapKListV/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + com.app + BONUS_3_ConvertListVtoMapKListV + 1.0-SNAPSHOT + jar + + UTF-8 + 13 + 13 + + BONUS_3_ConvertListVtoMapKListV + \ No newline at end of file diff --git a/Chapter05/BONUS_3_ConvertListVtoMapKListV/src/main/java/modern/challenge/Converters.java b/Chapter05/BONUS_3_ConvertListVtoMapKListV/src/main/java/modern/challenge/Converters.java new file mode 100644 index 00000000..562f7762 --- /dev/null +++ b/Chapter05/BONUS_3_ConvertListVtoMapKListV/src/main/java/modern/challenge/Converters.java @@ -0,0 +1,46 @@ +package modern.challenge; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +public class Converters { + + private Converters() { + throw new AssertionError("Cannot be instantiatied"); + } + + public static Map> toMap(List list) { + + if (list == null || list.isEmpty()) { + return Collections.emptyMap(); + } + + return list.stream().collect( + Collectors.groupingBy(String::length, + HashMap::new, Collectors.toCollection(ArrayList::new)) + ); + } + + @SuppressWarnings("unchecked") + public static , M extends Map> M toMap( + List list, Function c, Supplier ms, Supplier cs) { + + if (list == null || c == null || ms == null || cs == null + || list.isEmpty()) { + + throw new IllegalArgumentException("Non of the arguments can be null or empty"); + } + + return list.stream().collect( + Collectors.groupingBy(c, ms, Collectors.toCollection(cs)) + ); + } + +} \ No newline at end of file diff --git a/Chapter05/BONUS_3_ConvertListVtoMapKListV/src/main/java/modern/challenge/MainApplication.java b/Chapter05/BONUS_3_ConvertListVtoMapKListV/src/main/java/modern/challenge/MainApplication.java new file mode 100644 index 00000000..e8f5dc42 --- /dev/null +++ b/Chapter05/BONUS_3_ConvertListVtoMapKListV/src/main/java/modern/challenge/MainApplication.java @@ -0,0 +1,26 @@ +package modern.challenge; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; + +public class MainApplication { + + public static void main(String[] args) { + + /* Convert List into Map> */ + // consider this list + List names + = List.of("joana", "mark", "adela", "leo", "stan", "marius", "kely"); + + HashMap> result1 + = Converters.toMap(names, String::length, HashMap::new, ArrayList::new); + System.out.println("HashMap>: " + result1); + + LinkedHashMap> result2 + = Converters.toMap(names, String::length, LinkedHashMap::new, LinkedList::new); + System.out.println("LinkedHashMap>: " + result2); + } +} diff --git a/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/README.md b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/README.md new file mode 100644 index 00000000..ae04a606 --- /dev/null +++ b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/README.md @@ -0,0 +1,2 @@ +# Current directory +Write a program that return the current project root directory diff --git a/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/pom.xml b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/pom.xml new file mode 100644 index 00000000..db2d1fc8 --- /dev/null +++ b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + com.app + BONUS_1_GetCurrentProjectRootDirectory + 1.0-SNAPSHOT + jar + + UTF-8 + 13 + 13 + + BONUS_1_GetCurrentProjectRootDirectory + \ No newline at end of file diff --git a/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/src/main/java/modern/challenge/MainApplication.java b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/src/main/java/modern/challenge/MainApplication.java new file mode 100644 index 00000000..dca8ec4e --- /dev/null +++ b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/src/main/java/modern/challenge/MainApplication.java @@ -0,0 +1,10 @@ +package modern.challenge; + +public class MainApplication { + + public static void main(String[] args) { + + System.out.println("The root directory of this project is:\n" + + Roots.getCurrentProjectRootDirectory()); + } +} diff --git a/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/src/main/java/modern/challenge/Roots.java b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/src/main/java/modern/challenge/Roots.java new file mode 100644 index 00000000..82e892eb --- /dev/null +++ b/Chapter06/BONUS_1_GetCurrentProjectRootDirectory/src/main/java/modern/challenge/Roots.java @@ -0,0 +1,23 @@ +package modern.challenge; + +import java.nio.file.Path; +import java.nio.file.Paths; + +public class Roots { + + private Roots() { + throw new AssertionError("Cannot be instantiatied"); + } + + public static String getCurrentProjectRootDirectory() { + + String userDirectory = System.getProperty("user.dir"); + Path rootDirectory = Paths.get(".").normalize().toAbsolutePath(); + + if (rootDirectory.startsWith(userDirectory)) { + return rootDirectory.toString(); + } else { + throw new RuntimeException("Cannot find the current project root directory"); + } + } +} diff --git a/README.md b/README.md index 0f781169..dd423e8f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,11 @@ +## $5 Tech Unlocked 2021! +[Buy and download this Book for only $5 on PacktPub.com](https://www.packtpub.com/product/java-coding-problems/9781789801415) +----- +*If you have read this book, please leave a review on [Amazon.com](https://www.amazon.com/gp/product/1789801419). Potential readers can then use your unbiased opinion to help them make purchase decisions. Thank you. The $5 campaign runs from __December 15th 2020__ to __January 13th 2021.__* + # Java Coding Problems -Java Coding Problems +Java Coding Problems This is the code repository for [Java Coding Problems ](https://www.packtpub.com/programming/java-coding-problems?utm_source=github&utm_medium=repository&utm_campaign=), published by Packt.