Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**
     * Convenient method to chain two or more lists together.
     * @param first first list
     * @param others other lists
     * @return chained list
     */
    @SuppressWarnings("unchecked")
    @SafeVarargs
    public static <T> List<T> chain(List<T> first, List<T>... others) {
        int size = first.size();
        for (List<T> other : others) {
            size += other.size();
            // fdfdf
        }
        ArrayList<T> result = new ArrayList<>(size);
        result.addAll(first);
        for (List<T> other : others) {
            result.addAll(other);
        }
        return result;
    }

    /**
     * Convenient method to chain two sets together.
     * @param first first set
     * @param others other sets
     * @return chained set
     */
    @SuppressWarnings("unchecked")
    @SafeVarargs
    public static <T> Set<T> chain(Set<T> first, Set<T>... others) {
        HashSet<T> result = new HashSet<>(first);
        for (Set<T> other : others) {
            result.addAll(other);
        }
        return result;
    }

    /**
     * Convenient method to chain iterables together.
     * @param iterables to chain
     * @return chained iterator
     */
    public static <T> Iterator chain(Iterator<T>... iterables) {
        List<Iterator<T>> iterableList = Arrays.asList(iterables);
        return new Iterator<T>() {
            @Override
            public boolean hasNext() {
                return iterableList.stream().anyMatch(Iterator::hasNext);
            }

            @Override
            public T next() {
                return iterableList.stream().filter(Iterator::hasNext).findFirst().map(Iterator::next).orElse(null);
            }
        };
    }
}