Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. @Override
  2. public Set<E> union(Set<E> that) {
  3. //The code is really fucking convoluted but
  4. //Iterate through both Sets based on which is greater (If there equal the compare doesn't matter
  5. //if they both have the same elemenet e, place it in a seperate array to be added at the end
  6. //YOU DONT ADD to the return Set if they are the same. Only unique objects are added
  7. //At the end, the final array is added with all the duplicate elements.
  8. LinkedSet rval = new LinkedSet<E>(head);
  9. LinkedSet doubleAdd = null;
  10. int matches = 0;
  11. LinkedNodeIterator<E> tempThis = (LinkedNodeIterator<E>) iterator();
  12. LinkedNodeIterator<E> tempThat = (LinkedNodeIterator<E>) that.iterator();
  13. if (this.size() > that.size()){
  14. while (tempThis.hasNext()) {
  15. E dataThis = tempThis.next();
  16. while (tempThat.hasNext()) {
  17. E dataThat = tempThat.next();
  18. if (dataThat == dataThis){
  19. if (matches == 0){
  20. doubleAdd = new LinkedSet<E>(dataThis);
  21. matches++;
  22. }
  23. else{
  24. doubleAdd = (LinkedSet) doubleAdd.adjoin(dataThis);
  25. }
  26.  
  27. }
  28. else{
  29. rval = (LinkedSet) rval.adjoin(dataThat);
  30. }
  31. }
  32. }
  33. tempThis = (LinkedNodeIterator<E>) iterator();
  34. tempThat = (LinkedNodeIterator<E>) that.iterator();
  35. while(tempThat.hasNext()){
  36. E dataThat = tempThat.next();
  37. while (tempThis.hasNext()){
  38. E dataThis = tempThis.next();
  39. if (dataThat == dataThis){
  40. }
  41. else{
  42. rval = (LinkedSet) rval.adjoin(dataThis);
  43. }
  44. }
  45. }
  46. }
  47. //If size difference
  48. else{
  49. tempThis = (LinkedNodeIterator<E>) iterator();
  50. tempThat = (LinkedNodeIterator<E>) that.iterator();
  51. while(tempThat.hasNext()){
  52. E dataThat = tempThat.next();
  53. while (tempThis.hasNext()){
  54. E dataThis = tempThis.next();
  55. if (dataThat == dataThis){
  56. if (matches == 0){
  57. doubleAdd = new LinkedSet<E>(dataThis);
  58. matches++;
  59. }
  60. else{
  61. doubleAdd = (LinkedSet) doubleAdd.adjoin(dataThat);
  62. }
  63. }
  64. else{
  65. rval = (LinkedSet) rval.adjoin(dataThis);
  66. }
  67. }
  68. }
  69. tempThis = (LinkedNodeIterator<E>) iterator();
  70. tempThat = (LinkedNodeIterator<E>) that.iterator();
  71. while(tempThis.hasNext()){
  72. E dataThis = tempThis.next();
  73. while (tempThat.hasNext()){
  74. E dataThat = tempThat.next();
  75. if (dataThat == dataThis){
  76. }
  77. else{
  78. rval = (LinkedSet) rval.adjoin(dataThat);
  79. }
  80. }
  81. }
  82.  
  83. }
  84. LinkedNodeIterator<E> tempDA = (LinkedNodeIterator<E>) doubleAdd.iterator();
  85. while(tempDA.hasNext()){
  86. rval = (LinkedSet) rval.adjoin(tempDA.next());
  87. }
  88. LinkedNodeIterator<E> rvalIt = (LinkedNodeIterator<E>) rval.iterator();
  89. System.out.println("The final rval is: ");
  90. while(rvalIt.hasNext()){
  91. System.out.print(" " + rvalIt.next());
  92. }
  93. return rval;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement