Java generic method: Merge two lists alternately
Java Generic: Exercise-5 with Solution
Write a Java program to create a generic method that takes two lists of the same type and merges them into a single list. This method alternates the elements of each list.
Sample Solution:
Java Code:
// ReverserList.java
// ReverserList Class
import java.util.ArrayList;
import java.util.List;
public class Merge_two_lists {
public static < T > List < T > mergeLists(List < T > list1, List < T > list2) {
List < T > mergedList = new ArrayList < > ();
int maxLength = Math.max(list1.size(), list2.size());
for (int i = 0; i < maxLength; i++) {
if (i < list1.size()) {
mergedList.add(list1.get(i));
}
if (i < list2.size()) {
mergedList.add(list2.get(i));
}
}
return mergedList;
}
public static void main(String[] args) {
List < Integer > nums1 = List.of(1, 3, 5, 7);
List < Integer > nums2 = List.of(2, 4, 8, 10);
System.out.println("List of numbers:");
System.out.println(nums1);
System.out.println(nums2);
List < String > colors1 = List.of("Red", "Green", "Blue");
List < String > colors2 = List.of("White", "Black", "Orange", "Pink");
System.out.println("\nList of colors:");
System.out.println(colors1);
System.out.println(colors2);
List < Integer > mergedNumbers = mergeLists(nums1, nums2);
// Output:[1, 2, 3, 4, 5, 8, 7, 10]
System.out.println("\nMerged Numbers: " + mergedNumbers);
// Output:[Red, White, Green, Black, Blue, Orange, Pink]
List < String > mergedWords = mergeLists(colors1, colors2);
System.out.println("Merged Colors: " + mergedWords);
}
}
Sample Output:
List of numbers: [1, 3, 5, 7] [2, 4, 8, 10] List of colors: [Red, Green, Blue] [White, Black, Orange, Pink] Merged Numbers: [1, 2, 3, 4, 5, 8, 7, 10] Merged Colors: [Red, White, Green, Black, Blue, Orange, Pink]
Explanation:
In the above exercise, we define a generic method mergeLists() that takes two lists list1 and list2 as input. The method creates a newly created mergedList from an ArrayList. It iterates up to the maximum length of the two lists using a for loop. In each iteration, it checks if the index is within the bounds of each list. It adds the corresponding element to the mergedList using the add() method.
The method returns the mergedList at the end.
Using the main() method, we demonstrate the use of mergeLists() by passing two lists of integers (numbers1 and numbers2) and two lists of strings (words1 and words2). We merge the elements of each pair of lists and print the merged lists.
Flowchart:
Java Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Reverse list elements.
Next: Filter list with predicate.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/generic/java-generic-exercise-5.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics