Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

UnaryOperator cannot be found

 
Greenhorn
Posts: 26
  • 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: 69847
278
  • 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: 26
  • 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: 69847
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why have you got your own UnaryOperator file?
 
kiros haregewoine
Greenhorn
Posts: 26
  • 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: 69847
278
  • 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.
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic