Java tutorial
//package com.java2s; /* * Copyright 2016 Dmitry Korotych (dkorotych at gmail dot com). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.StreamSupport; public class Main { /** * Convert a sequence of elements to the list. * * @param <I> Type of objects for input collection * @param <O> Type of objects for output collection * @param iterable The sequence of elements to convert. If the input * sequence is empty, then the resulting collection will be empty * @param transformer The conversion function. If the transfer function is * not set, the resulting collection will be empty * @return Reformed collection of input elements */ public static <I, O> List<O> transformWithoutNull(final Iterable<I> iterable, final Function<I, O> transformer) { return StreamSupport .stream(Optional.ofNullable(iterable).orElse(Collections.<I>emptySet()).spliterator(), false) .map(Optional.ofNullable(transformer).orElse(i -> null)).filter(Objects::nonNull) .collect(Collectors.toList()); } }