Java flatMap

Java flatMap

In this Java 8 tutorial you can learn about what a flatMap is and in which scenario it can be used. 

flatMap is an intermediate operation of the Java 8 Stream API

If you are just into Java 8 and streams, I strongly recommend you to go through the linked tutorial to learn about streams and operations.

Broadly stream operations are classified as intermediate and terminal operations. 

flatMap is an intermediate operation, which returns a new stream. 

Intermediate operations are classified as stateless and stateful operations based on need for sharing information between elements when processing them. 

flatMap is a stateless intermediate operation.

Java-flatMap

As per the Javadoc,

“…Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element…”

Following example will help you to understand this flatMap and a possible example scenario. 

Let us consider a zoo and the list of animals in it. Then we can use the flatMap to get an aggregate of animals in a list of Zoo. 

If we use the Pre-Java8 approach, we would be doing this by using multiple for-loops. 

Now the same can be achieve with Java 8 Stream API as below.

flatMap Example

package com.javapapers.java;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class FlatMapExample {

	public static void main(String args[]) {
		List<Zoo> zooList = new ArrayList<>();
		Zoo nationalZoo = new Zoo("National");
		nationalZoo.add("Lion");
		nationalZoo.add("Tiger");
		nationalZoo.add("Peacock");
		nationalZoo.add("Gorilla");

		Zoo aCountyZoo = new Zoo("Wills County");
		aCountyZoo.add("Peacock");
		aCountyZoo.add("Camelion");

		zooList.add(nationalZoo);
		zooList.add(aCountyZoo);

		// to get the aggregate
		List<String> animalList = zooList.stream()
				.flatMap(element -> element.getAnimals().stream())
				.collect(Collectors.toList());
		System.out.println(animalList);

		// to get the unique set
		Set<String> animalSet = zooList.stream()
				.flatMap(element -> element.getAnimals().stream())
				.collect(Collectors.toSet());
		System.out.println(animalSet);

	}

}

class Zoo {
	private String zooName;
	private Set<String> animals;

	public Zoo(String zooName) {
		this.zooName = zooName;
		this.animals = new HashSet<>();
	}

	public void add(String animal) {
		this.animals.add(animal);
	}

	public Set<String> getAnimals() {
		return animals;
	}
}

Example Output

[Peacock, Lion, Tiger, Gorilla, Peacock, Camelion]
[Peacock, Lion, Tiger, Gorilla, Camelion]

Another year has passed and we are into a new year. Wishing you a happy new year and I wish you to achieve all your dreams.

For the regular reader of Java Papers, you might have noticed an unusual silence for the past two months. 

I am working on a project related to technology learning and it took all of my time. 

I will be launching it very soon to you and it will be maintained in-tandem with Java Papers. 

We have lot more coming up in Java Papers in 2016. 

My two resolutions for Java Papers are, as always continue to publish regularly and remove advertisements gradually from the blog. 

Keep reading, sharing and rocking!

Comments on "Java flatMap" Tutorial:

  1. Swetja  says:

    Happy new year Joe.

  2. Patrick  says:

    So nice to hear about your resolutions for Java Papers. Keep flying!

  3. NAGENDRA  says:

    Happy new year Joe.

  4. deepak  says:

    If you will be removing the advertisement completely, then how you are gonna make this site working???

  5. Thiru  says:

    HI Joe,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值