Please write this code in Java and include the entire program. Including the mai
ID: 3722450 • Letter: P
Question
Please write this code in Java and include the entire program. Including the main methods.
Given two sorted lists, L1 and L2, complete a following procedure in Java to compute: (a) L1 L2 = { x | x e L1 and x f L2 } using only the basic list operators (next(), hasNext(), and compareTo)) and one loop public static > void difference(List 1terL2 L2.11stIterator(); if iterL1.hasNext) &s; iterL2.hasNexto) iten! itemL2 iterL1.next(); iterL2.next(); /I YOUR CODE GOES HERE (b) L1 L2 = { x | x e L1 and x e L2) using only the basic list operators (next(), hasNext(), and compareTo)) and one loop public static AnyType extends comparable? super AnyType>> void intersection(List L1, ListExplanation / Answer
code:-
part a:-
int size=list1.size()>list2.size()?list1.size():list2.size();
int a=itr1.next();
int b=itr2.next();
List<Integer> diff=new ArrayList<>();
for (int i = 0; i < size; i++) {
if(a==b) {
a=itr1.next();
b=itr2.next();
}
else if (a>b)
{
diff.add(b);
b=itr2.next();
}else if(i==size-1)
{
diff.add(a);
diff.add(b);
}
else
{
diff.add(a);
a=itr1.next();
}
}
System.out.println(diff);
code
part b:-
int size=list1.size()>list2.size()?list1.size():list2.size();
int a=itr1.next();
int b=itr2.next();
List<Integer> intersec=new ArrayList<>();
for (int i = 0; i < size; i++) {
if(a==b) {
intersec.add(x);
a=itr1.next();
b=itr2.next();
}
else if(a>b) {
b=itr2.next();
}else {
a=itr1.next();
}
}
System.out.println(intersec);