• Post Reply Bookmark Topic Watch Topic
  • New Topic

Lambda - mapping and streaming only part of a list  RSS feed

 
Tom Worden
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot seem to figure out these two problems. I need to have the list print out but only display the PartDescription and Quantity for the first problem and the next print out the PartDescription and Amount (product of Quantity * Price), I think I may need to use a binary operator. I do not know how to get started on this. Any help would be appreciated. My code is below.



[edited to fix code tags]
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36441
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom,
Welcome to CodeRanch!

For the first one, it sounds like you want to display two fields. You have two choices for this.
  • Replace the method reference in map() with i -> i.getPartDescription() + " " + i.getQuantity()
  • Add a method to Invoice that returns a String with what you want to display


  • For the second one, you can you a similar technique.

    Note that the code you posted does not compile. I think you forgot to remove Comparator<Invoice> _Amount = Comparator.comparing(byAmount);
     
    Tom Worden
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you.
     
    Pierre-Yves Saumont
    Author
    Ranch Hand
    Posts: 98
    17
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Tom,

    The answer given by Jeanne is perfectly correct, and it might be what you are looking for. However, Your code is having design problems. So minimal changes will not really fix it. The major problem is about your model. Your are dealing with invoices. Invoices are made of invoice lines, each line referencing a part and a quantity. So you should start with creating the right model for your problem.

    A Part may be modeled as:



    Note that validation should not be made in the constructor. Constructors should never show exceptions. Also note that the product method could, instead of throwing exceptions, return a special kind of Optional that might contain the exception, letting the choice to the caller to either throw it or do something else. Java 8 does not offer such a class, but I describe one in my book, Functional Programming in Java.

    Next you need an InvoiceLine class to represent each line in an invoice. An InvoiceLine is constructed with a reference to a part and a quantity. This class will also be responsible for calculating the total price for the line, and for providing the comparators:



    Note that you should NEVER use double to represent prices, since prices are fix decimal value while double are floating decimals. But this is another story.

    Then you need an Invoice class to represent each invoice:



    Now, you may write the ProcessInvoices program as:



    Note that there is no need to write something like:



    Doing this could lead the compiler to create unnecessary object. In general, you should not declare explicit object to wrap functions. Lambdas and method references free us from writing anonymous classes, but it also free the compiler (and/or the JVM) to create useless objects.

    However, it might sometimes be interesting to create such objects in order to eliminate duplicate code. For example, you could replace:



    with:



    Not sure if the compiler or the JVM are able to optimize this code by using a method reference for the map parameter, but even if not, this version is better because it reduces duplication.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!