Iterating through a HashSet

I am trying the code below as I want to iterate through and display the contents of the HashSet, but this doesn't work.

How do you iterate through a HashSet and display it?

Set intersection =new HashSet(listSet);

intersection.retainAll(mapSet);

Iterator iter = intersection.iterator();

while (iter.hasNext())

{

AccountList var = (AccountList)iter.next();

System.out.println("INTERSECTION IS "+var.getAccountNumber());

}

[625 byte] By [McAlexa] at [2007-11-26 14:49:13]
# 1
Why do you say it doesn't work? Are you getting error messages?
paulcwa at 2007-7-8 8:37:08 > top of Java-index,Java Essentials,Java Programming...
# 2

> How do you iterate through a HashSet

Like how you have done it.

> and display it?

Dunno, what is in the object that you are trying to display? If your AccountList is a list then I'm guessing you should you be iterating over it, not calling getAccountNumber(). Does AccountList have an account number or is it a list of Accounts?

Ted.

ted_trippina at 2007-7-8 8:37:08 > top of Java-index,Java Essentials,Java Programming...
# 3

I think this code will help you......

import java.io.*;

import java.util.*;

class simple{

public static void main(String args[]){

HashSet listSet = new HashSet();

listSet.add("AccountNo:");

listSet.add("Account Name:");

listSet.add("Address:");

listSet.add("City:");

Set intersection = new HashSet(listSet);

//intersection.retainAll(mapSet);

Iterator iter = intersection.iterator();

while (iter.hasNext())

{

String var = (String)iter.next();

System.out.println("INTERSECTION IS: "+var);

}

}

}

ggopia at 2007-7-8 8:37:08 > top of Java-index,Java Essentials,Java Programming...
# 4

I am getting a ClassCastException at the point marked below. What can iter.next() be cast to?

Set intersection = new HashSet(listSet);

intersection.retainAll(mapSet);

Iterator iter = intersection.iterator();

while (iter.hasNext())

{

AccountList var = (AccountList)iter.next(); <- Get a ClassCastException here

System.out.println("INTERSECTION IS "+var.getAccountNumber());

}

McAlexa at 2007-7-8 8:37:08 > top of Java-index,Java Essentials,Java Programming...
# 5

>

> AccountList var = (AccountList)iter.next();

> <- Get a ClassCastException here

> System.out.println("INTERSECTION IS

> "+var.getAccountNumber());

>

This clearly implies that your Set doesn't contain AccountList

objects.

Check what exactly goes into the Set.

fun_one

FUN_ONEa at 2007-7-8 8:37:08 > top of Java-index,Java Essentials,Java Programming...
# 6

I did the loop below before putting the list into the Set.

List list= new ArrayList();

for (int i = 0; i < theList.size(); i++)

{

AccountList list = (AccountList)theList.get(i);

list.add(list.getAccountNumber());

System.out.println("the new list "+list.get(i));

}

McAlexa at 2007-7-8 8:37:08 > top of Java-index,Java Essentials,Java Programming...
# 7

AccountList list = (AccountList)theList.get(i);

list.add(list.getAccountNumber());

System.out.println("the new list "+list.get(i));

Your AccountList doesn't exist outside of the for loop. You declared an AccountList with the same variable name as a List, so when you access list after that loop, you're accessing the List instead of the AccountList like you probably think.

Why don't you try posting the whole thing together so we don't have to try to put bits and pieces together.

hunter9000a at 2007-7-8 8:37:08 > top of Java-index,Java Essentials,Java Programming...
# 8

> I did the loop below before putting the list into the

> Set.

>

> > List list= new ArrayList();

> for (int i = 0; i < theList.size(); i++)

> {

>AccountList list = (AccountList)theList.get(i);

> list.add(list.getAccountNumber());

> System.out.println("the new list

> "+list.get(i));

>

>

I intuitively see that this could be a problem with two objects declared with the same name 'list'. Please elaborate on what this AccountList class is.

You can try your luck changing

>AccountList list = (AccountList)theList.get(i);

> list.add(list.getAccountNumber());

to

>AccountList accList = (AccountList)theList.get(i);

> list.add(accList.getAccountNumber());

Also it would be more helpful if you post the complete stack trace.

fun_one

FUN_ONEa at 2007-7-8 8:37:08 > top of Java-index,Java Essentials,Java Programming...