4 Replies - 2340 Views - Last Post: 07 September 2009 - 04:02 AM Rate Topic: -----

#1 Laythe   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 76
  • Joined: 09-June 09

How to Create Iterator object from the HashSet object?

Posted 05 September 2009 - 03:54 PM

How To Create Iterator object from the HashSet object and iterate them to get displayed?
It seems to me that i cant use listIterator() Method with HashSet Object. I have no idea why.

This is My code:

import java.util.HashSet;
import java.util.ListIterator;

public class MyHashSet
{
	public MyHashSet()
	{
	}

	public static void main(String Laythe[])
	{
		HashSet HashSetX = new HashSet(5);

		HashSetX.add(new Integer(1));
		HashSetX.add(new Integer(2));
		HashSetX.add(new Integer(3));

		System.out.println(HashSetX);

		ListIterator HashSetY = HashSetX.listIterator();

		while (HashSetY.hasNext())
		{
			System.out.println("From ListEterator, HashSetY.next() = "
					+HashSetY.next());
		}
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: How to Create Iterator object from the HashSet object?

#2 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: How to Create Iterator object from the HashSet object?

Posted 05 September 2009 - 10:34 PM

Java api should be the first place you look for any of this stuff. http://java.sun.com/...#iterator%28%29
Was This Post Helpful? 0
  • +
  • -

#3 AntonWebsters   User is offline

  • D.I.C Regular
  • member icon

Reputation: 89
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: How to Create Iterator object from the HashSet object?

Posted 05 September 2009 - 11:57 PM

Does this work for you? You gotta import java.util.Iterator though.
Iterator HashSetY = HashSetX.iterator();

		while (HashSetY.hasNext())
		{
			System.out.println("From ListEterator, HashSetY.next() = "
					+HashSetY.next());
		}


This post has been edited by AntonWebsters: 05 September 2009 - 11:58 PM

Was This Post Helpful? 1
  • +
  • -

#4 Laythe   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 76
  • Joined: 09-June 09

Re: How to Create Iterator object from the HashSet object?

Posted 06 September 2009 - 01:54 PM

View PostAntonWebsters, on 5 Sep, 2009 - 10:57 PM, said:

Does this work for you? You gotta import java.util.Iterator though.
Iterator HashSetY = HashSetX.iterator();

		while (HashSetY.hasNext())
		{
			System.out.println("From ListEterator, HashSetY.next() = "
					+HashSetY.next());
		}



It Worked Thank you Sir AntonWebsters :D
Was This Post Helpful? 0
  • +
  • -

#5 AntonWebsters   User is offline

  • D.I.C Regular
  • member icon

Reputation: 89
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: How to Create Iterator object from the HashSet object?

Posted 07 September 2009 - 04:02 AM

You are welcome. =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1