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.Collection;
import java.util.Collections;
import java.util.List;

import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
    public static <T> Set<String> toSet(List<T> list, Function<T, String> getId) {
        return stream(list).map(getId).collect(Collectors.toSet());
    }

    public static <T, R> List<R> map(List<T> collection, Function<T, R> mapper) {
        return stream(collection).map(mapper).collect(Collectors.toList());
    }

    public static <T> Stream<T> stream(Collection<T> collection) {
        return Optional.ofNullable(collection).orElseGet(Collections::emptyList).stream();
    }
}