I am new to Java Generics. I have two List with different type Object and Integer and i want to write a generic method which takes the list of lists and want to print out the containing elements inside the list. Example
List<Object> list1 = List.of(1, List.of(2, 3, 4, List.of(5)), List.of(6, 7));
genericmethod(list1);
System.out.println();
List<Integer> list2 = List.of(1, 2, 3, 4, 5, 6, 7, 8);
genericmethod(list2);
System.out.println();
both the method should print 1,2,3,4,5,6,7,8
I tried with
public static void genericmethod(List<?> list) {
list.stream().forEach(System.out::println);
}
Output : 1 [2, 3, 4, [5]] [6, 7]
and
1 2 3 4 5 6 7 8
But i want to print out just 1,2,3,4,5,6,7,8 from both the list as a result.