All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aima.core.util.SetOps Maven / Gradle / Ivy

Go to download

AIMA-Java Core Algorithms from the book Artificial Intelligence a Modern Approach 3rd Ed.

There is a newer version: 3.0.0
Show newest version
package aima.core.util;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * Note: This code is based on - Java Tutorial: The Set Interface 
* * Using LinkedHashSet, even though slightly slower than HashSet, in order to * ensure order is always respected (i.e. if called with TreeSet or * LinkedHashSet implementations). * * @author Ciaran O'Reilly * @author Ravi Mohan */ public class SetOps { /** * * @param * @param s1 * @param s2 * @return the union of s1 and s2. (The union of two sets is the set * containing all of the elements contained in either set.) */ public static Set union(Set s1, Set s2) { if (s1 == s2) { return s1; } Set union = new LinkedHashSet(s1); union.addAll(s2); return union; } /** * * @param * @param s1 * @param s2 * @return the intersection of s1 and s2. (The intersection of two sets is * the set containing only the elements common to both sets.) */ public static Set intersection(Set s1, Set s2) { if (s1 == s2) { return s1; } Set intersection = new LinkedHashSet(s1); intersection.retainAll(s2); return intersection; } /** * * @param * @param s1 * @param s2 * @return the (asymmetric) set difference of s1 and s2. (For example, the * set difference of s1 minus s2 is the set containing all of the * elements found in s1 but not in s2.) */ public static Set difference(Set s1, Set s2) { if (s1 == s2) { return new LinkedHashSet(); } Set difference = new LinkedHashSet(s1); difference.removeAll(s2); return difference; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy