Java Iterable Item concat(Iterable first, Iterable second)

Here you can find the source of concat(Iterable first, Iterable second)

Description

concat

License

Open Source License

Declaration

public static <T> Iterable<T> concat(Iterable<T> first, Iterable<T> second) 

Method Source Code


//package com.java2s;
import java.util.*;

public class Main {
    public static <T> Iterable<T> concat(Iterable<T> first, Iterable<T> second) {

        return new Iterable<T>() {

            final Iterator<T> it1 = first.iterator();
            final Iterator<T> it2 = second.iterator();

            @Override//  w ww  . jav a  2s  . com
            public Iterator<T> iterator() {

                return new Iterator<T>() {

                    Iterator<T> curr;

                    @Override
                    public boolean hasNext() {
                        if (it1.hasNext()) {
                            curr = it1;
                            return true;
                        } else if (it2.hasNext()) {
                            curr = it2;
                            return true;
                        }
                        return false;
                    }

                    @Override
                    public T next() {
                        return curr.next();
                    }
                };
            }
        };
    }

    public static <T> List<T> concat(List<T> list1, List<T> list2) {
        List<T> list = new ArrayList<>(list1.size() + list2.size());
        list.addAll(list1);
        list.addAll(list2);
        return list;
    }
}

Related

  1. clear(Iterable iterable)
  2. concat(final Iterable> iterables)
  3. concat(Iterable... iterables)
  4. contains(Iterable iter, Object o)
  5. getFirst(final Iterable iterable)
  6. getFirst(Iterable iterable)
  7. getFirstElement(Object maybeIterable)