You can do this in a single Stream pipeline using the StreamEx library.
List<Container> containersList = Arrays.asList(new Container("A"), new Container("B"), new Container("A"), new Container("A"), new Container("B"));
String[] result =
StreamEx.of(containersList)
.map(Container::getLetter)
.groupRuns(Object::equals)
.collect(MoreCollectors.pairing(
MoreCollectors.flatMapping(List::stream, Collectors.joining()),
MoreCollectors.mapping(l -> l.get(0), Collectors.joining()),
(s1, s2) -> new String[] { s1, s2 }
));
System.out.println(result[0]);
System.out.println(result[1]);
This code creates a Stream of the containers and maps each of those to their letter.
Then, the method groupRuns
collapses into a List
the successive elements that matches the given predicate. In this case, the predicate is the equality of the String: so if you start with the stream [A, A, B]
, this method will collapse it into the Stream [List(A, A), List(B)]
(the first element is the list of 2 A
successive elements in the input).
Finally, this is collected with the pairing
collector that allows to collect into two different collector. The first one joins the flat map result of each list while the second one joins only the first element of the list (hence removing the successive elements).
The result is stored inside an array which just serves as a holder for two values.
Output:
ABAAB
ABAB
Using the Stream API directly
If you want to stay with the current API and not using a library, your best bet would be to write a custom Collector
:
public static void main(String[] args) {
List<Container> containersList = Arrays.asList(new Container("A"), new Container("B"), new Container("A"), new Container("A"), new Container("B"));
String[] result = containersList.stream().parallel().map(Container::getLetter).collect(ContainerCollector.collector());
System.out.println(result[0]);
System.out.println(result[1]);
}
private static final class ContainerCollector {
private StringBuilder text = new StringBuilder();
private StringBuilder dupText = new StringBuilder();
private void accept(String letter) {
text.append(letter);
if (dupText.indexOf(letter, dupText.length() - letter.length()) < 0) {
dupText.append(letter);
}
}
private ContainerCollector combine(ContainerCollector other) {
text.append(other.text);
other.dupText.codePoints().forEach(i -> {
String letter = new String(Character.toChars(i));
if (dupText.indexOf(letter, dupText.length() - letter.length()) < 0) {
dupText.append(letter);
}
});
return this;
}
private String[] finish() {
return new String[] { text.toString(), dupText.toString() };
}
private static Collector<String, ?, String[]> collector() {
return Collector.of(ContainerCollector::new, ContainerCollector::accept, ContainerCollector::combine, ContainerCollector::finish);
}
}
This custom collector builds the text
and dupText
when each letter is accepted. For the text
String, the letter is always appended. For the dupText
, the letter is only appended if the last one is different.
The combiner code (ran in case of parallel execution) is a bit tricky for the dupText
: the second one is appended if it does not start with the end of the first one. Otherwise, the first letter is dropped and the rest is appended.
The output is the same.
if(condition == false)
. The canonical negation of aboolean
is to use logical not, aka the!
operator:if(!condition)
, just like you would writeif(condition)
rather thanif(condition==true)
, wouldn’t you… wouldn’t you?(condition==true)==true
to be consistent? After all,condition==true
is a kind of condition. Oh wait…(condition==true)==true
is also a condition… Are you allowed to writea!=b
or do you have to write(a==b)==false
?==true
on a boolean expression is denying the fact that there is aboolean
type in Java that already prevents mixing of values and conditionals. There is no problem that adding a==true
can solve.true
orfalse
is bad style. Too bad your company's coding standard says to do that. Whoever wrote that should be taken out and ... chastised firmly.