Java List Zip zipper(List> listOfLists)

Here you can find the source of zipper(List> listOfLists)

Description

Given a list of equal-sized lists, group the ith elements of each inner list with each other.

License

Open Source License

Declaration

public static <E> List<List<E>> zipper(List<List<E>> listOfLists) 

Method Source Code


//package com.java2s;
/*/* www . j a va  2s  .c  o m*/
Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com)
    
This file is part of the Semantic Discovery Toolkit.
    
The Semantic Discovery Toolkit is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
The Semantic Discovery Toolkit 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 Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public License
along with The Semantic Discovery Toolkit.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.util.List;
import java.util.ArrayList;

import java.util.Iterator;

public class Main {
    /**
     * Given a list of equal-sized lists, group the ith elements of each
     * inner list with each other.
     * <p>
     * Examples:
     * <p>((a)) generates ((a))
     * <p>((a) (1)) generates ((a 1))
     * <p>((a b c) (1 2 3)) generates ((a 1) (b 2) (c 3))
     * <p>((a b c) (1 2 3) (x y z)) generates ((a 1 x) (b 2 y) (c 3 z))
     */
    public static <E> List<List<E>> zipper(List<List<E>> listOfLists) {
        if (listOfLists == null) {
            return null;
        }

        List<List<E>> result = new ArrayList<List<E>>();
        int innerLength = maxInnerSize(listOfLists);

        for (int i = 0; i < innerLength; ++i) {
            result.add(new ArrayList<E>());
        }

        for (Iterator<List<E>> it1 = listOfLists.iterator(); it1.hasNext();) {
            List<E> innerList = it1.next();
            int index = 0;
            for (Iterator<E> it2 = innerList.iterator(); it2.hasNext(); ++index) {
                E element = it2.next();

                List<E> resultList = result.get(index);
                resultList.add(element);
            }

            /*
            //maybe its better not to pad after all
              // pad missing elements with null when lists aren't same size after all
              for (int i = index; i < innerLength; ++i) {
                List<E> resultList = result.get(i);
                resultList.add(null);
              }
            */
        }

        return result;
    }

    /**
     * Find the maximum size of the inner lists.
     */
    private static <E> int maxInnerSize(List<List<E>> listOfLists) {
        int result = 0;

        for (Iterator<List<E>> it = listOfLists.iterator(); it.hasNext();) {
            List<E> list = it.next();
            int size = list.size();
            result = (size > result) ? size : result;
        }

        return result;
    }
}

Related

  1. zip(final List l1, final List l2)
  2. zip(List a, List b)
  3. zip(List> lists)
  4. zip(List> lists)
  5. zipDoubleArray(List lists)