ozanonurtek
ozanonurtek

Reputation: 306

Difference of Sets

Hello everyone my question is a bit easy

public static Set<String> edits(String word) {
    Set<String> edits = new HashSet<String>();
    List<SplitWord> splits = SplitWord.allSplits(word);
    for (SplitWord split: splits) {
        String a = split.prefix;
        String b = split.suffix;
        int lb = b.length();
        if (lb > 0) {
            edits.add(a + b.substring(1)); // delete
            for (int i = 0; i < ALPHABET.length(); ++i)
                edits.add(a + ALPHABET.charAt(i) + b.substring(1)); // replace
        }
        if (lb > 1)
            edits.add(a + b.charAt(1) + b.charAt(0) + b.substring(2)); // transpose
        for (int i = 0; i < ALPHABET.length(); ++i)
            edits.add(a + ALPHABET.charAt(i) + b); // insert
    }
    return edits;
}
public static Set<String> edits2(String word){//Double Edits according to norvig's spell corrector. Recursive way.
    Set<String> firstSet = new HashSet<String>();
    Set<String> secondSet= new HashSet<String>();

    firstSet.addAll(edits(word));
    for(String w: editsn){
            secondSet.addAll(edits(w));
            if(secondSet.contains(edits1(word))){
                secondSet.remove(w);
            }
    }

    return secondSet;
}

Here are my two methods, my "edits" method check spelling error for just one spell error. For example, if you write (atson) it says (watson). I use "edits" function in recursive way to check double spell errors(tson to watson for example). It works but my secondSet contains firstSet so it prints also one spelling errors too. I try to remove elements, but it doesn't work. So how can print the difference of two set? (Mathematically A-B)

Upvotes: 0

Views: 91

Answers (3)

awsome
awsome

Reputation: 2153

You can use this method of set for the difference of two sets

removeAll(java.util.Collection)

Important Note : This mutates the set that you call removeAll on

Upvotes: 2

e.doroskevic
e.doroskevic

Reputation: 2167

Example:

Let's assume you have a List containing a some words;

// List declaration;
List<String> words = new ArrayList<String>();

// Populate list;
words.add("one");
words.add("two");
words.add("three");

And you have two sets;

// Set declaration; 
Set<String> setA = new HashSet<String>();
Set<String> setB = new HashSet<String>();

Let's populate both our sets with words from list we defined previously and add an exclusive word to one of the sets;

setA.addAll(words);
setB.addAll(words);
setA.add("four");

Now we want to remove those elements which are in both sets

setA.removeAll(setB);

Output the result:

System.out.println(setA);

Upvotes: 1

spa
spa

Reputation: 5185

It is also very easy to do with Guava, which most people have in their class path anyway.

final HashSet<String> set1 = Sets.newHashSet("A", "B");
final HashSet<String> set2 = Sets.newHashSet("B", "C");
final Sets.SetView<String> difference = Sets.difference(set1, set2); // contains A only

Upvotes: 1

Related Questions