Jump to content

Iterator and collections

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
I have a collection of buckets and lets say one of the buckets has a collection of buckets
as a subset or subcollection.
Im using a treeset and i already verified that all the buckets were added to
the set in the correct place including the buckets that were in another bucket.

My question is can the iterator for the set of main buckets interate over the set of main buckets and when it reaches a bucket, let say bucket C, that contains sub buckets, iterate through those buckets in C, then get back to the main list
and continue?
I have a simple iterator that iterates over such a list but im not sure if it is possible to do the above.
Here is the method:

public String printBuckets(){ //a lot of work to be done here

      Iterator iptr = treeset.iterator();

      String str = new String("");

      while(iptr.hasNext())

         str = str + iptr.next().toString() + "\n";

      return str;

   }

Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#2
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
This is a classic call for recursion. Something like this:


public String printBuckets(Iterator iptr){

   String str;

   while(iptr.hasNext()){

      Object obj = iptr.next();


      if(obj instanceof String){

         //do your stuff for a string

      }

      if{obj instanceof Iterator){

         printBuckets((Iterator)obj);

      }

   }

}


Why do you want to use a treeset though? What about an ArrayList?

Hope that helps.

#3
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
That isn't working because, i created a new bucket, then i searched for the bucket that i wanted to put it into, lets say B then i call a method to add the new bucket to B. Something like
B.addbucket(Bb);
However i made some progress but my recursive method only goes one level deep and i added all in a similar manner.
static str;


public String printBucket(){ 

      Iterator iptr = treeset.iterator();      

      while( iptr.hasNext() ){ 

         Bucket subr = (Bucket)iptr.next();         

         if( subr instanceof Bucket ){

            str += subr.getID() + "\n";

            subr.printBucket(); 

         }

          

      }

      return str;

   }

Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#4
navghost

navghost

    Newbie

  • Members
  • PipPip
  • 17 posts
If I understand You, then my suggestion is try to create two loops, for example:

private String print(String text) {
	if(text == null) {
		text = new String("");
	}
	
	if(list != null && list.size() > 0) {
		Iterator<Bucket> iterList = list.iterator();
		
		while(iterList.hasNext()) {
			text += subprint(iterList.next(), text);
		}
		return text;
	} else {
		return text;
	}
}

private String subprint(Bucket bucket, String text) {
	if(bucket != null) {
		Iterator<Bucket> iter;
		
		text += bucket.getName() + "\n";
		iter = bucket.getBucket().iterator();
		
		while(iter.hasNext()) {
			text = subprint(iter.next(), text);
		}
		
		return text;
	} else {
		return text;
	}
}

/* where: 
  1. List is a variable of java.util.List<Bucket>
  2. Simple Bucket class looks like this:

	class Bucket {
		private String name;
		
		private List<Bucket> buckets;
		
		
		public Bucket(String name) {
			super();
			
			this.name = name;
			this.buckets = new ArrayList<Bucket>();
		}
		
		public String getName() {
			return name;
		}
		
		public void setName(String name) {
			this.name = name;
		}
		
		public List<Bucket> getBucket() {
			return buckets;
		}
		
		public void addBucket(Bucket bucket) {
			this.buckets.add(bucket);
		}
	}
*/

Best regards
navghost
"Work should be challenging and the challenge should be fun. Great just isn't good enough!"