, , TreeSet.
TreeSet , . , URL- , , , . TreeSet. /// O (logn)
, .
TreeSet<URL> treeSet = new TreeSet<URL>(new URLComparator());
class URL {
private String url;
int count;
public URL(String string, int i) {
url = string;
count = i;
}
@Override
public int hashCode() {
return url.hashCode();
}
@Override
public String toString() {
return "url : " + url + " ,count : " + count+"\n";
}
}
. hashcode URL- hashcode URL.
URLComparator. URL-.
class URLComparator implements Comparator<URL> {
@Override
public int compare(URL o1, URL o2) {
return new Integer(o2.count).compareTo(o1.count);
}
}
TreeSet<URL> treeSet = new TreeSet<URL>(new URLComparator());
treeSet.add(new URL("url1", 12));
treeSet.add(new URL("url2", 0));
treeSet.add(new URL("url3", 5));
System.out.println(treeSet);
: -
[url : url1 ,count : 12
, url : url3 ,count : 5
, url : url2 ,count : 0
]
10 , .
Iterator<URL> iterator = treeSet.iterator();
int count = 0;
while(count < 10 && iterator.hasNext() ){
System.out.println(iterator.next());
count++;
}