Since Java 8, there are some standard options to do this in JDK:

Collection<E> in = ...
Object[] mapped = in.stream().map(e -> doMap(e)).toArray();
// or
List<E> mapped = in.stream().map(e -> doMap(e)).collect(Collectors.toList());

See java.util.Collection.stream() and java.util.stream.Collectors.toList().

Answer from leventov on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Map.html
Map (Java Platform SE 8 )
1 month ago - remappingFunction - the function to recompute a value if present ... ClassCastException - if the class of the specified key or value prevents it from being stored in this map (optional) NullPointerException - if the specified key is null and this map does not support null keys or the value or remappingFunction is null ... Java...
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › util › Map.html
Map (Java SE 11 & JDK 11 )
January 20, 2026 - If the mapping function returns null, no mapping is recorded. If the mapping function itself throws an (unchecked) exception, the exception is rethrown, and no mapping is recorded. The most common usage is to construct a new object serving as an initial mapped value or memoized result, as in:
🌐
W3Schools
w3schools.com › java › java_map.asp
Java Map
Java Examples Java Videos Java ... ... The Map interface is a part of the Java Collections Framework and is used to store key-value pairs....
🌐
GeeksforGeeks
geeksforgeeks.org › java › stream-map-java-examples
Stream map() in Java with examples - GeeksforGeeks
January 4, 2025 - Stream is an interface and T is the type of stream elements. mapper is a stateless function which is applied to each element and the function returns the new stream. ... Stream map() function with operation of number * 3 on each element of stream.
🌐
Java67
java67.com › 2015 › 01 › java-8-map-function-examples.html
Java 8 Stream map() function Example with Explanation | Java67
By using the map() function, you can apply any function to every element of the Collection. It can be any predefined function or a user-defined function. You not only can use the lambda expression but also method references. One example of Map in Java 8 is to convert a list of integers and then the square of each number.
🌐
Medium
medium.com › @AlexanderObregon › javas-stream-map-method-explained-df0d0d461d39
Java’s Stream.map() Method Explained | Medium
August 27, 2024 - The map() method uses this function to apply a transformation to each element in the stream. This approach not only simplifies the code but also leverages the power of lambda expressions and method references in Java, leading to more concise ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › map-interface-in-java
Map Interface in Java - GeeksforGeeks
In Java, the Map Interface is part of the java.util package and represents a collection of key-value pairs, where Keys should be unique, but values can be duplicated.
Published   January 7, 2026
Find elsewhere
🌐
CodingBat
codingbat.com › doc › java-functional-mapping.html
Java Functional Mapping
Here is a mapping solution: public List<Integer> doubling(List<Integer> nums) { nums.replaceAll(n -> n * 2); return nums; } How does the above code work? First look at this snippet of code: n -> n * 2 · Mapping uses a little function that takes in one item and computes the new value for that item.
🌐
Belief Driven Design
belief-driven-design.com › functional-programm-with-java-map-filter-reduce-77e479bd73e
Functional Programming With Java: map, filter, reduce | belief driven design
Stream#map(Function<T> mapper) is an intermediate stream operation that transforms each element. It applies its argument, a Function<T, R>, and returns a Stream<R>:
🌐
Simplilearn
simplilearn.com › home › resources › software development › map in java: all about map interface in java
Map in Java: All About Map Interface in Java
July 16, 2024 - Map in java is an interface available in java.util package that represents a mapping between key and value. Start learning about map interface in java now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
DZone
dzone.com › coding › languages › java 8 map, filter, and collect examples
Java 8 Examples: Map, Filter and Collect
June 21, 2018 - All you need is a mapping function to convert one object to the other. Then, the map() function will do the transformation for you. It is also an intermediate Stream operation, which means you can call other Stream methods, like a filter, or collect on this to create a chain of transformations.
🌐
W3Schools
w3schools.com › java › java_ref_hashmap.asp
Java HashMap Reference
Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
🌐
Educative
educative.io › answers › what-is-the-stream-map-method-in-java
What is the Stream map() method in Java?
This method accepts a Function mapper object as an argument. This function describes how each element in the original stream is transformed into an element in the new stream.
🌐
Javatpoint
javatpoint.com › java-map
Java Map Interface - javatpoint
Java Map interface provides methods for storing values based on key basis. Methods of Map interface, methods of map.entry interface.
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java stream map()
Java Stream map() with Examples - HowToDoInJava
August 26, 2023 - Java 8 Stream.map() operation transforms the elements of a stream from one type to another. After the map() operation completes, for each element of type X in the current Stream, a new object of type Y is created and put in the new Stream.
Top answer
1 of 3
146

