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 {
    /**
     * Partition on the keys returned by mapper
     */
    public static <T1, T2> Map<T2, List<T1>> partition(List<T1> lst, Function<T1, T2> mapper) {
        Map<T2, List<T1>> map = new HashMap<>();
        for (T1 elem : lst) {
            T2 key = mapper.apply(elem);
            if (!map.containsKey(key))
                map.put(key, new ArrayList<T1>());
            map.get(key).add(elem);
        }
        return map;
    }
}