Avatar ·

How to merge two lists (List) in Java? Simple and efficient ways

Hello everyone! I'm a beginner in Java and I have a seemingly simple task. I have two lists (ArrayList) that contain strings. For example, the first list is the names of employees from department A, and the second is the names from department B. I need to merge them into one common list for further processing (for example, to display on the screen or pass to another method). Here's how I tried to do it: ```java import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List listA = new ArrayList<>(); listA.add("Alice"); listA.add("Bob"); List listB = new ArrayList<>(); listB.add("Charlie"); listB.add("David"); // Trying to merge, but the compiler complains ```