DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Java Holiday Calendar 2016 (Day 10): MapStream

Java Holiday Calendar 2016 (Day 10): MapStream

The open-source class MapStream lets you stream over elements as well as pairs of key value elements, making changes to everything along the way.

Per-Åke Minborg user avatar by
Per-Åke Minborg
·
Dec. 10, 16 · Java Zone · Tutorial
Like (7)
Save
Tweet
9.51K Views

Join the DZone community and get the full member experience.

Join For Free


Image title


Today's tip is about the open-source class MapStream, which allows us to stream not only over elements, but over a pair of key value elements and make changes either to the keys, values, or both.

You can find the source code for MapStream here together with some examples of how to use it. It is free, so go ahead and use or copy it in your application! MapStream is a part of Speedment.

With MapStream, you can do this:

Map<String, Integer> numberOfCats = new HashMap<>();

numberOfCats.put("Anne", 3);
numberOfCats.put("Berty", 1);
numberOfCats.put("Cecilia", 1);
numberOfCats.put("Denny", 0);
numberOfCats.put("Erica", 0);
numberOfCats.put("Fiona", 2);

System.out.println(
  MapStream.of(numberOfCats)
      .filterValue(v -> v > 0)
      .sortedByValue(Integer::compareTo)
      .mapKey(k -> k + " has ")
      .mapValue(v -> v + (v == 1 ? " cat." : " cats."))
      .map((k, v) -> k + v)
      .collect(Collectors.joining("\n"))
);


This would produce the following:

Cecilia has 1 cat.
Berty has 1 cat.
Fiona has 2 cats.
Anne has 3 cats.


Read more about MapStream here.

Follow the Java Holiday Calendar 2016 with small tips and tricks all the way through the winter holiday season.

Calendar (Apple) Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Event-Driven Hello World Program
  • Dynamically Provisioning Persistent Volumes with Kubernetes
  • Build a Data Pipeline on AWS With Kafka, Kafka Connect, and DynamoDB
  • Streaming ETL with Apache Kafka in the Healthcare Industry

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo