• 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

UnaryOperator cannot be found

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was studying  functional programming unaryoperator.
<code>
UnaryOperator<String> u1 = String::toUpperCase;
</code>
could not compile. but for function
<code>
Function<String,Integer> f1=String::length;
</code>
compiles.
I have imported java.util.function.*;

Is there command line to list if util package contains a function?
 
Marshal
Posts: 67443
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding discussion to our λs/Streams forum.

Most of the predefined functional interfaces are in the [java.base/]java.util.function package. So let's look there, and lo and behold, there is UnaryOperator<T>. It shoiuld be used with something like what you wrote:-I tried this code, copied from your post, on JShell, and it compiled first try.

jshell> UnaryOperator<String> u1 = String::toUpperCase;
u1 ==> $Lambda$25/0x0000000800b6ac40@5a10411

jshell> u1.a
andThen(   apply(    
jshell> u1.apply("Campbell");
$2 ==> "CAMPBELL"

Please tell us what error message you are getting, and how you are importing UnaryOperator. The following won't work because the package name is wrong:-
 
kiros haregewoine
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the import is
import java.util.function.*;

bad source file: ./UnaryOperator.java

on linux´╝Ćubuntu, openjdk
 
Campbell Ritchie
Marshal
Posts: 67443
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why have you got your own UnaryOperator file?
 
kiros haregewoine
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surprising the file is there by mistake. First I created file UnaryOperator.java and later changed file to TestUnaryOperator.java noticing conflict but forgot to delete the file. Now I deleted the file and the code compiles. Thank you.
 
Campbell Ritchie
Marshal
Posts: 67443
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure

Another lesson learnt by surprise Beware of two classes with the same name.
 
Straws are for suckers. Now suck on this tiny ad!
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!