Java Stream Operation interleave(Stream a, Stream b)

Here you can find the source of interleave(Stream a, Stream b)

Description

interleave

License

Open Source License

Declaration

public static <T9> Stream<T9> interleave(Stream<T9> a, Stream<T9> b) 

Method Source Code

//package com.java2s;
/*/* www. j  a va 2  s. c  o m*/
 This file is part of the BlueJ program. 
 Copyright (C) 1999-2009,2011,2012,2013,2014,2015  Michael Kolling and John Rosenberg 
    
 This program is free software; you can redistribute it and/or 
 modify it under the terms of the GNU General Public License 
 as published by the Free Software Foundation; either version 2 
 of the License, or (at your option) any later version. 
    
 This program is distributed in the hope that it will be useful, 
 but WITHOUT ANY WARRANTY; without even the implied warranty of 
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 GNU General Public License for more details. 
    
 You should have received a copy of the GNU General Public License 
 along with this program; if not, write to the Free Software 
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. 
    
 This file is subject to the Classpath exception as provided in the  
 LICENSE.txt file that accompanied this code.
 */

import java.util.ArrayList;

import java.util.List;

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
    public static <T9> Stream<T9> interleave(Stream<T9> a, Stream<T9> b) {
        // I'm sure there is a cleverer way to do this, but never mind:
        List<T9> ar = a.collect(Collectors.toList());
        List<T9> br = b.collect(Collectors.toList());
        ArrayList<T9> r = new ArrayList<>(ar.size() + br.size());
        for (int i = 0; i < Math.max(ar.size(), br.size()); i++) {
            if (i < ar.size())
                r.add(ar.get(i));
            if (i < br.size())
                r.add(br.get(i));

        }
        return r.stream();
    }
}

Related

  1. gcd(IntStream numbers)
  2. getOSIllegalCharacterStream(String path)
  3. getStream(Iterable iterable)
  4. getStringStreamFromArray(String... ids)
  5. infiniteParallelStream()
  6. isEmpty(final Stream stream)
  7. isFinite(Stream stream)
  8. iterableOf(Stream stream)
  9. iteratorToFiniteStream(Iterator iterator, boolean parallel)