Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

BiPredicate as function argument

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was searching for BiPredicate examples.

I found it here 2. BiPredicate as function argument

But this code gave two compile time errors, which are attached herewith.

Though, comments are given along with code, but those comments are insufficient to understand flow of program

I request that any developer please help me with rectifying bugs as well as writing detailed comments after each line.
092-BiPredicate-Example2-error.png
Error thrown by Java 8
Error thrown by Java 8
 
Saloon Keeper
Posts: 6798
162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Note this comment in the Domain class:

// getters , setters , toString

That means you need to add the usual getter and setter methods: setName, getName, setScore and getScore. (toString is not needed for this example.)
 
Sukumar Sat
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:Welcome to the Ranch.

Note this comment in the Domain class:

// getters , setters , toString

That means you need to add the usual getter and setter methods: setName, getName, setScore and getScore. (toString is not needed for this example.)



Tim,

Thank you,

Can you please help me by explaining class Domain and filterBadDomain method?

How it is helping entire program?

Sukumar
 
Tim Moores
Saloon Keeper
Posts: 6798
162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Domain class has no particular purpose except to illustrate the concept of BiPredicate - it models an internet domain (like "google.com") along with a "goodness" score of that domain.

The filterBadDomain method simply applies a Bipredicate to a List of domains. it's shown in action several times in the code; did you get that to run now? That should be the first step in learning from this example, as it makes it much easier to understand what's going on. (Although the source code does actually show the output of each of the System.out.println statements.)
 
Sukumar Sat
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:The Domain class has no particular purpose except to illustrate the concept of BiPredicate - it models an internet domain (like "google.com") along with a "goodness" score of that domain.

The filterBadDomain method simply applies a Bipredicate to a List of domains. it's shown in action several times in the code; did you get that to run now? That should be the first step in learning from this example, as it makes it much easier to understand what's going on. (Although the source code does actually show the output of each of the System.out.println statements.)



Tim,

Thank you again,

I have amended code and shared working code below with some questions, for which I need your help to understand.

I understood that in Java 8, BiPredicate is a functional interface, which accepts two arguments and returns a boolean

Please help me with answers to those Six requested questions asked in comments within below code.
Please give any Reference to elaborate my Learning.

093-BiPredicate-Example2-error-corrected.png
output after error corrected
output after error corrected
 
Ranch Foreman
Posts: 175
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Suakamar:

I happen to be studying the same topic right now!  First, let's clear up one bit of background info:


   1. Arrays.asList() creates a fix-sized List "view" of an array of Domain objects.
   Instead of separately creating an array and then passing that in as a named
   array which will back the List<Domain> that is created, we save lines by
   creating an anonymous Array.  This is a common way to conveniently create
   a fixed-size but mutable List which is just a view of the Array, meaning
   that all changes to Array elements show in the List that is backed by it, and
   vice versa.  Any attempts to add or remove elements to the fixed size list
   will throw runtime exceptions.
   https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#asList(T...)


   You might have been less confused by:


   However, as written, this is a very common idiom which as you can see saves
   a lot of space when all the work you will be doing is via. the List interface.
   You should know it because it is a very common idiom in Java, but is not
   at all specific to functional programming or lambdas or streams.
 
Jesse Silverman
Ranch Foreman
Posts: 175
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


   Questions 2, 3 and 4 are indeed about the basics of lambda expression syntax
   2. domain and score are "parameters to the lambda expression".
       They can basically be thought of as parameters to the anonymous method
       you are creating, but there are some scoping rules to pay attention to
       that this simplification would confuse one with.
   3. No, it is the body of the lambda expression, the work that gets carried
       out when it is called.  In the trivial case that one writes just a
       single expression that gets returned, we can leave out the return and {}
   4. This is calling a method on the reference variable domain which was one
       of two parameters to the lambda expression.  It is a normal instance
       method call that happens to be in the body of this lambda expression.
