Skip to content

Instantly share code, notes, and snippets.

@coderRohan123
Created January 2, 2024 11:29
Show Gist options
  • Save coderRohan123/debd8ac97eb05da3a29dabd36b344bb5 to your computer and use it in GitHub Desktop.
Save coderRohan123/debd8ac97eb05da3a29dabd36b344bb5 to your computer and use it in GitHub Desktop.
The code snippet creates a map with string keys and integer values, adds some key-value pairs to it, and then reverses the map by swapping the keys and values. The reversed map is then printed.

Reverse Map using Generics

Preview:
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> myMap = new HashMap<>();
        myMap.put("apple", 1);
        myMap.put("banana", 2);
        myMap.put("orange", 3);

        Map<Integer, String> reversedMap = reverseMap(myMap);
        System.out.println(reversedMap);
    }

    public static <K, V> Map<V, K> reverseMap(Map<K, V> originalMap) {
        Map<V, K> reversedMap = new HashMap<>();
        for (Map.Entry<K, V> entry : originalMap.entrySet()) {
            reversedMap.put(entry.getValue(), entry.getKey());
        }
        return reversedMap;
    }
}
Associated Context
Type Code Snippet ( .java )
Associated Tags Java HashMap Reverse Map Entry Set Key-Value Pairs Java SDK Data Structures Main Method Print Output Framework: Java.util Map Key-Value Pair Generics Data Structure SDK Use Case
πŸ“ Custom Description
πŸ’‘ Smart Description The code snippet reverses a map by creating two maps, "apple", "banana", and "orange". It then uses the reverseMap method to create an object with values from each entry. Finally, it returns
The code snippet creates a map with string keys and integer values, adds some key-value pairs to it, and then reverses the map by swapping the keys and values. The reversed map is then printed.
πŸ”Ž Suggested Searches Java code to reverse a map
How to reverse an array in Java using HashMap
Code snippet for reversing elements of the original maps in Java
Java program to reverse two Map objects
Reverse mapping values from another Hashtable in Java
How to reverse a map in Java
Java code to swap keys and values in a map
HashMap reverse key value pairs Java
Java code to create a reversed map from an existing map
Related Links https://www.udemy.com/course/data-structures-and-algorithms-bootcamp-in-python/learn/lecture/37708576#notes
https://www.udemy.com/course/data-structures-and-algorithms-bootcamp-in-python/learn/lecture/37691908#notes
https://www.udemy.com/course/data-structures-and-algorithms-bootcamp-in-python/learn/quiz/5880944#notes
https://www.javatpoint.com/java-tutorial
https://www.javatpoint.com/javafx-tutorial
https://www.javatpoint.com/java-hashmap
https://www.codejava.net/java-core/collections/java-list-collection-tutorial-and-examples
https://mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/
https://www.w3schools.com/java/java_hashmap.asp
https://www.baeldung.com/java-hashmap
https://www.tutorialspoint.com/java/util/hashmap_put.htm
https://www.programiz.com/java-programming/library/hashmap
https://www.digitalocean.com/community/tutorials/java-hashmap
https://www.javatpoint.com/java-map
https://www.softwaretestinghelp.com/hashmap-in-java/
Related People Rohan Mallick
Sensitive Information No Sensitive Information Detected
Shareable Link https://ca772742-b4e2-4612-af82-29a364c1f0bb.pieces.cloud/?p=6be3488a7c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment