Results 1 to 2 of 2
  1. #1
    yasmin k is offline Member
    Join Date
    Mar 2009
    Rep Power

    Exclamation 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?


    Java Code:
    import org.htmlparser.util.*;	
    import org.htmlparser.*;
    import org.htmlparser.tags.*;
    import org.htmlparser.filters.*;
    import java.util.HashSet;
    class crawler2
    	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();
    			return s1;
    		catch (Exception e)
    			return new HashSet();
    	public static void main(String args[]) throws Exception	
    		HashSet <String> s1 = visit(args[0]);
    		HashSet <String> s2 = new HashSet();
    		for (String u:s1)
    	        for (String u:s1) System.out.println(u);

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Ljubljana, Slovenia
    Rep Power


    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.
    Last edited by m00nchile; 04-17-2010 at 10:09 PM. Reason: typo

Similar Threads

  1. HashSets
    By DavidG24 in forum New To Java
    Replies: 5
    Last Post: 04-19-2009, 06:02 PM
  2. how does the remove method work for sets and hashsets
    By haridharna in forum Advanced Java
    Replies: 4
    Last Post: 08-06-2007, 12:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts