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

    Exclamation help using hashsets?

    hello

    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?

    thanks

    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(); 
    		try{
    			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();
    	        		s1.add(st);	
    	    
    			}
    			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)
    		{
    			s2.addAll(visit(u));				
    		}
    	
    	        s1.addAll(s2);
    	        for (String u:s1) System.out.println(u);
    	    
    	}
    }

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

    Default

    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 11:09 PM. Reason: typo

Similar Threads

  1. HashSets
    By DavidG24 in forum New To Java
    Replies: 5
    Last Post: 04-19-2009, 07: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, 01: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
  •