• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Lambda - mapping and streaming only part of a list

 
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
author & internet detective
Posts: 42055
926
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you.
     
    Author
    Posts: 161
    31
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic