The map
function defined in the Stream
interface takes Function
as an input, it's an intermediate
operation. The map takes a stream
as an input, applies a function
on it, object type and produces a new Stream
which can be of a different type.
package org.wesome.java8;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
class Apple {
public static void main(String args[]) {
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println("*---------------------------Traditional method---------------------------*");
for (Integer integer : integers) {
System.out.println(integer * integer);
}
Function<Integer, Integer> integerFunction = i -> i * i;
System.out.println("*---------------------------Predicate---------------------------*");
integers.stream().map(integerFunction).forEach(System.out::println);
System.out.println("*---------------------------Predicate as method argument---------------------------*");
integers.stream().map(integer -> filteredList(integer, integerFunction)).forEach(System.out::println);
System.out.println("*---------------------------Inline Predicate---------------------------*");
integers.stream().map(integer -> integer * integer).forEach(System.out::println);
}
private static Integer filteredList(Integer integer, Function<Integer, Integer> function) {
return function.apply(integer);
}
}
Stream Map on Java Map
package org.wesome.java8;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
class Apple {
public static void main(String args[]) {
Map<Integer, String> map = new HashMap();
map.put(1, "Macintosh");
map.put(2, "Fuji");
map.put(3, "Gala");
map.put(4, "Jonagold");
System.out.println("*---------------------------Traditional method---------------------------*");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue().concat(" Apple"));
}
Function<Map.Entry<Integer, String>, Map> function = entry -> {
Map tempMap = new HashMap<>();
tempMap.put(entry.getKey(), entry.getValue().concat(" Apple"));
return tempMap;
};
System.out.println("*---------------------------Predicate---------------------------*");
map.entrySet().stream().map(function).forEach(System.out::println);
System.out.println("*---------------------------Predicate as method argument---------------------------*");
map.entrySet().stream().map(entry -> filteredList(entry, function)).forEach(System.out::println);
System.out.println("*---------------------------Inline Predicate---------------------------*");
map.entrySet().stream().map(entry -> entry.setValue(entry.getValue().concat(" APple"))).forEach(System.out::println);
}
private static Map filteredList(Map.Entry<Integer, String> map, Function<Map.Entry<Integer, String>, Map> function) {
return function.apply(map);
}
}
Stream Map on user-defined function
package org.wesome.java8;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
@Data
@AllArgsConstructor
class Fruit {
int fruitId;
String fruitName;
}
class Apple {
public static void main(String args[]) {
List<Fruit> fruits = Arrays.asList(new Fruit(1, "Macintosh"), new Fruit(2, "Fuji"), new Fruit(3, "Gala"), new Fruit(4, "Jonagold"));
System.out.println("*---------------------------Traditional method---------------------------*");
for (Fruit fruit : fruits) {
System.out.println(new Fruit(fruit.getFruitId() * fruit.getFruitId(), fruit.getFruitName().concat(" Apple")));
}
Function<Fruit, Fruit> fruitFunction = (Fruit fruit) -> new Fruit(fruit.getFruitId() * fruit.getFruitId(), fruit.getFruitName().concat(" Apple"));
System.out.println("*---------------------------Predicate---------------------------*");
fruits.stream().map(fruitFunction).forEach(System.out::println);
System.out.println("*---------------------------Predicate as method argument---------------------------*");
fruits.stream().forEach(entry -> System.out.println(filteredList(entry, fruitFunction)));
System.out.println("*---------------------------Inline Predicate---------------------------*");
fruits.stream().map(fruit -> new Fruit(fruit.getFruitId() * fruit.getFruitId(), fruit.getFruitName().concat(" Apple"))).forEach(System.out::println);
}
private static Fruit filteredList(Fruit fruit, Function<Fruit, Fruit> function) {
return function.apply(fruit);
}
}
Stream Map on User Defined Object
package org.wesome.java8;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
@Data
@AllArgsConstructor
class Fruit {
int fruitId;
String fruitName;
}
class Apple {
public static void main(String args[]) {
List<Fruit> fruits = Arrays.asList(new Fruit(1, "Macintosh"), new Fruit(2, "Fuji"), new Fruit(3, "Gala"), new Fruit(4, "Jonagold"));
System.out.println("*---------------------------Traditional method---------------------------*");
for (Fruit fruit : fruits) {
System.out.println(new Fruit(fruit.getFruitId() * fruit.getFruitId(), fruit.getFruitName().concat(" Apple")));
}
Function<Fruit, Fruit> fruitFunction = (Fruit fruit) -> new Fruit(fruit.getFruitId() * fruit.getFruitId(), fruit.getFruitName().concat(" Apple"));
System.out.println("*---------------------------Predicate---------------------------*");
fruits.stream().map(fruitFunction).forEach(System.out::println);
System.out.println("*---------------------------Predicate as method argument---------------------------*");
fruits.stream().forEach(entry -> System.out.println(filteredList(entry, fruitFunction)));
System.out.println("*---------------------------Inline Predicate---------------------------*");
fruits.stream().map(fruit -> new Fruit(fruit.getFruitId() * fruit.getFruitId(), fruit.getFruitName().concat(" Apple"))).forEach(System.out::println);
}
private static Fruit filteredList(Fruit fruit, Function<Fruit, Fruit> function) {
return function.apply(fruit);
}
}