Mapping List objects using lambdas and streams

Mapping List objects using lambdas and streams

To start with, I have the following list of invoices. Each list object has a part number, a description, quantity and a price.

To start with, I have the following list of invoices. Each list object has a part number, a description, quantity and a price.

Invoice[] invoices = new Invoice[8];
invoices[0] = new Invoice("83","Electrische schuurmachine",7,57.98);
invoices[1] = new Invoice("24","Power zaag", 18, 99.99);
invoices[2] = new Invoice("7","Voor Hamer", 11, 21.50);
invoices[3] = new Invoice("77","Hamer", 76, 11.99);
invoices[4] = new Invoice("39","Gras maaier", 3, 79.50);
invoices[5] = new Invoice("68","Schroevendraaier", 16, 6.99);
invoices[6] = new Invoice("56","Decoupeer zaal", 21, 11.00);
invoices[7] = new Invoice("3","Moersleutel", 34, 7.50);

List<Invoice> list = Arrays.asList(invoices);

What's asked: Use lambdas and streams to map every Invoice on PartDescription and Quantity, sort by Quantity and show the results.

So what I do have now:

list.stream()
    .map(Invoice::getQuantity)
    .sorted()
    .forEach(System.out::println);

I mapped it on quantity and sorted it on quantity as well and I get below results:

3
7
11
16
18
21
34
76

But how do I map on PartDescription as well, so that's showed in my results in front of the shown quantities too? I can't do this:

list.stream()
    .map(Invoice::getPartDescription)
    .map(Invoice::getQuantity)
    .sorted()
    .forEach(System.out::println);


java

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Java Core (2020) | Java for beginners | Brush up your Java Skills | Get Hired

In this video we will learn about all the major concepts that come under Java Core . The course is very carefully designed and is made with keeping simplicit...

Java Fundamentals: Learn Java for absolute beginners |Simpliv

Java Fundamentals: Learn Java for absolute beginners

Java Essentials : Learn Core Java From Basic to Advance

Learn Java Programming Using Practical Assignments. Start Building Back-end Web Applications Robust Test Automation Frameworks By End Of The Course. Learn More!

Top 10 Java Frameworks For Web App Development: 2020 Edition

Java Frameworks are the pre-written code body through which you are enabled to add your own code. There are numerous Java frameworks that have different leading features, on the basis of the same here, I will discuss Top 10 JAVA Frameworks For Web App Development 2020-2021. 

How Java Became The Best Career Option for IT Jobs in 2020?

Join CETPA, India's best institute for Java online training and Certification Course. Enroll now to learn Core to Advanced Java & avail 50% discount on Java Online Course with Certificate.