Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Function object
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== In Java == [[Java (programming language)|Java]] has no [[first-class function]]s, so function objects are usually expressed by an interface with a single method (most commonly the <code>Callable</code> interface), typically with the implementation being an anonymous [[inner class]], or, starting in Java 8, a [[anonymous function|lambda]]. For an example from Java's standard library, <code>java.util.Collections.sort()</code> takes a <code>List</code> and a functor whose role is to compare objects in the List. Without first-class functions, the function is part of the Comparator interface. This could be used as follows. <syntaxhighlight lang="java"> List<String> list = Arrays.asList("10", "1", "20", "11", "21", "12"); Comparator<String> numStringComparator = new Comparator<String>() { public int compare(String str1, String str2) { return Integer.valueOf(str1).compareTo(Integer.valueOf(str2)); } }; Collections.sort(list, numStringComparator); </syntaxhighlight> In Java 8+, this can be written as: <syntaxhighlight lang="java"> List<String> list = Arrays.asList("10", "1", "20", "11", "21", "12"); Comparator<String> numStringComparator = (str1, str2) -> Integer.valueOf(str1).compareTo(Integer.valueOf(str2)); Collections.sort(list, numStringComparator); </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)