Merging key-value sets as specified by a command
Abstract as much as possible over type: List instead of ArrayList.   
Use OO where appropriate: List&lt;String&gt; operations should clearly be some
class MoveOperation(String to, String from).
It's not clear why you are using LinkedHashSet instead of List.
The algorithm is ill-defined: what happens if there are numerous
foo's or bar's.


I implemented something below, but it is in functional style, so it might be hard to read.  There are three implicit loops: two for the filters and one for the map.  You might also notice that I never modify a collection, but always create a new one.  Again, this is the functional style.  In your algorithm, you can actually shoot yourself in the foot if you modify some collections at the wrong moment.

private List&lt;List&lt;String&gt;&gt; merge2(List&lt;String&gt; operations, List&lt;List&lt;String&gt;&gt; listOfSentences) {
    Stream&lt;List&lt;String&gt;&gt; sentencesWithTo = listOfSentences.stream()
                                           .filter(sentence -&gt; sentence.contains(toMerge));
    Stream&lt;List&lt;String&gt;&gt; sentencesWithFrom = listOfSentences.stream()
                                             .filter(sentence -&gt; sentence.contains(fromMerge));

    long nTo = sentencesWithTo.count();
    long nFrom = sentencesWithFrom.count();

    if (nTo == 0  || nFrom == 0) {
        return listOfSentences;
    } else if (nTo &gt; 1 || nFrom &gt; 1) {
        // what to do here??
    } else {
        List&lt;String&gt; sentenceTo = sentencesWithTo.collect(Collectors.toList()).get(0); // should only be one element ?
        List&lt;String&gt; sentenceFrom = sentencesWithFrom.collect(Collectors.toList()).get(0); // should only be one element ?
        Stream&lt;List&lt;String&gt;&gt; updatedSentences = listOfSentences.stream().map(sentence -&gt; {
            if (sentence.equals(sentenceTo))
                return Stream.concat(sentenceTo.stream(), sentenceFrom.stream()).collect(Collectors.toList());
            else if (sentence.equals(sentenceFrom))
                return new ArrayList&lt;&gt;();
            else 
                return sentence;
        });
        return updatedSentences.collect(Collectors.toList());
    }
}