Java 8 - Remove repeated sequence of elements from a List
Another concise syntax would be
AtomicReference<Character> previous = new AtomicReference<>(null);
Stream.of('a', 'b', 'b', 'a').filter(cur -> !cur.equals(previous.getAndSet(cur)));
You can use IntStream
to get hold of the index positions in the List
and use this to your advantage as follows :
List<String> acc = IntStream
.range(0, list.size())
.filter(i -> ((i < list.size() - 1 && !list.get(i).equals(list
.get(i + 1))) || i == list.size() - 1))
.mapToObj(i -> list.get(i)).collect(Collectors.toList());
System.out.println(acc);
Explanation
IntStream.range(0,list.size())
: Returns a sequence of primitive int-valued elements which will be used as the index positions to access the list.filter(i -> ((i < list.size() - 1 && !list.get(i).equals(list.get(i + 1) || i == list.size() - 1))
: Proceed only if the element at current index position is not equal to the element at the next index position or if the last index position is reachedmapToObj(i -> list.get(i)
: Convert the stream to aStream<String>
.collect(Collectors.toList())
: Collect the results in a List.
You might use a custom Collector to achieve your goal. Please find details below:
Stream<String> lines = Files.lines(Paths.get("distinct.txt"));
LinkedList<String> values = lines.collect(Collector.of(
LinkedList::new,
(list, string) -> {
if (list.isEmpty())
list.add(string);
else if (!string.equals(list.getLast()))
list.add(string);
},
(left, right) -> {
left.addAll(right);
return left;
}
));
values.forEach(System.out::println);
However it might have some issues when parallel
stream is used.