class GFG {
// Driver's code
public static void main(String[] args)
{
// Creating first set named set1
Set<String>
set1 = Sets
.newHashSet("H", "E", "L", "L", "O", "G");
// Creating second set named set2
Set<String>
set2 = Sets
.newHashSet("L", "I", "K", "E", "G");
// Using Guava's Sets.difference() method
Set<String>
diff = Sets.difference(set1, set2);
// Displaying the unmodifiable view of
// the difference of two sets.
System.out.println("Set 1: "
+ set1);
System.out.println("Set 2: "
+ set2);
System.out.println("Difference between "
+ "Set 1 and Set 2: "
+ diff);
}
}
public static void main(String[] args) {
// Given sets
Set<Character> ab = ImmutableSet.of('a', 'b');
Set<Character> bc = ImmutableSet.of('b', 'c');
// Difference of two sets
Sets.difference(ab, bc); // -> [a]
}
public class SetsDifferenceTest {
public static void main(String[] args) {
ImmutableSet<String> stringSet = ImmutableSet.of("ONE", "TWO", "THREE");
System.out.println(stringSet);
ImmutableSet<String> stringSet2 = ImmutableSet.of("TWO", "THREE", "FOUR");
System.out.println(stringSet2);
//Returns a set containing all elements that are contained by set1 and
//not contained by set2
Set<String> resultSet = Sets.difference(stringSet, stringSet2);
System.out.println(resultSet);
}
}
public static void main(String[] args) {
Set<String> s1 = Sets.newHashSet("1","2","3");
Set<String> s2 = Sets.newHashSet("2","3","4");
Sets.difference(s1,s2);
}
public static void main(String[] args) {
HashSet setA = newHashSet(1, 2, 3, 4, 5);
HashSet setB = newHashSet(4, 5, 6, 7, 8);
SetView union = Sets.union(setA, setB);
System.out.println("union:");
for (Integer integer : union)
System.out.println(integer);
SetView difference = Sets.difference(setA, setB);
System.out.println("difference:");
for (Integer integer : difference)
System.out.println(integer);
SetView intersection = Sets.intersection(setA, setB);
System.out.println("intersection:");
for (Integer integer : intersection)
System.out.println(integer);
}
static void formatConfiguration(StringBuilder sb, AttributeContainer fromConfigurationAttributes, AttributesSchema consumerSchema, List<ConfigurationMetadata> matches, Set<String> requestedAttributes, int maxConfLength, final String conf) {
Optional<ConfigurationMetadata> match = Iterables.tryFind(matches, new Predicate<ConfigurationMetadata>() {
@Override
public boolean apply(ConfigurationMetadata input) {
return conf.equals(input.getName());
}
});
if (match.isPresent()) {
AttributeContainer producerAttributes = match.get().getAttributes();
Set<Attribute<?>> targetAttributes = producerAttributes.keySet();
Set<String> targetAttributeNames = Sets.newTreeSet(Iterables.transform(targetAttributes, ATTRIBUTE_NAME));
Set<Attribute<?>> allAttributes = Sets.union(fromConfigurationAttributes.keySet(), producerAttributes.keySet());
Set<String> commonAttributes = Sets.intersection(requestedAttributes, targetAttributeNames);
Set<String> consumerOnlyAttributes = Sets.difference(requestedAttributes, targetAttributeNames);
sb.append(" ").append("- Configuration '").append(StringUtils.rightPad(conf + "'", maxConfLength + 1)).append(" :");
List<Attribute<?>> sortedAttributes = Ordering.usingToString().sortedCopy(allAttributes);
List<String> values = new ArrayList<String>(sortedAttributes.size());
formatAttributes(sb, fromConfigurationAttributes, consumerSchema, producerAttributes, commonAttributes, consumerOnlyAttributes, sortedAttributes, values);
}
}
private Set<Group> getUserGroups(User user) {
//get user's new group
Set<Group> groupsForUser = findGroupsForUser(user.getPermission(), user.getRoles()).stream()
.map(group -> {
group.getUsers().add(user);
groupDAO.save(group);
return group;
}).collect(Collectors.toSet());
//remove user from old group
Set<Group> oldGroups = Sets.difference(user.getGroups(), groupsForUser);
oldGroups.stream()
.filter(group -> group.getGroupType().equals(GroupType.PUBLIC))
.map(group -> {
group.getUsers().remove(user);
groupDAO.save(group);
return group;
}).collect(Collectors.toSet());
return groupsForUser;
}
public void saveAssignedRoles(String userId, Set<Role> newRoles) {
Set<Role> oldRoles = getAssignedRoles(userId);
Sets.SetView<Role> removedRoles = Sets.difference(oldRoles, newRoles);
Sets.SetView<Role> addedRoles = Sets.difference(newRoles, oldRoles);
removedRoles.forEach(role -> {
store.deleteRole(userId, role);
logger.logEvent(new RoleDelEvent(Instant.now(), userId, role.getId(), role.getName()));
});
addedRoles.forEach(role -> {
store.addRole(userId, role);
logger.logEvent(new RoleAddEvent(Instant.now(), userId, role.getId(), role.getName()));
});
}
public void clusterChanged(ClusterChangedEvent event) {
if (!event.metaDataChanged()) {
return;
}
Set<String> newCurrentSchemas = getNewCurrentSchemas(event.state().metaData());
synchronized (schemas) {
Sets.SetView<String> nonBuiltInSchemas = Sets.difference(schemas.keySet(), builtInSchemas.keySet());
Set<String> deleted = Sets.difference(nonBuiltInSchemas, newCurrentSchemas).immutableCopy();
Set<String> added = Sets.difference(newCurrentSchemas, schemas.keySet()).immutableCopy();
for (String deletedSchema : deleted) {
try {
schemas.remove(deletedSchema).close();
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
for (String addedSchema : added) {
schemas.put(addedSchema, getCustomSchemaInfo(addedSchema));
}
}
}
private void updateTopics(Set<String> topicNames) {
Set<String> currentTopics = topicsByName.keySet();
Set<String> topicsToAdd = Sets.difference(topicNames, currentTopics);
Set<String> topicsToRemove = Sets.difference(currentTopics, topicNames);
topicsToAdd.stream()
.filter(topicName -> !topicName.equals("__consumer_offsets"))
.forEach(topicName -> {
try {
String partitionsPath = "/brokers/topics/" + topicName + "/partitions";
zkClient.waitUntilExists(partitionsPath, TimeUnit.SECONDS, 5);
topicsByName.put(topicName, new KafkaTopic(topicName, zkClient.getChildren(partitionsPath).size()));
log.info("Topic added: [{}]", topicName);
} catch (Exception e) {
log.error("Exception fetching info for topic [{}]: {}", topicName, e.getMessage(), e);
}
});
topicsToRemove.forEach(topicName -> {
topicsByName.remove(topicName);
log.info("Topic deleted: [{}]", topicName);
});
}