Java Stream Operation flatOptionals(Stream> list)

Here you can find the source of flatOptionals(Stream> list)

Description

flat Optionals

License

Mozilla Public License

Declaration

public static <T> Stream<T> flatOptionals(Stream<Optional<T>> list) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0.  If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 * /*  w  w  w . jav a 2  s . com*/
 * Software distributed under the License is distributed on an "AS IS" basis, 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for 
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is: FetchMailAtt
 * The Initial Developer of the Original Code is: William Wong (williamw520@gmail.com)
 * Portions created by William Wong are Copyright (C) 2015 William Wong, All Rights Reserved.
 *
 ******************************************************************************/

import java.util.*;
import java.util.stream.*;

public class Main {
    public static <T> Stream<T> flatOptionals(Stream<Optional<T>> list) {
        return list.filter(Optional::isPresent).map(opt -> opt.get());
    }

    public static <T> List<T> flatOptionals(List<Optional<T>> list) {
        return list.stream().filter(Optional::isPresent).map(opt -> opt.get()).collect(Collectors.toList());
    }
}

Related

  1. filterType(Stream stream, Class type)
  2. findLast(Stream s)
  3. findLastOf(Stream stream)
  4. findStreamAmongst(Class clazz, Collection instances)
  5. firstValue(Stream stream)
  6. flattenFeatureStreamToMap( Stream>>> stream)
  7. gcd(IntStream numbers)
  8. getOSIllegalCharacterStream(String path)
  9. getStream(Iterable iterable)