+ 5

Can somone help me understand this

I have give up trying understand how this even works and cant find anything to help me understand can anyone help me step by step please 😔 Does isEqual() method store the value in a field or somthing? I'm not getting how test() returns the boolean how is it able to see what's passed to equals method? 🤯 https://code.sololearn.com/cKRHO8DHaPwd/?ref=app

5th Mar 2021, 1:54 PM
Nobody
Nobody - avatar
18 Answers
+ 6
the value passed in Predicate.isEqual is stored inside the returned object (assigned to 'str')...
5th Mar 2021, 2:05 PM
visph
visph - avatar
+ 2
Predicate.isEqual is a constructor (or call a constructor) and return a new object wich is holding the string passed as argument, and (at least) one method 'test'... inside 'test' method of Predicate.isEqual object, the value passed as argument is available, so it's checked against 'test' argument... boolean is returned by 'test' method ^^
5th Mar 2021, 2:18 PM
visph
visph - avatar
+ 2
Simple way to think about it. When you call the isEqual(var) method It returns the following s->var.equals(s) This lambda expression is the body for test(s) boolean test(s){ return var.equals(s) ^ '------- var here is the string you passed to the IsEquals method }
6th Mar 2021, 11:57 PM
D_Stark
D_Stark - avatar
0
visph im still not getting it 😔 how is there an objevt without using lambda?
5th Mar 2021, 2:14 PM
Nobody
Nobody - avatar
0
visph woah I think my brain died 😆 is it possible to create an example code that does this so I can see how it works. In my mind I'm thinking how is test which is the abstract method of interface getting hold of the argument passed to a static method isequal method are there fields involved in the interface because I can see any
5th Mar 2021, 2:27 PM
Nobody
Nobody - avatar
0
abstract methods are implemented in final classes...
5th Mar 2021, 2:29 PM
visph
visph - avatar
0
visph is the annoumous class the final class that implemented isEquals? or did test become isEquals?
5th Mar 2021, 2:30 PM
Nobody
Nobody - avatar
0
there's no 'anonymous' classes... all the more hidden classes ^^ the final class is the one used to build the object returned by Predicate.isEqual, so the object have a 'test' method implemented...
5th Mar 2021, 2:33 PM
visph
visph - avatar
0
visph Are you able to create an example code I'm so confused because I thought there was an inner annomus object created which was stored to str
5th Mar 2021, 2:36 PM
Nobody
Nobody - avatar
0
OOP: class = blueprint object = instance of class the class is stored outside of instances (but is linked and shared betweens instance objects)
5th Mar 2021, 2:38 PM
visph
visph - avatar
0
visph I know the difference, I know that you cant create an instance of interfaces which made me think that an annoumous inner object is created and stored in str variable
5th Mar 2021, 2:43 PM
Nobody
Nobody - avatar
0
str store a Predicate object with a 'test' method wich use the value passed in Predicate object: there's no needs to have a hidden inner object, but it could be implemented in that way (even there's no real reasons to do so) ^^
5th Mar 2021, 2:48 PM
visph
visph - avatar
0
visph so when I called the static isEqual on the predicate interface did that implemt the test method ? I'm just trying to figure out how test was able to compare both objects..
5th Mar 2021, 2:53 PM
Nobody
Nobody - avatar
0
public interface Predicate<T> { boolean test(T t); static <T> Predicate<T> isEqual(Object target) { return object -> target.equals(object); } } class IsItJava implements Predicate<String> { public boolean test(String s) { return "Java".equals(s); } } public class Program { public static void main(String[] args) { // Predicate<String> str = object -> "Java".equals(object); IsItJava str = new IsItJava(); System.out.println(str.test("Java") ); } }
5th Mar 2021, 6:57 PM
zemiak
0
very simple you just create an object of predicate and give it the value. the object remembers this value you passed to when creating it. when you envoke test method. then you are using a method of that object which you created before. the method test will do the job for you so you can define target value once and test it how many times you need.
6th Mar 2021, 12:17 AM
Saad
Saad - avatar
- 1
Compare the string you passed with function u called
7th Mar 2021, 12:01 PM
Ahmed Mohammed Alnor Abdalmahmod Abdalslam
Ahmed Mohammed Alnor Abdalmahmod Abdalslam - avatar
- 1
java is
8th Mar 2021, 11:39 AM
الطيب محمد إسحق محمد الطيب محمد إسحق محمد
الطيب محمد إسحق محمد الطيب محمد إسحق محمد - avatar
- 3
Can anyone please tell me why range slider is not working Help me! https://code.sololearn.com/W5UHHJVz70nV/?ref=app
7th Mar 2021, 2:27 AM
Rohan Prasad
Rohan Prasad - avatar