Solved

I should have efficient Collections method

Posted on 2004-08-26
23
271 Views
Last Modified: 2010-03-31
This is my method

 private java.util.List[] getURLList(java.util.List list) {
    Hashtable ht = new Hashtable();
    for (int i = 0; i < list.size(); i++) {
      String url = list.get(i).toString();
      String array[] = url.split("/");

      String domain = array[0];
      if (array[0].toLowerCase().startsWith("http")) {
        domain = array[2];
        domain = domain + array[4];
      }
      else {
        domain = domain + array[2];
      }

      ArrayList sublist = (ArrayList) ht.get(domain);
      if (sublist == null) {
        sublist = new ArrayList();
      }
      sublist.add(url);
      ht.put(domain, sublist);
    }
    return (List[]) Collections.list(ht.elements()).toArray(new ArrayList[0]);
  }







from : http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21102101.html (Answered by CEHJ)
private static java.util.List[] getUrlList(java.util.List list) {
          Hashtable hostToUrls = new Hashtable();
          try {
               for (int i = 0; i < list.size(); i++) {
                    URL url = new URL((String) list.get(i));
                    String hostKey = url.getHost();
                         String protocol=url.getProtocol();
                    //     System.err.println(url.getProtocol() );
                    // Could be a Set if no duplicates required
                    ArrayList urlsForHost = (ArrayList)hostToUrls.get(protocol+hostKey);
                    if (urlsForHost == null) {
                         urlsForHost = new ArrayList();
                         urlsForHost.add(url);
                         hostToUrls.put(protocol+hostKey, urlsForHost);
                    }
                    else {
                         urlsForHost.add(url);
                    }
               }
          }
          catch (MalformedURLException e) {
               return null;
          }
          int numberOfLists = hostToUrls.size();
          return (java.util.List[]) Collections.list(hostToUrls.elements()).toArray(new ArrayList[numberOfLists]);
     }




Hello all,

This question was asked already by me. But I am looking at much more efficient method. The reason is although CEHJ's method is efficient, it is not giving correct sets. Actually my method gives the correct sets. But it is less efficient method than CEHJ's

So I need the correct results and should increase the performance of method than CEHJ's and my method.


for your testing,

Here is the test sample
http://www.geocities.com/sudhakar_koundinya/testsample.html (If u place the problem - copy the URL in location bar and press enter. Then it works :-D )

Actually what I am thinking is

return (List[]) Collections.list(ht.elements()).toArray(new ArrayList[0]); this line is giving poor performance results.

Any ideas are welcome from you guys

thank you
Sudhakar
0
Comment
Question by:sudhakar_koundinya
  • 13
  • 6
  • 3
  • +1
23 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11901189
>>it is not giving correct sets

What do you mean ? ;-)
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11901396
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11901400
my code gives three sets  where as your's give 2 sets

thanks
sudhakar
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11901431
Well yours looks wrong to me ;-) Or at least it's not working in the way i thought the algo was meant to work:

>>
List :0
http://192.12.0.38/exchange/vijay/Inbox/testattachment.EML
List :1
http://192.12.0.38/exchange/Administrator/Deleted Items/TEST Message 0056.EML
>>

AFAIK the unique key is host+protocol (or vice versa). Why then does the same host and protocol in your version (above) appear in different lists?
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11901635
CEHJ,

keys should be


http://192.12.0.38/exchange/vijay
http://192.12.0.38/exchange/Administrator

some thing like this where as

using ur code, we just have protocol and host

thanks,
sudhakar
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11901653
the key should be protocol+host+user
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11901664
>>using ur code, we just have protocol and host

But that's what i thought you wanted! Where is the key meant to stop - after the second (account?) directory? And are you separating on protocol too? i.e. should


http://192.12.0.38/exchange/vijay

and

https://192.12.0.38/exchange/vijay

appear in separate lists?
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11901675
this should be protocol+host+user final key for me
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11906608
CEHJ,

I got an Idea how to speed the result

But just wanted to clarify one thing from you

Initially I have declared some thing like this

Object object[]=new Object[0];

Now dynamically how can we Increase the size of array with out loosing previous results. Which we can do same in c/c++

0
 
LVL 92

Expert Comment

by:objects
ID: 11907362
> The reason is although CEHJ's method is efficient, it is not giving correct sets.

Then why did you accept it. You should ask for that question to be reopened instead of opening a new question.
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11907715
>> Then why did you accept it.

I have waited for some time for some other experts inputs. As no one participated, I thought that, I  should  close the question and then open it as new question

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 11907741
> I  should  close the question and then open it as new question

You shouldn't do that.
Either delete the question, and open a new one.
Or ask a new question linking to the old one.

CS can help you with it.
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11907791
OK,

shall I ask the CS people to reopen that question and move this question points to that Link?

Thanks
Sudhakar
0
 
LVL 92

Expert Comment

by:objects
ID: 11907836
you can probably delete this one, and ask a new 20 point q linking to the reopened question.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11910865
>>I have waited for some time for some other experts inputs

sudhakar - i haven't forgotten you. i've corrected the error but have not posted the code as i haven't optimized it yet. Do you want me to post it anyway?
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11910871
sure
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11910880
But plz post that in previous link. So that objects also will participate in that thread

Thanks
Sudhakar
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11910893
The reason is I have already asked the CS people to reopen the previous question

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11910896
Can you remind me which one it was? - i'm losing track ;-)
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11910900
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11913414
Also, could you post a link to some of the most representative data? - it will help with testing ;-)
0
 

Accepted Solution

by:
CetusMOD earned 0 total points
ID: 11915013
Closed, 250 points refunded per request http:Q_21109008.html

Please all continue at http:Q_21102101.html 

CetusMOD
Community Support Moderator
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11922492
Hello objects, are you there??
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 3 50
JAVA part two 5 57
backtracking recursion  code 19 53
map related example 6 32
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now