-3

I have a list that contains elements of different texts such that:

First element txt1 is the following text:

Analysis of coverage
Better algorithm
Strategy of random search

Second element txt2 is the following text:

Analysis of coverage
Better algorithm
Master process
Strategy of genetic search

Third element txt3 is the following text:

Analysis of coverage
Better algorithm
Master process
Strategy of genetic search

What I want is to compare each element with the previous one and detect any differences. In the case above, I want to compare txt1 with txt2 and return the following lines as they are different:

Master process
Strategy of genetic search

But when comparing txt2 and txt3, there is no difference at all.

How can I do that?

Adam Amin
  • 1,406
  • 2
  • 11
  • 23
  • 1
    Did you try anything at all? – ninesalt Feb 05 '19 at 11:20
  • Possible duplicate of [What is the best way get the symmetric difference between two sets in java?](https://stackoverflow.com/questions/8064570/what-is-the-best-way-get-the-symmetric-difference-between-two-sets-in-java) – Olf Feb 05 '19 at 11:21
  • if (!set.add){...} – Chris Feb 05 '19 at 11:30

1 Answers1

2

Disclaimer: Don't want to give you specific code as currently question shows no attempt at the problem.

I would try this:

  1. Read contents of elements from the list and convert into single string.
  2. Separate String contents by newline String.split("\\r?\\n"). Essentially, you want to have a list of sentences present in each element. If needed, you can also trim each sentence of white spaces: String.split("\\s*\\r?\\n\\s*").
  3. Compare the two lists and retrieve difference. There's many ways you can do that e.g: iterate between both lists and record difference, or use ArrayList.removeAll() (i.e. you remove all contents of one list from another list and see the change). Good luck!
vs97
  • 5,765
  • 3
  • 28
  • 41