Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public Set<E> union(Set<E> that) {
- //The code is really fucking convoluted but
- //Iterate through both Sets based on which is greater (If there equal the compare doesn't matter
- //if they both have the same elemenet e, place it in a seperate array to be added at the end
- //YOU DONT ADD to the return Set if they are the same. Only unique objects are added
- //At the end, the final array is added with all the duplicate elements.
- LinkedSet rval = new LinkedSet<E>(head);
- LinkedSet doubleAdd = null;
- int matches = 0;
- LinkedNodeIterator<E> tempThis = (LinkedNodeIterator<E>) iterator();
- LinkedNodeIterator<E> tempThat = (LinkedNodeIterator<E>) that.iterator();
- if (this.size() > that.size()){
- while (tempThis.hasNext()) {
- E dataThis = tempThis.next();
- while (tempThat.hasNext()) {
- E dataThat = tempThat.next();
- if (dataThat == dataThis){
- if (matches == 0){
- doubleAdd = new LinkedSet<E>(dataThis);
- matches++;
- }
- else{
- doubleAdd = (LinkedSet) doubleAdd.adjoin(dataThis);
- }
- }
- else{
- rval = (LinkedSet) rval.adjoin(dataThat);
- }
- }
- }
- tempThis = (LinkedNodeIterator<E>) iterator();
- tempThat = (LinkedNodeIterator<E>) that.iterator();
- while(tempThat.hasNext()){
- E dataThat = tempThat.next();
- while (tempThis.hasNext()){
- E dataThis = tempThis.next();
- if (dataThat == dataThis){
- }
- else{
- rval = (LinkedSet) rval.adjoin(dataThis);
- }
- }
- }
- }
- //If size difference
- else{
- tempThis = (LinkedNodeIterator<E>) iterator();
- tempThat = (LinkedNodeIterator<E>) that.iterator();
- while(tempThat.hasNext()){
- E dataThat = tempThat.next();
- while (tempThis.hasNext()){
- E dataThis = tempThis.next();
- if (dataThat == dataThis){
- if (matches == 0){
- doubleAdd = new LinkedSet<E>(dataThis);
- matches++;
- }
- else{
- doubleAdd = (LinkedSet) doubleAdd.adjoin(dataThat);
- }
- }
- else{
- rval = (LinkedSet) rval.adjoin(dataThis);
- }
- }
- }
- tempThis = (LinkedNodeIterator<E>) iterator();
- tempThat = (LinkedNodeIterator<E>) that.iterator();
- while(tempThis.hasNext()){
- E dataThis = tempThis.next();
- while (tempThat.hasNext()){
- E dataThat = tempThat.next();
- if (dataThat == dataThis){
- }
- else{
- rval = (LinkedSet) rval.adjoin(dataThat);
- }
- }
- }
- }
- LinkedNodeIterator<E> tempDA = (LinkedNodeIterator<E>) doubleAdd.iterator();
- while(tempDA.hasNext()){
- rval = (LinkedSet) rval.adjoin(tempDA.next());
- }
- LinkedNodeIterator<E> rvalIt = (LinkedNodeIterator<E>) rval.iterator();
- System.out.println("The final rval is: ");
- while(rvalIt.hasNext()){
- System.out.print(" " + rvalIt.next());
- }
- return rval;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement