Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. set1.stream().filter(s -> set2.contains(s)).collect(Collectors.toSet());
  2.  
  3. Set<String> intersection = Sets.intersection(set1, set2);
  4. Set<String> difference = Sets.difference(set1, set2);
  5. Set<String> symmetricDifference = Sets.symmetricDifference(set1, set2);
  6. Set<String> union = Sets.union(set1, set2);
  7.  
  8. Collection<Foo> oldSet = ...;
  9. Collection<Foo> newSet = ...;
  10.  
  11. private Collection difference(Collection a, Collection b) {
  12. Collection result = a.clone();
  13. result.removeAll(b)
  14. return result;
  15. }
  16.  
  17. private Collection intersection(Collection a, Collection b) {
  18. Collection result = a.clone();
  19. result.retainAll(b)
  20. return result;
  21. }
  22.  
  23. public doWork() {
  24. // if foo is in(*) oldSet but not newSet, call doRemove(foo)
  25. Collection removed = difference(oldSet, newSet);
  26. if (!removed.isEmpty()) {
  27. loop removed {
  28. Foo foo = removedIter.next();
  29. doRemove(foo);
  30. }
  31. }
  32. //else if foo is not in oldSet but in newSet, call doAdd(foo)
  33. Collection added = difference(newSet, oldSet);
  34. if (!added.isEmpty()) {
  35. loop added {
  36. Foo foo = addedIter.next();
  37. doAdd(foo);
  38. }
  39. }
  40.  
  41. // else if foo is in both collections but modified, call doUpdate(oldFoo, newFoo)
  42. Collection matched = intersection(oldSet, newSet);
  43. Comparator comp = new Comparator() {
  44. int compare(Object o1, Object o2) {
  45. Foo f1, f2;
  46. if (o1 instanceof Foo) f1 = (Foo)o1;
  47. if (o2 instanceof Foo) f2 = (Foo)o2;
  48. return f1.activated == f2.activated ? f1.startdate.compareTo(f2.startdate) == 0 ? ... : f1.startdate.compareTo(f2.startdate) : f1.activated ? 1 : 0;
  49. }
  50.  
  51. boolean equals(Object o) {
  52. // equal to this Comparator..not used
  53. }
  54. }
  55. loop matched {
  56. Foo foo = matchedIter.next();
  57. Foo oldFoo = oldSet.get(foo);
  58. Foo newFoo = newSet.get(foo);
  59. if (comp.compareTo(oldFoo, newFoo ) != 0) {
  60. doUpdate(oldFoo, newFoo);
  61. } else {
  62. //else if !foo.activated && foo.startDate >= now, call doStart(foo)
  63. if (!foo.activated && foo.startDate >= now) doStart(foo);
  64.  
  65. // else if foo.activated && foo.endDate <= now, call doEnd(foo)
  66. if (foo.activated && foo.endDate <= now) doEnd(foo);
  67. }
  68. }
  69. }
  70.  
  71. /* Main method */
  72. private void execute(Collection<Foo> oldSet, Collection<Foo> newSet) {
  73. List<Foo> oldList = asSortedList(oldSet);
  74. List<Foo> newList = asSortedList(newSet);
  75.  
  76. int oldIndex = 0;
  77. int newIndex = 0;
  78. // Iterate over both collections but not always in the same pace
  79. while( oldIndex < oldList.size()
  80. && newIndex < newIndex.size()) {
  81. Foo oldObject = oldList.get(oldIndex);
  82. Foo newObject = newList.get(newIndex);
  83.  
  84. // Your logic here
  85. if(oldObject.getId() < newObject.getId()) {
  86. doRemove(oldObject);
  87. oldIndex++;
  88. } else if( oldObject.getId() > newObject.getId() ) {
  89. doAdd(newObject);
  90. newIndex++;
  91. } else if( oldObject.getId() == newObject.getId()
  92. && isModified(oldObject, newObject) ) {
  93. doUpdate(oldObject, newObject);
  94. oldIndex++;
  95. newIndex++;
  96. } else {
  97. ...
  98. }
  99. }// while
  100.  
  101. // Check if there are any objects left in *oldList* or *newList*
  102.  
  103. for(; oldIndex < oldList.size(); oldIndex++ ) {
  104. doRemove( oldList.get(oldIndex) );
  105. }// for( oldIndex )
  106.  
  107. for(; newIndex < newList.size(); newIndex++ ) {
  108. doAdd( newList.get(newIndex) );
  109. }// for( newIndex )
  110. }// execute( oldSet, newSet )
  111.  
  112. /** Create sorted list from collection
  113. If you actually perform any actions on input collections than you should
  114. always return new instance of list to keep algorithm simple.
  115. */
  116. private List<Foo> asSortedList(Collection<Foo> data) {
  117. List<Foo> resultList;
  118. if(data instanceof List) {
  119. resultList = (List<Foo>)data;
  120. } else {
  121. resultList = new ArrayList<Foo>(data);
  122. }
  123. Collections.sort(resultList)
  124. return resultList;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement