### Question:
MessageListCollector { public static Collector<Message, ?, MessageList> toMessageList() { return Collector.of(MessageList::new, MessageList::add, addAll()); } private  MessageListCollector(); static Collector<Message, ?, MessageList> toMessageList();  }### Answer:
@Test public void testToMessageList_Parallel() { MessageList sortedMessageList = Stream.of(Message.newInfo("A", "bla"), Message.newError("B", "blubb")) .parallel() .collect(MessageListCollector.toMessageList()); assertThat(sortedMessageList, hasSize(2)); assertThat(sortedMessageList, hasInfoMessage("A")); assertThat(sortedMessageList, hasErrorMessage("B")); List<String> codes = sortedMessageList.stream().map(Message::getCode).collect(toList()); assertThat(codes, contains("A", "B")); }
@Test public void testToMessageList() { MessageList emptyMessageList = Stream.<Message> empty().collect(toMessageList()); assertThat(emptyMessageList, is(emptyMessageList())); MessageList singleMessageList = Stream.of(Message.newError("A", "bla")) .collect(MessageListCollector.toMessageList()); assertThat(singleMessageList, hasSize(1)); MessageList sortedMessageList = Stream.of(Message.newInfo("A", "bla"), Message.newError("B", "blubb")) .collect(MessageListCollector.toMessageList()); assertThat(sortedMessageList, hasSize(2)); assertThat(sortedMessageList, hasInfoMessage("A")); assertThat(sortedMessageList, hasErrorMessage("B")); List<String> codes = sortedMessageList.stream().map(Message::getCode).collect(toList()); assertThat(codes, contains("A", "B")); }