You are given two lists (or arrays) of elements. Write a function that returns a third list containing only the elements that are common to both input lists. The result should contain unique elements that appear in both lists. The order of elements in the resulting list does not matter unless explicitly specified.
Example:
Input:
list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]
Output: [3, 4, 5]
List res = list1.stream().filter(list2::contains).toList();
Construct a function that can be executed by multiple threads, where each thread operates under different conditions but shares the same function logic. Ensure that both threads have access to the shared function while processing data based on distinct conditions or inputs. Implement proper synchronization if necessary to prevent race conditions or data inconsistencies.
Example Concept:
Thread A prints even numbers from a shared list.
Thread B prints odd numbers from the same list.
Both threads call the same function but handle data differently depending on whether the value is even or odd.
public class MyThread { private static final Object lock = new Object(); private static int number = 0; private static final int max = 10; public static void main(String[] args) { Function task = (parity) -> () -> { while (true){ synchronized (lock){ if (number + 1 >= max){ lock.notifyAll(); break; } while (number % 2 != parity){ try { lock.wait(); }catch (InterruptedException ex){ Thread.currentThread().interrupt(); System.out.println(ex.getMessage()); } } System.out.println(Thread.currentThread().getName() + " is Running." + " value " + ++number); lock.notifyAll(); } } }; Thread thread1 = new Thread(task.apply(0), "Thread 1"); Thread thread2 = new Thread(task.apply(1), "Thread 2"); thread1.start(); thread2.start(); } }

Here's your problem of the day
Solving this problem will increase your chance to get selected in this company
What is recursion?