Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- set1.stream().filter(s -> set2.contains(s)).collect(Collectors.toSet());
- Set<String> intersection = Sets.intersection(set1, set2);
- Set<String> difference = Sets.difference(set1, set2);
- Set<String> symmetricDifference = Sets.symmetricDifference(set1, set2);
- Set<String> union = Sets.union(set1, set2);
- Collection<Foo> oldSet = ...;
- Collection<Foo> newSet = ...;
- private Collection difference(Collection a, Collection b) {
- Collection result = a.clone();
- result.removeAll(b)
- return result;
- }
- private Collection intersection(Collection a, Collection b) {
- Collection result = a.clone();
- result.retainAll(b)
- return result;
- }
- public doWork() {
- // if foo is in(*) oldSet but not newSet, call doRemove(foo)
- Collection removed = difference(oldSet, newSet);
- if (!removed.isEmpty()) {
- loop removed {
- Foo foo = removedIter.next();
- doRemove(foo);
- }
- }
- //else if foo is not in oldSet but in newSet, call doAdd(foo)
- Collection added = difference(newSet, oldSet);
- if (!added.isEmpty()) {
- loop added {
- Foo foo = addedIter.next();
- doAdd(foo);
- }
- }
- // else if foo is in both collections but modified, call doUpdate(oldFoo, newFoo)
- Collection matched = intersection(oldSet, newSet);
- Comparator comp = new Comparator() {
- int compare(Object o1, Object o2) {
- Foo f1, f2;
- if (o1 instanceof Foo) f1 = (Foo)o1;
- if (o2 instanceof Foo) f2 = (Foo)o2;
- return f1.activated == f2.activated ? f1.startdate.compareTo(f2.startdate) == 0 ? ... : f1.startdate.compareTo(f2.startdate) : f1.activated ? 1 : 0;
- }
- boolean equals(Object o) {
- // equal to this Comparator..not used
- }
- }
- loop matched {
- Foo foo = matchedIter.next();
- Foo oldFoo = oldSet.get(foo);
- Foo newFoo = newSet.get(foo);
- if (comp.compareTo(oldFoo, newFoo ) != 0) {
- doUpdate(oldFoo, newFoo);
- } else {
- //else if !foo.activated && foo.startDate >= now, call doStart(foo)
- if (!foo.activated && foo.startDate >= now) doStart(foo);
- // else if foo.activated && foo.endDate <= now, call doEnd(foo)
- if (foo.activated && foo.endDate <= now) doEnd(foo);
- }
- }
- }
- /* Main method */
- private void execute(Collection<Foo> oldSet, Collection<Foo> newSet) {
- List<Foo> oldList = asSortedList(oldSet);
- List<Foo> newList = asSortedList(newSet);
- int oldIndex = 0;
- int newIndex = 0;
- // Iterate over both collections but not always in the same pace
- while( oldIndex < oldList.size()
- && newIndex < newIndex.size()) {
- Foo oldObject = oldList.get(oldIndex);
- Foo newObject = newList.get(newIndex);
- // Your logic here
- if(oldObject.getId() < newObject.getId()) {
- doRemove(oldObject);
- oldIndex++;
- } else if( oldObject.getId() > newObject.getId() ) {
- doAdd(newObject);
- newIndex++;
- } else if( oldObject.getId() == newObject.getId()
- && isModified(oldObject, newObject) ) {
- doUpdate(oldObject, newObject);
- oldIndex++;
- newIndex++;
- } else {
- ...
- }
- }// while
- // Check if there are any objects left in *oldList* or *newList*
- for(; oldIndex < oldList.size(); oldIndex++ ) {
- doRemove( oldList.get(oldIndex) );
- }// for( oldIndex )
- for(; newIndex < newList.size(); newIndex++ ) {
- doAdd( newList.get(newIndex) );
- }// for( newIndex )
- }// execute( oldSet, newSet )
- /** Create sorted list from collection
- If you actually perform any actions on input collections than you should
- always return new instance of list to keep algorithm simple.
- */
- private List<Foo> asSortedList(Collection<Foo> data) {
- List<Foo> resultList;
- if(data instanceof List) {
- resultList = (List<Foo>)data;
- } else {
- resultList = new ArrayList<Foo>(data);
- }
- Collections.sort(resultList)
- return resultList;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement