user386537
user386537

Reputation: 461

how to compare a set of characters with a map which keys are characters?

If i have a set of characters which is called characters and contains the following Characters (doesn't have to be SortedSet)

'c''h''a''r''a''c''t''e''r'

and i have a map which has sets of charcters as its keys and strings as values for example

map<Set<Character>>,<String> aMap = new HashMap<Set<Character>>,<String>();

aMap.put('a''h''t', "hat");
aMap.put('o''g''d', "dog");
aMap.put('c''r''a''t''e', "react");

What javdoc method would i use to compare the characters since they are both in a Set, then to iterate through the keySet using a for loop to compare the characters to find only the keys that are made from charcters that are contained within the first. So in above example the second entry ('o''g''d', "dog") would be ommitted.

thanks

andy

Upvotes: 2

Views: 946

Answers (2)

stacker
stacker

Reputation: 68962

To get something comparable to your set call map.keySet()

public class SetTest {
    public static void main(String[] args) {

        Set<Character> set = new HashSet<Character>();
        HashMap<Character, String> map = new HashMap<Character, String>();
        for (char c : "Character".toCharArray()) {
            set.add(c);
            map.put(c, "some value");
        }
        System.out.println( set + " == " + map.keySet() + set.containsAll( map.keySet() ));
        set.remove('C');
        System.out.println( set + " == " + map.keySet() + set.containsAll( map.keySet() ));
    }
}


[e, t, c, r, a, C, h] == [e, t, c, r, a, C, h]true
[e, t, c, r, a, h] == [e, t, c, r, a, C, h]false

Upvotes: 1

foret
foret

Reputation: 728

just play with set.containsAll(...).
example: if sets have equal sizes and firstSet.containsAll(secondSet) is true, than 2 sets are identical.

Upvotes: 0

Related Questions