Store web links in a file. How to find top 20 links
Page 1 of 1
Store web links in a file. How to find top 20 links
public static void findlinks(String[] str){
Hashtable<String> hash=new Hashtable<String>();
for(int i=0;i<str.length();i++){
if(!hash.contains(str[i])){
hash.put(str[i],new Integer(0));
}
else{
int temp=hash.get(str[i]).intvalue();
hash.put(str[i],new Integer(temp+1));
}
}
MyComparator my=new MyComparator();
Object[] arr=hash.entrySet.toArray();
Array.sort(arr,my);
for(int j=0;j<20;j++){
System.out.println("This "+j+" 's link is "+arr[j]);
}
}
}
class MyComparator implement Comparator{
public int compare(Object o1, Object o2){
if(((Integer)(Map.entry)o1.getValue()).intValue()>((Integer)(Map.entry)o2.getValue()).intValue())
return 1;
else if(((Integer)(Map.entry)o1.getValue()).intValue()<((Integer)(Map.entry)o2.getValue()).intValue())
return -1;
return 0;
}
}
Hashtable<String> hash=new Hashtable<String>();
for(int i=0;i<str.length();i++){
if(!hash.contains(str[i])){
hash.put(str[i],new Integer(0));
}
else{
int temp=hash.get(str[i]).intvalue();
hash.put(str[i],new Integer(temp+1));
}
}
MyComparator my=new MyComparator();
Object[] arr=hash.entrySet.toArray();
Array.sort(arr,my);
for(int j=0;j<20;j++){
System.out.println("This "+j+" 's link is "+arr[j]);
}
}
}
class MyComparator implement Comparator{
public int compare(Object o1, Object o2){
if(((Integer)(Map.entry)o1.getValue()).intValue()>((Integer)(Map.entry)o2.getValue()).intValue())
return 1;
else if(((Integer)(Map.entry)o1.getValue()).intValue()<((Integer)(Map.entry)o2.getValue()).intValue())
return -1;
return 0;
}
}
Similar topics
» Given a sorted array find all possible |ai - aj| where ai,aj belongs to Array A. n^2 is obvious. Find a solution in O(N).
» Find if a singly linked List has loop or not. How to find out middle element from a looped single linked list
» Sort 10 GB file using 2 GB memory. and complexity
» Describe an algorithm to save a Binary Search Tree (BST) to a file
» Find the height of BST
» Find if a singly linked List has loop or not. How to find out middle element from a looped single linked list
» Sort 10 GB file using 2 GB memory. and complexity
» Describe an algorithm to save a Binary Search Tree (BST) to a file
» Find the height of BST
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum