Win a copy of Serverless Applications with Node.js this week in the NodeJS 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Confused a function in Streams API  RSS feed

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i am reading OCP book by Kathy and Sierra and i got confused with ToDoubleFunction. As far as i understood about a function is that it takes one value and converts to antoher.

Ex: Function<T,R> --> Function takes T as input type and operates on T but return type is R

But in case of ToDoubleFunction<T>, i can see only one paramter. My question is what is the return type of ToDoubleFunction<T> ?

Is ToDoubleFunction<T> acts as UnaryOperator function which takes input as T and return type as also T ?


Thanks & Regards,

Swapan
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swapna,

As its name implies, ToDoubleFunction<T> must return a value of type double. The input can be any type, not necessarily double.

For UnaryOperator<T>, the input and return types must be the same.
 
Swapna latha
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Edmund Yong wrote:Hi Swapna,

As its name implies, ToDoubleFunction<T> must return a value of type double. The input can be any type, not necessarily double.

For UnaryOperator<T>, the input and return types must be the same.



Still its not clear to me. Could you please provide example

Thanks

Swapna
 
Marshal
Posts: 63842
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What on earth you would do with the logarithm of the length of a String, I do not know. You will get an exception if you pass null, and −∞ if you pass the empty String.
 
Swapna latha
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What on earth you would do with the logarithm of the length of a String, I do not know. You will get an exception if you pass null, and −∞ if you pass the empty String.



Thank you Campbell Ritchie. I have executed the code and is as below:

package com.izo809.lambda;

import java.util.function.ToDoubleFunction;

public class ToDoubleFunctionTest {
   public static void main(String[] args){
       ToDoubleFunction<String> logLength = s->s.length();
       System.out.println("logLength.applyAsDouble(\"Hellow world\") = " + logLength.applyAsDouble("Hello world"));
       
       ToDoubleFunction<String> innerClassImplementation = new ToDoubleFunction<String>() {
           @Override
           public double applyAsDouble(String value) {
               return value.length();
           }
       };

       System.out.println("innerClassImplementation.applyAsDouble(\"Welcome\") = " + innerClassImplementation.applyAsDouble("Welcome"));
   }
}

Thanks

Swapna
 
Campbell Ritchie
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!