java api 接口篇(二)上

http://tech.ddvip.com   2006年11月23日    社区交流

本文详细介绍java api 接口篇(二)上

  下列程序是这个技术的一个直接的实现。仅有的技巧部分是alphabetize方法,它返回一个串,这个串包含与它的参数相同的字符,并按字母顺序排列。这个例程(它与Collections Framework无关) 实现一个巧妙的桶式分类。 它假定按字母顺序排列的单词完全由小写字母组成。

  import java.util.*;
import java.io.*; 
 
public class Perm {
public static void main(String[] args) {
int minGroupSize = Integer.parseInt(args[1]); 
 
// Read words from file and put into simulated multimap
Map m = new HashMap();
try {
BufferedReader in =
new BufferedReader(new FileReader(args[0]));
String word;
while((word = in.readLine()) != null) {
String alpha = alphabetize(word);
List l = (List) m.get(alpha);
if (l==null)
m.put(alpha, l=new ArrayList());
l.add(word);
}
} catch(IOException e) {
System.err.println(e);
System.exit(1);
} 
 
// Print all permutation groups above size threshold
for (Iterator i = m.values().iterator(); i.hasNext(); ) {
List l = (List) i.next();
if (l.size() $#@62; = minGroupSize)
System.out.println(l.size() + ": " + l);
}
} 
 
private static String alphabetize(String s) {
int count[] = new int[256];
int len = s.length();
for (int i=0; i$#@60; len; i++)
count[s.charAt(i)]++;
StringBuffer result = new StringBuffer(len);
for (char c="a"; c$#@60; ="z"; c++)
for (int i=0; i$#@60; count[c]; i++)
result.append(c);
return result.toString();
}
}
 

责编:豆豆技术应用

正在加载评论...