From 2d1216f22e3d02e960f08d26b113423996bf5f61 Mon Sep 17 00:00:00 2001 From: Duzzenn <109883788+davidanukam@users.noreply.github.com> Date: Sat, 4 Jan 2025 11:41:30 -0500 Subject: [PATCH 1/3] Create zip-two-lists.md --- .../java/array-manipulation/zip-two-lists.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 snippets/java/array-manipulation/zip-two-lists.md diff --git a/snippets/java/array-manipulation/zip-two-lists.md b/snippets/java/array-manipulation/zip-two-lists.md new file mode 100644 index 00000000..20abc7a2 --- /dev/null +++ b/snippets/java/array-manipulation/zip-two-lists.md @@ -0,0 +1,31 @@ +--- +title: Zip Two Lists +description: Zips two lists into a list of paired elements, combining corresponding elements from both lists. +author: davidanukam +tags: lists,java,zip,stream-api,collections +--- + +```java +import java.util.*; // Importing utility classes for List and Arrays +import java.util.stream.IntStream; // Importing IntStream for range and mapping +import java.util.stream.Collectors; // Importing Collectors for collecting stream results + +public class Main { + // Generic method to zip two lists into a list of paired elements + public static List> zip(List list1, List list2) { + // Create pairs by iterating through the indices of both lists + return IntStream.range(0, Math.min(list1.size(), list2.size())) // Limit the range to the smaller list + .mapToObj(i -> Arrays.asList(list1.get(i), list2.get(i))) // Pair elements from both lists at index i + .collect(Collectors.toList()); // Collect the pairs into a List + } + + public static void main(String[] args) { + // Usage: + List arr1 = Arrays.asList("a", "b", "c"); + List arr2 = Arrays.asList(1, 2, 3); + List> zipped = zip(arr1, arr2); + + System.out.println(zipped); // Output: [[a, 1], [b, 2], [c, 3]] + } +} +``` From 37099a299f61e58a63d4dd9526d7d30bb485cf37 Mon Sep 17 00:00:00 2001 From: Duzzenn <109883788+davidanukam@users.noreply.github.com> Date: Sat, 4 Jan 2025 12:05:33 -0500 Subject: [PATCH 2/3] Update zip-two-lists.md --- snippets/java/array-manipulation/zip-two-lists.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/java/array-manipulation/zip-two-lists.md b/snippets/java/array-manipulation/zip-two-lists.md index 20abc7a2..739a21fc 100644 --- a/snippets/java/array-manipulation/zip-two-lists.md +++ b/snippets/java/array-manipulation/zip-two-lists.md @@ -2,7 +2,7 @@ title: Zip Two Lists description: Zips two lists into a list of paired elements, combining corresponding elements from both lists. author: davidanukam -tags: lists,java,zip,stream-api,collections +tags: lists,zip,stream-api,collections --- ```java From 8f6de4d2cbb8bce5465e30d6d0c2c41f094f8c5d Mon Sep 17 00:00:00 2001 From: Duzzenn <109883788+davidanukam@users.noreply.github.com> Date: Sat, 4 Jan 2025 16:07:25 -0500 Subject: [PATCH 3/3] Update zip-two-lists.md --- .../java/array-manipulation/zip-two-lists.md | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/snippets/java/array-manipulation/zip-two-lists.md b/snippets/java/array-manipulation/zip-two-lists.md index 739a21fc..800d2ce9 100644 --- a/snippets/java/array-manipulation/zip-two-lists.md +++ b/snippets/java/array-manipulation/zip-two-lists.md @@ -10,22 +10,17 @@ import java.util.*; // Importing utility classes for List and Arrays import java.util.stream.IntStream; // Importing IntStream for range and mapping import java.util.stream.Collectors; // Importing Collectors for collecting stream results -public class Main { - // Generic method to zip two lists into a list of paired elements - public static List> zip(List list1, List list2) { - // Create pairs by iterating through the indices of both lists - return IntStream.range(0, Math.min(list1.size(), list2.size())) // Limit the range to the smaller list - .mapToObj(i -> Arrays.asList(list1.get(i), list2.get(i))) // Pair elements from both lists at index i - .collect(Collectors.toList()); // Collect the pairs into a List - } +public List> zip(List list1, List list2) { + // Create pairs by iterating through the indices of both lists + return IntStream.range(0, Math.min(list1.size(), list2.size())) // Limit the range to the smaller list + .mapToObj(i -> Arrays.asList(list1.get(i), list2.get(i))) // Pair elements from both lists at index i + .collect(Collectors.toList()); // Collect the pairs into a List +} - public static void main(String[] args) { - // Usage: - List arr1 = Arrays.asList("a", "b", "c"); - List arr2 = Arrays.asList(1, 2, 3); - List> zipped = zip(arr1, arr2); +// Usage: +List arr1 = Arrays.asList("a", "b", "c"); +List arr2 = Arrays.asList(1, 2, 3); +List> zipped = zip(arr1, arr2); - System.out.println(zipped); // Output: [[a, 1], [b, 2], [c, 3]] - } -} +System.out.println(zipped); // Output: [[a, 1], [b, 2], [c, 3]] ```