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 - Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null. This method may be of use when combining multiple mapped values for a key. For example, to either create or append a String msg to a value mapping:
🌐
Java67
java67.com › 2015 › 01 › java-8-map-function-examples.html
Java 8 Stream map() function Example with Explanation | Java67
In this Java 8 tutorial, we have used the map function for two examples, first to convert each element of List to upper case, and second to square each integer in the List. By the way, this is just the tip of the iceberg of what you can do with ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › map-interface-in-java
Map Interface in Java - GeeksforGeeks
... import java.util.HashMap; import ... // Create a Map using HashMap Map<String, Integer> m = new HashMap<>(); // Adding key-value pairs to the map m.put("Geek1", 1); m.put("Geek2", 2); m.put("Geek3", 3); System.out.println("Map elements: " + m); } }...
Published   January 7, 2026
🌐
W3Schools
w3schools.com › java › java_map.asp
Java Map
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... 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 - Example 1 : Stream map() function with operation of number * 3 on each element of stream. Java · // Java code for Stream map(Function mapper) // to get a stream by applying the // given function to this stream.
🌐
CodingBat
codingbat.com › doc › java-functional-mapping.html
Java Functional Mapping
Mapping uses a little function that takes in one item and computes the new value for that item. For the doubling problem, we want a function that takes in a single Integer and returns double its value. The code snippet n -> n * 2 is that function in "lambda" syntax: take in a number and return ...
🌐
Scaler
scaler.com › home › topics › java stream map()
Java Stream map() - Scaler Topics
June 22, 2024 - Now let us another example of the ... to convert all the uppercase letters to lowercase letters. Below is the code example for the same. ... In this example, we have taken all the stream elements one by one and converted them to the lower case using the mapper function of the map() method...
Find elsewhere
🌐
DZone
dzone.com › coding › languages › java 8 map, filter, and collect examples
Java 8 Examples: Map, Filter and Collect
June 21, 2018 - That's why we called the map() function first. Once we have the Stream of Integer, we can apply maths to find the even numbers. We passed that condition to filter method. If we needed to filter on String, e.g. select all string which has length > 2, then we would have called filter before map. That's all about how to use map and filter in Java 8. We have seen an interesting example of how we can use the map to transform an object to another and how to use filter to select an object based upon condition.
🌐
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 - Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null. This method may be of use when combining multiple mapped values for a key. For example, to either create or append a String msg to a value mapping:
🌐
Javatpoint
javatpoint.com › java-map
Java Map Interface - javatpoint
class class is the specialized Map implementation for enum keys. It inherits Enum and AbstractMap classes. EnumMap class hierarchy The hierarchy of EnumMap class is given in the figure given below. EnumMap class declaration Let's see the declaration for java.util.EnumMap class.
🌐
Medium
medium.com › @AlexanderObregon › javas-stream-map-method-explained-df0d0d461d39
Java’s Stream.map() Method Explained | Medium
August 27, 2024 - One of the most common use cases for the map() method is transforming primitive data types. For example, consider a scenario where you have a list of numeric strings that need to be converted to integers.
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java stream map()
Java Stream map() with Examples - HowToDoInJava
August 26, 2023 - Java 8 Stream.map() converts Stream to Stream. For each object of type X, 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();
🌐
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 - As seen in the Map hierarchy, LinkedHashMap extends HashMap but maintains the order of insertion. This makes the function highly useful when the order of elements matters, such as in a caching system or tracking the sequence of user actions. Similar to HashMap, LinkedHashMap allows null keys and values. Let’s take a look at an example. Here, LinkedHashMap keeps track of the order of recent actions. import java.util.LinkedHashMap; import java.util.Map; public class LinkedHashMapExample { public static void main(String[] args) { Map<Integer, String> recentActions = new LinkedHashMap<>(); recentActions.put(1, "Login"); recentActions.put(2, "Viewed Profile"); recentActions.put(3, "Added to Cart"); System.out.println("Recent actions: " + recentActions); } }
🌐
Programiz
programiz.com › java-programming › map
Java Map Interface
... import java.util.Map; import ... { // Creating a map using the HashMap Map<String, Integer> numbers = new HashMap<>(); // Insert elements to the map numbers.put("One", 1); numbers.put("Two", 2); System.out.println("Map: " + numbers); // Access keys of the map System.out.p...
🌐
Medium
medium.com › javarevisited › how-to-use-streams-map-filter-and-collect-methods-in-java-1e13609a318b
How to use map, filter, and collect methods in Java Stream? Example Tutorial | by javinpaul | Javarevisited | Medium
January 25, 2024 - In the past, I have shared the best Java functional programming courses as well as some Java books on Lambda and Stream, and today, I am going to teach you how you can use map, filter, and collect() methods to create your stream pipeline to transform your data from one form to other.
🌐
LinkedIn
linkedin.com › pulse › how-safely-use-map-operation-java-8-streams-nikhil-gargatte
How to Safely Use the map() Operation in Java 8 Streams
May 11, 2023 - In this example, we're using the map() operation to remove all even numbers from the List of integers. However, when we try to remove elements from the List while iterating over it, we get a ConcurrentModificationException.
🌐
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
If our mapping function returns one of the related value types, we could use the corresponding mapTo...(mapper) / flatMapTo...(mapper) to create a value-type-based Stream:
🌐
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