Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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
            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;
    }
}