tags:

views:

214

answers:

3

I need a map function. Is there something like this in Java already?

(For those who wonder: I of course know how to implement this trivial function myself...)

+7  A: 

There is a wonderful library called Functional Java which handles many of the things you'd want Java to have but it doesn't. Then again, there's also this wonderful language Scala which does everything Java should have done but doesn't while still being compatible with anything written for the JVM.

wheaties
I am interested in how did they enable following syntax: `a.map({int i => i + 42});` did they extend compiler? or added preprocessor?
Andrey
@Andrey - You can either ask them that yourself or check out the source code to see how it's done. Here's a link to the source: http://functionaljava.org/source/
wheaties
@Andrey: examples use syntax from BGGA closures proposal. While there is running prototype, it's not in 'official' Java yet.
Peter Štibraný
@Andrey: that syntax is part of a proposed specification for closures in Java (see second-to-last paragraph on homepage). There's only a prototypical implementation.
Michael Borgwardt
to be fair: scala is not the only choice. Groovy, Clojure, JRuby and probably many others all offer this functionality.
seanizer
@Peter Štibraný but who compiles that code? javac? or they first use their preprocessor?
Andrey
Scala thread hijack :( I hope SO won't become like the JavaPosse mailing list ;)
Jorn
@seanizer , C# :-P
Andrey
@Andrey on the Java VM? I don't think so... :-)
seanizer
There are more functional libraries for Java, Functional Java being oen of them. See http://stackoverflow.com/questions/1267297/functional-programming-in-java
Adam Schmideg
@Adam true, but I'd still say that FunctionalJava and Guava are the two most promising options
seanizer
@Andrey: modified Javac can compile that code. It is available at http://javac.info/ (look for 'prototype'). But I think that Functional Java page use it only as an uncompilable example.
Peter Štibraný
+9  A: 

There is no notion of a function in the JDK as of java 6.

Guava has a Function interface though and the
Collections2.transform(Collection, Function)
method provides the functionality you require.

Example:

// example, converts a collection of integers to their
// hexadecimal string representations
final Collection<Integer> input = Arrays.asList(10, 20, 30, 40, 50);
final Collection<String> output =
    Collections2.transform(input, new Function<Integer, String>(){

        @Override
        public String apply(final Integer input){
            return Integer.toHexString(input.intValue());
        }
    });
System.out.println(output);

Output:

[a, 14, 1e, 28, 32]

seanizer
+3  A: 

map(function,iterable) i think this style of coding is with functional style of language, languages that support passing function as a argument. So this is possibly not in java.

sushil bharwani
Luckily this style of programming is becoming more popular in Java, even though Java was not explicitly designed to support it.
Jorn
agree with your comment. There are so many languages like javascript scala ruby using functional style or (lambda) programming. Which makes there code so dynamic. Java classicaly tries this through the use of annonymous inner classes.
sushil bharwani