这个是统计一段话里相同单词的频率,原理应该差不多 package ex22; import java.util.*;public class CountOccurrenceOfWords { public static void main(String[] args) { // TODO 自动生成方法存根 String text = "Good morning. Have a good class. "+ "Have a good visit. Have fun!";
//Create a TreeMap to hold words as key and count as value TreeMap<String, Integer> map = new TreeMap<String, Integer>();
String[] words = text.split("[ \n\t\r.,;:!?(){]"); for(int i = 0; i < words.length; i++){ String key = words[i].toLowerCase();
if(key.length() > 0){ if(map.get(key) == null){ map.put(key, 1); } else{ int value = map.get(key).intValue(); value++; map.put(key, value); } } }
//Get all entries into a set Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
//Get key and value from each entry for(Map.Entry<String, Integer> entry: entrySet) System.out.println(entry.getValue()+ "\t"+ entry.getKey()); }}
这样? 如果每次不是加1,只要你知道是加几就行import java.net.URL; import java.util.*;public class URLCount{
public static void main(String[] args) throws Exception {
List<URL> urls = Arrays.asList(
new URL("http://www.google.com"), new URL("http://www.csdn.net"), new URL("http://www.google.com"), new URL("http://www.csdn.net"), new URL("http://www.google.com"), new URL("http://www.google.com"), new URL("http://www.csdn.net"), new URL("http://www.csdn.net"), new URL("http://www.google.com"), new URL("http://www.google.com") );
另外考虑下相加后依旧重复的情况
import java.util.Map;
import java.util.Set;public class Text1 {
public static void main(String[] args) throws Exception {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("1", 1);
map.put("2", 2);
map.put("3", 3);
map.put("4", 4);
map.put("5", 5);
map.put("6", 6);
int[] values = new int[]{1,2,3,4,756,6,4,5,7,34};
Text1.putVlues(map, values);
Set<String> keys = map.keySet();
for (String key : keys) {
System.out.println("key:"+key+" value:"+map.get(key));
}
} public static void putVlues(Map<String, Integer> map, int[] values) {
for (int i : values) {
Integer key = map.get(i+"");
if (key != null) {
map.put(i + "", key + 1);
} else {
map.put(i + "", i);
}
}
}
}
是不是这个意思嘛
然后统计出url的个数
package ex22;
import java.util.*;public class CountOccurrenceOfWords {
public static void main(String[] args) {
// TODO 自动生成方法存根
String text = "Good morning. Have a good class. "+
"Have a good visit. Have fun!";
//Create a TreeMap to hold words as key and count as value
TreeMap<String, Integer> map = new TreeMap<String, Integer>();
String[] words = text.split("[ \n\t\r.,;:!?(){]");
for(int i = 0; i < words.length; i++){
String key = words[i].toLowerCase();
if(key.length() > 0){
if(map.get(key) == null){
map.put(key, 1);
}
else{
int value = map.get(key).intValue();
value++;
map.put(key, value);
}
}
}
//Get all entries into a set
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
//Get key and value from each entry
for(Map.Entry<String, Integer> entry: entrySet)
System.out.println(entry.getValue()+ "\t"+ entry.getKey());
}}
如果每次不是加1,只要你知道是加几就行import java.net.URL;
import java.util.*;public class URLCount{
public static void main(String[] args) throws Exception {
List<URL> urls = Arrays.asList(
new URL("http://www.google.com"),
new URL("http://www.csdn.net"),
new URL("http://www.google.com"),
new URL("http://www.csdn.net"),
new URL("http://www.google.com"),
new URL("http://www.google.com"),
new URL("http://www.csdn.net"),
new URL("http://www.csdn.net"),
new URL("http://www.google.com"),
new URL("http://www.google.com")
);
Counter<URL> urlCounter = new Counter<URL>();
for(URL url : urls)
urlCounter.addCount(url, 1);
for(URL key : urlCounter)
System.out.println(key + " : " + urlCounter.getCount(key));
}
}class Counter<K> implements Iterable<K> {
private Map<K, Count> counts = new HashMap<K, Count>();
void addCount(K key, int count) {
if( key == null ) throw new NullPointerException();
Count c = counts.get(key);
if( c == null ) {
c = new Count();
counts.put(key, c);
}
c.add(count);
}
int getCount(K key) {
if( key == null ) throw new NullPointerException();
Count c = counts.get(key);
return c == null ? 0 : c.get();
}
@Override
public Iterator<K> iterator() {
return counts.keySet().iterator();
}
}class Count { private int count; public Count() { this.count = 0;
} public void add(int count) { this.count += count;
}
public int get() {
return count;
}
}