Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Java equivalent for Python's map function?

I would like to easily transform a collection (list) of objects of class A to a collection of objects of class B, just as Python's map function does it. Is there any "well-known" implementation of this (some kind of library)? I've already searched for it in Apache's commons-lang but with no luck.

like image 381
gregorej Avatar asked Dec 21 '10 13:12

gregorej


People also ask

Is there a map function in Java?

The map() function is a method in the Stream class that represents a functional programming concept. In simple words, the map() is used to transform one object into other by applying a function. That's why the Stream. map(Function mapper) takes a function as an argument.

Is Java map the same as Python Dictionary?

One difference between the two is that dict has stricter requirements as to what data types can act as a key. Java will allow any object to work as a key -- although you should take care to ensure that the object's hashCode() method returns a unique value that reflects its internal state.

What is the equivalent of map in Python?

Python's map() is a built-in function that allows you to process and transform all the items in an iterable without using an explicit for loop, a technique commonly known as mapping. map() is useful when you need to apply a transformation function to each item in an iterable and transform them into a new iterable.

Does Python have a map object?

Python map() function is used to apply a function on all the elements of specified iterable and return map object. Python map object is an iterator, so we can iterate over its elements. We can also convert map object to sequence objects such as list, tuple etc. using their factory functions.


1 Answers

Starting from Java 8, it can be done thanks to the Stream API using an appopriate mapper Function that we will use to convert our instances of class A to instances of class B.

The pseudo code would be:

List<A> input = // a given list of instances of class A
Function<A, B> function = // a given function that converts an instance 
                          // of A to an instance of B
// Call the mapper function for each element of the list input
// and collect the final result as a list
List<B> output = input.stream().map(function).collect(Collectors.toList());

Here is a concrete example that will convert a List of String to a List of Integer using Integer.valueOf(String) as mapper function:

List<String> input = Arrays.asList("1", "2", "3");
List<Integer> output = input.stream().map(Integer::valueOf).collect(Collectors.toList());
System.out.println(output);

Output:

[1, 2, 3]

For previous versions of Java, you can still use FluentIterable from Google Guava to replace the Stream and use com.google.common.base.Function instead of java.util.function.Function as mapper function.

The previous example would then be rewritten as next:

List<Integer> output = FluentIterable.from(input)
    .transform(
        new Function<String, Integer>() {
            public Integer apply(final String value) {
                return Integer.valueOf(value);
            }
        }
    ).toList();

Output:

[1, 2, 3]
like image 163
Nicolas Filotto Avatar answered Sep 23 '22 09:09

Nicolas Filotto