Harsha Vardhan
Harsha Vardhan

Reputation: 49

How to convert a map to set in java and add all elements of map to set?

I am wanting to convert a HashMap to a Set.I am trying to find common elements between two maps by first putting that to a set and use retainAll. How to convert a Map to a Set.

Upvotes: 1

Views: 10415

Answers (3)

Robert White
Robert White

Reputation: 711

To find the intersection of two maps, using java.util.Set.retainAll() is reasonable, but the set one ends up with is a Set<Map.Entry>. If one wants to use these entries again, one must remap them (as shown in the example below).

In the example below, two lines do all the work. There are three parts to the example:

  1. Set up of test code
  2. Two lines of work, to perform the intersection, plus one for loop to remap the common entries.
  3. Show the results.

    public static void main(String[] args) {
    
        // **** Section 1: Setup ****
        // Create 3 maps; Two with duplicate values 
        // and a third to hold the ultimate results
        Map<String, String> m2 = new HashMap<String, String>();
        Map<String, String> m1 = new HashMap<String, String>();
        Map<String, String> common = new HashMap<String, String>();
        // Populate test map 1
        m1.put("1", "One");
        m1.put("2", "Two");
        m1.put("2a", "Two");
    
        // Create map 2 containing some of the same values in map 1
        m2.put("I", "One");
        m2.put("1", "One");
        m2.put("II", "Two");
        m2.put("2", "Two");
    
        // **** Section 2: Perform the intersection ****
        // create a set to handle the intersection
        Set<Map.Entry<String,String>> dups = m1.entrySet();
        dups.retainAll(m2.entrySet());
    
        // Remap the results that were common to both maps
        for (Map.Entry<String, String> entry: dups) {
            common.put(entry.getKey(), entry.getValue());
        }
    
        // **** Section 3: Show the results ****
        // show the resulting map of values found in both maps
        int ii = 0;
        for (Map.Entry<String, String> entry: dups) {
            System.out.println("Common member " + ++ii + "= " + entry.getKey() + ":" + entry.getValue());
        }
        // show the original maps
        showMap(1, m1);
        showMap(2, m2);
    }
    
    static private void showMap(int mapNumber, Map<String,String> m) {
        int ii = 0;
        for (Map.Entry<String, String> entry: m.entrySet()) {
            System.out.println("Map " + mapNumber 
                    + " member " + ++ii 
                    + ": key = " + entry.getKey() 
                    + ": value = " + entry.getValue());
        }
    }
    

Upvotes: 0

CptBartender
CptBartender

Reputation: 1220

Depending on what you expect to extract, you can take a look at one of these three methods (Java 7):

  1. Map.entrySet() - if you want to compare on both key and value
  2. Map.keySet() - if key is enough for you
  3. Map.values() - if you're only interested in values; note, that this returns a Collection, not a Set, but that should be easier to convert.

Upvotes: 3

Lorenzo Murrocu
Lorenzo Murrocu

Reputation: 688

Maybe what you need is just https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#entrySet-- or https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#keySet--

As you can see, calling map.values() or map.keySet() returns a set containing all values or keys.

Upvotes: 0

Related Questions