Question

In: Computer Science

Java: How do I compare two lists for example: List<A> listOne {a,b,c,d,e} List<A> listTwo {a,d,e} 1....

Java:

How do I compare two lists for example:

List<A> listOne {a,b,c,d,e}

List<A> listTwo {a,d,e}

1. I am supposed to make sure that whatever that is in listTwo is in listOne even though there's more in listOne and there are repetitives in listTwo e.g {a,a,d,e} but it would still turn out true. But if listTwo is {a,b,d,f} it would return false.

2. Also, how can I make sure that in listTwo, there can be repetitives of any elements in listOne but it cannot appear consecutively. For example, listTwo {a,b,c,b,e} or {a,b,c,d,e,d} or {a} is valid but listTwo {a,b,c,a} or {a,a,b,c,d} is invalid.

I am supposed to throw an exception if listTwo is null or empty, if an element in listTwo is not in listOne, and if an element in listTwo appears twice or more consecutively (the start and end of the list are also considered consecutive) in an ordeer with at least two elements.

Solutions

Expert Solution

1)

import java.util.*;
public class ArrayListExample
{
public static void main(String[] args)
{
ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d","e"));

ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "d","e"));

Collections.sort(listOne);
Collections.sort(listTwo);

//Compare unequal lists example

boolean isEqual = listOne.equals(listTwo); //false
System.out.println(isEqual);

listTwo.add("a");
Collections.sort(listTwo);
System.out.println(listTwo);
//adding same element list 2
//Compare equals lists example
LinkedHashSet<String> hashSet = new LinkedHashSet<>(listTwo);

ArrayList<String> listWithoutDuplicates = new ArrayList<>(hashSet);

System.out.println(listWithoutDuplicates);
listWithoutDuplicates.add("f");
System.out.println(listWithoutDuplicates);
isEqual = listOne.equals(listWithoutDuplicates); //true
System.out.println(isEqual);
}
}

output

2)

import java.util.*;
public class ArrayListExample
{
public static void main(String[] args)
{
ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d","e"));

ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "d","e"));

Collections.sort(listOne);
  
  
Collections.sort(listTwo);
  

  

  
try
{
if( listTwo.isEmpty() ) || if(!listOne.equals(listTwo)) throws Exception
System.out.println("listtwo is not null or emty");
}
catch(Exception e)
{
System.out.println(e);
  
}
  
System.out.println(listOne);
System.out.println(listTwo);
  
}


  
}
  
  
  
  


Related Solutions

Consider the cross: A/a; b/b; C/c; D/d; E/e x A/a; B/b; c/c; D/d; e/e a) what...
Consider the cross: A/a; b/b; C/c; D/d; E/e x A/a; B/b; c/c; D/d; e/e a) what proportion of the progeny will phenotypically resemble the first parent? b) what proportion of the progeny will genotypically resemble neither parent?
I have A B and C answered I only need the answer to D and E....
I have A B and C answered I only need the answer to D and E. I have included the correct answers for A B and C Problem 8-41 (LO. 2, 3, 9) Lori, who is single, purchased 5-year class property for $200,000 and 7-year class property for $400,000 on May 20, 2016. Lori expects the taxable income derived from her business (without regard to the amount expensed under § 179) to be about $800,000. Lori wants to elect immediate...
how do I calculate WACC with the following formula? rWACC = (E/E+D) rE + D/(E+D) rD...
how do I calculate WACC with the following formula? rWACC = (E/E+D) rE + D/(E+D) rD (1-TC) Assume the Market Risk Premium is 5%, The risk free rate is normally the yield on US Treasury securities such as a 10-year treasury. For this assignment, please use 3.5%. You may assume a corporate tax rate of 40%. Equity is 59.75Billion Debt is 30.28Billion Beta is .68 Please show me how to do the calculations and which numbers go where in the...
please i need the solve for A&B&C&D&E ESPECIALLY B&C&D&E Company Information Wood Work Ltd manufacture specialist...
please i need the solve for A&B&C&D&E ESPECIALLY B&C&D&E Company Information Wood Work Ltd manufacture specialist wood furniture and sell their products all over Saudi  Arabia. The company was established three  years ago in Jeddah and is performing well to date. Wood work Ltd have three main product lines; TV tables, dining table and chairs. The following financial information has been provided.   Financial Information TV tables dining Tables Chairs Selling Price per unit SAR 1,000 SAR 5,000 SAR 700 Direct Materials (wood...
1. a. How many permutations are there of the letters {A,B,C,D,E,F}? Of there, how many are...
1. a. How many permutations are there of the letters {A,B,C,D,E,F}? Of there, how many are even? b. Express the permutation BAFEDC in P6 in cycle notation and determine whether it is even or odd. c. Determine the composition BAFEDC*BCAFDE in P6. Is the composition even or odd? d. What is the members of P6 whose cycle notation (1345)(26)?
Compare hepatitis A, B, C, D, and E in terms of source of infection, incubation period,...
Compare hepatitis A, B, C, D, and E in terms of source of infection, incubation period, acute disease manifestations, development of chronic disease, and the carrier state.
How many proper subsets are there for this set {A,B,C,D,E,F,G,H,I}?
How many proper subsets are there for this set {A,B,C,D,E,F,G,H,I}?
Given the following knowledge base: a <- b^c. b <- d^e. b <- g^e. c <-...
Given the following knowledge base: a <- b^c. b <- d^e. b <- g^e. c <- e. d. e. ƒ <- a^g. Which of the following would be the trace of resolved atoms assuming a bottoms-up proof procedure? Select one: a. {a,b,c,e,g} b. {a,b,c,e,d} c. {g,e,b,e,c,a} d. None of these options Constraint Satisfaction Problem (CSP) is consists of a set of _________________. Select one: a. Variables, heuristics, and solutions b. Variables, domains, and backtracking c. Variables, domains, and constraints d....
How many ways are there to arrange the letters ‘a’, ‘b’, ‘c’, ‘d’, and ‘e’ such...
How many ways are there to arrange the letters ‘a’, ‘b’, ‘c’, ‘d’, and ‘e’ such that ‘a’ is not immediately followed by ‘e’ (no repeats since it is an arrangement)? Justify your answer using the product rule, the sum rule, and/or the subtraction rule .
Let A = {a, b, c, d} and B = {b, d, e}. Write out all...
Let A = {a, b, c, d} and B = {b, d, e}. Write out all of the elements of the following sets. (a) B ∩ ∅ (b) A ∪ B (c) (A ∩ B) × B (d) P(A\B) (e) {X ∈ P(A) | |X| ≤ 3}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT