Skip to content

Commit 8f6de4d

Browse files
authored
Update zip-two-lists.md
1 parent 37099a2 commit 8f6de4d

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

snippets/java/array-manipulation/zip-two-lists.md

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,17 @@ import java.util.*; // Importing utility classes for List and Arrays
1010
import java.util.stream.IntStream; // Importing IntStream for range and mapping
1111
import java.util.stream.Collectors; // Importing Collectors for collecting stream results
1212

13-
public class Main {
14-
// Generic method to zip two lists into a list of paired elements
15-
public static <A, B> List<List<Object>> zip(List<A> list1, List<B> list2) {
16-
// Create pairs by iterating through the indices of both lists
17-
return IntStream.range(0, Math.min(list1.size(), list2.size())) // Limit the range to the smaller list
18-
.mapToObj(i -> Arrays.asList(list1.get(i), list2.get(i))) // Pair elements from both lists at index i
19-
.collect(Collectors.toList()); // Collect the pairs into a List
20-
}
13+
public <A, B> List<List<Object>> zip(List<A> list1, List<B> list2) {
14+
// Create pairs by iterating through the indices of both lists
15+
return IntStream.range(0, Math.min(list1.size(), list2.size())) // Limit the range to the smaller list
16+
.mapToObj(i -> Arrays.asList(list1.get(i), list2.get(i))) // Pair elements from both lists at index i
17+
.collect(Collectors.toList()); // Collect the pairs into a List
18+
}
2119

22-
public static void main(String[] args) {
23-
// Usage:
24-
List<String> arr1 = Arrays.asList("a", "b", "c");
25-
List<Integer> arr2 = Arrays.asList(1, 2, 3);
26-
List<List<Object>> zipped = zip(arr1, arr2);
20+
// Usage:
21+
List<String> arr1 = Arrays.asList("a", "b", "c");
22+
List<Integer> arr2 = Arrays.asList(1, 2, 3);
23+
List<List<Object>> zipped = zip(arr1, arr2);
2724

28-
System.out.println(zipped); // Output: [[a, 1], [b, 2], [c, 3]]
29-
}
30-
}
25+
System.out.println(zipped); // Output: [[a, 1], [b, 2], [c, 3]]
3126
```

0 commit comments

Comments
 (0)