With Java 8+ and Lambda expressions

With lambdas (available in Java 8+) we can do it as follows:

class Test {
    
    public static void main(String[] args) throws Exception {
        Map<Character, Runnable> commands = new HashMap<>();
        
        // Populate commands map
        commands.put('h', () -> System.out.println("Help"));
        commands.put('t', () -> System.out.println("Teleport"));
        
        // Invoke some command
        char cmd = 't';
        commands.get(cmd).run();   // Prints "Teleport"
    }
}

In this case I was lazy and reused the Runnable interface, but one could just as well use the Command-interface that I invented in the Java 7 version of the answer.

Also, there are alternatives to the () -> { ... } syntax. You could just as well have member functions for help and teleport and use YourClass::help resp. YourClass::teleport instead.

  • Oracle tutorial here: The Java Tutorials – Lambda Expressions.

Java 7 and below

What you really want to do is to create an interface, named for instance Command (or reuse for instance Runnable), and let your map be of the type Map<Character, Command>. Like this:

import java.util.*;

interface Command {
    void runCommand();
}

public class Test {
    
    public static void main(String[] args) throws Exception {
        Map<Character, Command> methodMap = new HashMap<Character, Command>();
        
        methodMap.put('h', new Command() {
            public void runCommand() { System.out.println("help"); };
        });
        
        methodMap.put('t', new Command() {
            public void runCommand() { System.out.println("teleport"); };
        });
        
        char cmd = 'h';
        methodMap.get(cmd).runCommand();  // prints "Help"
        
        cmd = 't';
        methodMap.get(cmd).runCommand();  // prints "teleport"
        
    }
}

Reflection "hack"

With that said, you can actually do what you're asking for (using reflection and the Method class.)

import java.lang.reflect.*;
import java.util.*;

public class Test {
    
    public static void main(String[] args) throws Exception {
        Map<Character, Method> methodMap = new HashMap<Character, Method>();
        
        methodMap.put('h', Test.class.getMethod("showHelp"));
        methodMap.put('t', Test.class.getMethod("teleport"));
        
        char cmd = 'h';
        methodMap.get(cmd).invoke(null);  // prints "Help"
        
        cmd = 't';
        methodMap.get(cmd).invoke(null);  // prints "teleport"
        
    }
    
    public static void showHelp() {
        System.out.println("Help");
    }
    
    public static void teleport() {
        System.out.println("teleport");
    }
}
2 of 3
7

Though you could store methods through reflection, the usual way to do it is to use anonymous objects that wrap the function, i.e.

  interface IFooBar {
    void callMe();
  }


 'h', new IFooBar(){ void callMe() { showHelp(); } }
 't', new IFooBar(){ void callMe() { teleport(); } }

 HashTable<IFooBar> myHashTable;
 ...
 myHashTable.get('h').callMe();
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › Map.html
Map (Java SE 21 & JDK 21)
January 20, 2026 - Replaces the entry for the specified key only if currently mapped to the specified value. ... Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
🌐
Stackify
stackify.com › an-introduction-to-java-map-what-it-is-and-how-it-works
An Introduction to Java Map: What It Is and How It Works - Stackify
November 26, 2024 - Map’s role is as a data structure focused on efficient mapping and retrieval rather than element storage alone. Java doesn’t allow you to create objects of interfaces; hence, you can’t create an object of the Map interface directly.
🌐
Scaler
scaler.com › home › topics › java stream map()
Java Stream map() - Scaler Topics
June 22, 2024 - The Java 8 Stream's map() method simply takes a stream of type X and returns another stream of type Y by applying the mapper function on the input stream elements and producing new stream elements of another type.