Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.*;

import java.util.function.Function;

public class Main {
    public static <T1, T2> List<T2> lazyMap(final List<T1> lst, final Function<T1, T2> mapper) {
        if (lst == null) {
            return null;
        }
        return new AbstractList<T2>() {
            @Override
            public T2 get(int index) {
                return mapper.apply(lst.get(index));
            }

            @Override
            public int size() {
                return lst.size();
            }
        };
    }

    public static <T1, T2> Collection<T2> lazyMap(final Collection<T1> lst, final Function<T1, T2> mapper) {
        return new AbstractCollection<T2>() {
            @Override
            public Iterator<T2> iterator() {
                return new Iterator<T2>() {
                    Iterator<T1> impl = lst.iterator();

                    @Override
                    public boolean hasNext() {
                        return impl.hasNext();
                    }

                    @Override
                    public T2 next() {
                        return mapper.apply(impl.next());
                    }

                    @Override
                    public void remove() {
                        impl.remove();
                    }
                };
            }

            @Override
            public int size() {
                return lst.size();
            }
        };
    }
}