Lostsoul
Lostsoul

Reputation: 25999

How to create a list that merges two other lists?

I'm really new to java and slowly learning so not sure if there is an obvious way to do this but I basically have two lists that I want to merge together to form a single list.

The python code for this, uses a function called zip. Say I have list1 = 1,2,3,4,5 and list2= 6,7,8,9,10..Then I want to make a new list with something like new_list = (1,6), (2,7), (3,8), (4,9), (5,10).

I found a question that had a similar problem but I don't want to use an external library and would rather learn how to create this function myself.

Upvotes: 0

Views: 464

Answers (2)

stevevls
stevevls

Reputation: 10843

The generalized algorithm would look something like this (assuming you want to take N input lists):

public <T> List<List<T>> zip(List<T> ... lists) {

    if(lists.isEmpty()) {
        return Collections.<List<T>>emptyList();
    }

    // validate that the input lists are all the same size.
    int numItems = lists[0].size();
    for(int i = 1; i < lists.length; i++) {
        if(lists[i].size() != numItems) {
            throw new IllegalArgumentException("non-uniform-length list at index " + i);
        }
    } 

    List<List<T>> result = new ArrayList<List<T>>();

    for(int i = 0; i < numItems; i++) {

        // create a tuple of the i-th entries of each list
        List<T> tuple = new ArrayList<T>(lists.length);
        for(List<T> list : lists) {
            tuple.add(list.get(i));
        }

        // add the tuple to the result
        result.add(tuple);
    }

    return result;
}

Upvotes: 1

DwB
DwB

Reputation: 38300

public class Blammy
{
    private String left;
    private String right;

    public Blammy(final String left, final String right)
    {
        this.left = left;
        this.right = right;
    }

    public String toString()
    {
        return "(" + left + ", " + right + ")";
    }
}

public class Kramlish
{
    public List<Blammy> mergalish(final List<String> left, final List<String> right)
    {
        int leftSize;
        int maxSize;
        int rightSize;
        String leftValue;
        List<Blammy> returnValue;
        String rightValue;

        if (left != null)
        {
            leftSize = left.size();
        }
        else
        {
            leftSize = 0;
        }

        if (right != null)
        {
            rightSize = right.size();
        }
        else
        {
            rightSize = 0;
        }

        if (leftSize > rightSize)
        {
            maxSize = leftSize;
        }
        else
        {
            maxSize = rightSize;
        }

        if (maxSize > 0)
        {
            returnValue = new ArrayList<Blammy>(maxSize);

            for (int index = 0; index < maxSize; ++index)
            {
                if (index < leftSize)
                {
                    leftValue = left.get(index);
                }
                else
                {
                    leftValue = null;
                }

                if (index < rightSize)
                {
                    rightValue = right.get(index);
                }
                else
                {
                    rightValue = null;
                }

                Blammy item = new Blammy(leftValue, rightValue);
                returnValue.add(item);
            }
        }
        else
        {
            returnValue = new ArrayList<Blammy>();
        }

        return returnValue;
    }
}

Upvotes: 1

Related Questions