boolean (@NotNull String name, @NotNull List<Pair<Field, YamlMetaType>> fields) { // it is not clear how to merge fields with different multiplicities // so we will reject the merge if they don't match Map<Boolean, List<YamlMetaType>> byMultiplicity = fields.stream().collect( Collectors.groupingBy(fieldAndType -> fieldAndType.getFirst().isMany(), Collectors.mapping(fieldAndType -> fieldAndType.getSecond(), Collectors.toList()))); List<YamlMetaType> forMany = byMultiplicity.getOrDefault(Boolean.TRUE, Collections.emptyList()); List<YamlMetaType> forSingle = byMultiplicity.getOrDefault(Boolean.FALSE, Collections.emptyList()); if (!forMany.isEmpty() && !forSingle.isEmpty()) { throw new IllegalArgumentException("Can't merge field " + name + ", it is many for: " + forMany + " but singular for: " + forSingle); } return forSingle.isEmpty(); // isMany for all }