help using hashsets?
i have written a program which takes a URL and finds all the links contained in that page and then for each of those URLs finds the links in in that page. It should print all the links out. i do it by writing a method which takes a String representing a URL as a parameter and returns a HashSet of all the links visited. (below i am printing out all the links at Depth 1 and 2 from a given URL.)
my question is how do i do the same thing but without using recursion, and also this time print out all the URLs at depth 1, 2 and 3. what other methods are there to use or i can read up on?
public static HashSet<String> visit (String s)
HashSet <String> s1 = new HashSet();
Parser parser1 = new Parser (s);
NodeList list1 = parser1.parse (new LinkStringFilter("http:")); // no filter
for (int i=0;i<list1.size();i++)
String st = ((LinkTag)(list1.elementAt(i))).extractLink();
catch (Exception e)
return new HashSet();
public static void main(String args) throws Exception
HashSet <String> s1 = visit(args);
HashSet <String> s2 = new HashSet();
for (String u:s1)
for (String u:s1) System.out.println(u);
Well, problems like this are inherently recursive, so I think this solution should work well. As for printing out all links at a certain level, have a look at tree traversals, what you're looking for is called inorder traversal.