There are countless pretty good lambda expressions basics tutorials.
Which ones you might prefer depend on which accents you find most comprehensible,
and your general level of knowledge in Java, Computer Science and programming.
Here are a few that I watched while I was learning.
There are some excellent ones that contain WAY more info, too much beyond the
scope of basic lambda expression syntax:
https://www.youtube.com/watch?v=lIXs4Y8sJCk
https://www.youtube.com/watch?v=jf3VVYHUuVA
https://www.youtube.com/watch?v=bnuQpz-DK3w&list=PLd3UqWTnYXOmXMlfkwocAUzyOYxMZ7cyw&index=2

This one is great but it covers a LOT of stuff!
He used to post on this site a lot when his book was new, but he is a full-time
professor and is mostly seen in person by students (probably online now due to COVID)
and in his commercial books:
https://www.youtube.com/watch?v=1OpAgZvYXLQ

If you watch that say goodbye to your loved ones for several hours, but he is great and you will learn a lot.

If you seek certification for Java 11 these topics are covered as part of that, in somewhat less detail and together with many, many other topics that are considered essential for certification or doing professional Java work.  One I am reading is seen here:
https://www.amazon.com/Oracle-Certified-Professional-Programmer-Study/dp/1119617626/ref=sr_1_10?dchild=1&keywords=selikoff+boyarsky&qid=1610055091&s=books&sr=1-10

That is the second of two books preparing seekers of the !Z0-819 certification from Oracle.
The authors frequent this site and errata reports and questions normally come thru this site.

If you aren't looking to take a certification exam, they are still good books, but some of the links given prior to that should get you comfortable with lambda expression syntax.
 
Jesse Silverman
Ranch Foreman
Posts: 175
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Questions 5 and 6 show that you need to catch up on the basics of Generics usage in Java.
Generics are not at all specific to functional programming, streams or lambda expressions, they have been around since Java 5.

There are many good tutorials on the basics of generics in Java, several can be found here:
https://www.youtube.com/results?search_query=java+generics+tutorial

It seems like you are trying to learn a LOT at once here.

Each individual thing is not particularly hard (knowing them well enough to ace a certification exam is pretty tricky, tho).

Learning them all at once can make your head swim.

So, a presentation on the basics of functional programming in Java is often going to presume you know the following pretty well:
1. The basics of Java Collections Frameworks (you seem to have some familiarity with that, there are countless tutorials to take you further, some are even very good)
2. Generics Basics in Java -- specifically, you were getting confused by an unfamiliar syntax which defines methods with generic parameters.
3. Lambda Expression basics
4. Basic functional interfaces.

If you are pretty solid on those pre-requisites, learning streams isn't so bad, tho there is a lot to potentially know to make full use of them.

Trying to learn the first four at the same time as streams can really drown you tho, many of your questions are valid and good but I would consider familiarity with the answers to be a Pre-Req, rather than a Co-Requisite for learning Streams in Java.
 
Jesse Silverman
Ranch Foreman
Posts: 175
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Standard and free, here are the official Oracle tutorials on Collections and Generics.  They cover some of the basics that seemed to be making it more difficult for you to understand the presentation you had found on BiPredicate<> which presumes familiarity with these topics:

https://docs.oracle.com/javase/tutorial/collections/index.html

https://docs.oracle.com/javase/tutorial/extra/generics/index.html
 
Jesse Silverman
Ranch Foreman
Posts: 175
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the list of Pre-Requisites that might be necessary to fully understand the example the Original Poster had found and presented I believe I neglected VarArgs, also "new" for Java 5 in 2004.

A one sentence description would be that it allows you to treat zero or more arguments of the same type occurring at the end of a parameter list, or the only elements of a parameter list as an array, or to actually pass in an array, but longer presentations beat it to death for over an hour showing how it interacts with method overloading.

It is useful in many places in Java, and tricky, so researching VarArgs in Java would also yield a number of helpful tutorials.

I note that when diving into Functional Programming with Streams, Jeanne and Scott warn that one had better be on top of all of the topics mentioned before moving forward further to avoid some serious confusion or feeling overwhelmed.  Many otherwise good presentations including at least a couple I linked to presume you have been using these pre-requisites daily in code for quite some time and that you are quite familiar with their nuances.
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic