• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generic Containers  RSS feed

 
Greenhorn
Posts: 23
IntelliJ IDE Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I' ve got problem with implementation of code that was shown on my class. Can you explain how it works and how to use it?

 
Sheriff
Posts: 4865
136
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may get a better response if you ask a specific question.  What is it you don't understand?
 
Sheriff
Posts: 21325
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like the look of that reflection now that Java 8 is here. Instead of reflection, you can use the fact that a method or constructor can act as a function:

Usage:
 
Andrzej Zahorski
Greenhorn
Posts: 23
IntelliJ IDE Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All right I think I sorted it out, please correct me if I'm wrong:
This is class for instatation objects (which are subtype of collection) into collection.
First method is finding constructor specified for argument, and second makes objects with default constructor. 
Thanks Rob for this Java 8 trick, but I am not familiar with those things, could explain how this Function and Supplier works? Also I've never seen (in Java) this '::' thing
 
Marshal
Posts: 59074
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It the Function interface had been introduced in Java7, you would have to instantiate it like this:-or like this:-...and you can use it (named class version) in code like this:-...where f is an instance of Foo. We should all be familiar with that sort of code. In Java8 the compiler was enhanced to infer most of the content of that sort of code. Let's see how much we can leave out, starting with the anonymous class version in full. You have a Stream<Foo>, which means it will pass a reference to a Foo instance to whatever comes next, and this code will return a reference to a Stream<Bar>:-Now, you need to work out what the compiler “knows” about that anonymous class. It knows it has to be a Function<T, R> because that is what the method takes as its argument. So we can remove the interface name and one pair of {} delimiting the class:-It also “knows” that Function has one abstract method and one only which makes it a FunctionalInterface; it even says so in the documentation link I posted earlier. So who needs the method name, or return type because you can infer it is Bar apply?You no longer need the {} as long as you remove return, but you do need to add an arrow token:-In 90%+ of cases, it is possible to infer the type of the argument from the type of the preceding Stream, so we can presume it is a Foo, which means we can delete the type: we can also remove the () because they would now contain one token and () are redundant around a single token:-At which point you have the shortest λ you can get. But what are you doing with f? Nothing. You are simply passing it to a constructor call and the constructor may do something with it. So you have a shorthand (details in the Java™ Tutorials) for

Take that one reference and pass it to this method or constructor

What you do is write the name of an object or a class, double colon and the name of the method. Since constructors aren't methods, you can't give them a method name. But a constructor is called by means of the new keyword or operator, so they reused new.And that is what Rob told you about.
 
Bartender
Posts: 1802
45
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It the Function interface had been introduced in Java7, you would have to instantiate it like this...


Just nitpicking ... I guess Functional interface was introduced in Java8.
https://docs.oracle.com/javase/9/docs/api/java/lang/FunctionalInterface.html says :

Java doc wrote:Since:
1.8

 
Do not set lab on fire. Or this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!