/* Copyright rememberjava.com. Licensed under GPL 3. See http://rememberjava.com/license */ package com.rememberjava.lambda; import static org.junit.Assert.assertEquals; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collector; import java.util.stream.Collectors; import org.junit.Test; public class IdentityCollectorsTest { /** * Returns a specialised {@link Collector} which results in a {@link Map} * where the keys are the elements of the stream, and the values is provided * by the given mapping function. * * @param the type of the input elements, and keys of the Map * @param the output type of the value mapping function * * @param valueMapper a mapping function to produce values * @return a {@code Collector} which collects elements into a {@code Map} * whose keys are as given by the stream, and values are the result of * applying the given mapping function to the input elements. */ public static Collector> identityToValue( Function valueMapper) { return Collectors.toMap(Function.identity(), valueMapper); } /** * Returns a {@link Map} based on the given {@link Collection}. The keys are * the elements of the Collection, and the values is provided by the given * mapping function. * * @param the type of the input elements, and keys of the Map * @param the output type of the value mapping function * * @param c Collection of elements to map * @param valueMapper a mapping function to produce values * @return a {@code Map} whose keys are as given by the Collection, and values * are the result of applying the given mapping function to the input * elements. */ public static Map identityToValue( Collection c, Function valueMapper) { return c.stream().collect(identityToValue(valueMapper)); } /** * Returns a specialised {@link Collector} which results in a {@link Map} * where the keys are provided by the given mapping function, and the values * are the elements of the stream. * * @param the type of the input elements, and values of the Map * @param the output type of the key mapping function * * @param keyMapper a mapping function to produce keys * @return a {@code Collector} which collects elements into a {@code Map} * whose keys are the result of applying the given mapping function * to the input elements, and values are as given by the stream. */ public static Collector> keytoIdentity( Function keyMapper) { return Collectors.toMap(keyMapper, Function.identity()); } /** * Returns a {@link Map} based on the given {@link Collection}. The keys are * provided by the given mapping function, and the values are the elements of * the Collection. * * @param the type of the input elements, and values of the Map * @param the output type of the key mapping function * * @param c Collection of elements to map * @param valueMapper a mapping function to produce values * @return a {@code Map} whose keys are the result of applying the given * mapping function to the input elements, and values are as given * by the stream. */ public static Map keytoIdentity( Collection c, Function keyMapper) { return c.stream().collect(keytoIdentity(keyMapper)); } @Test public void keytoIdentityTest() { List> classes = Arrays.asList(String.class, ArrayList.class); Map> nameMap = classes.stream() .filter(c -> c.getName().contains("java.lang")) .collect(keytoIdentity(c -> c.getSimpleName())); assertEquals(String.class, nameMap.get("String")); Map> nameMap2 = keytoIdentity(classes, c -> c.getSimpleName()); assertEquals(ArrayList.class, nameMap2.get("ArrayList")); } @Test public void identityToValueTest() { List files = Arrays.asList("index.html", "about.html"); Map fileCache = files.stream() .filter(f -> f.contains("index")) .collect(identityToValue(f -> loadFile(f))); assertEquals("index.html", new String(fileCache.get("index.html"))); Map fileCache2 = identityToValue(files, this::loadFile); assertEquals("about.html", new String(fileCache2.get("about.html"))); } public byte[] loadFile(String name) { // Returns a dummy value for the sake of the test. return name.getBytes(); } }