随笔-28  评论-51  文章-10  trackbacks-0
hash哈希

重写hashCode()和equals()
import java.util.*

public class Hashs {
     
public static void main(String[] args){
           HashMap
<Element,Figureout> h2=new HashMap<Element,Figureout>();
          
for(int i=0;i<10;i++)
                h2.put(
new Element(i), new Figureout());
           System.out.println(
"h2:");
           System.out.println(
"Get the result for Element:");
           Element test
=new Element(3);
          
if(h2.containsKey(test))
                System.out.println((Figureout)h2.get(test));
          
else
                System.out.println(
"Not found");
      }
}
class Element{
     
int number;
     
public Element(int n){
           number
=n;
      } 
     
public int hashCode()
     {
        
return number; 
     }
     
public boolean equals(Object o)
     {
         
return (o instanceof Element)&&( number ==((Element)o).number);
            
     }
}

class Figureout{
      Random r
=new Random();
     
boolean possible=r.nextDouble()>0.5;
     
public String toString(){
          
if(possible)
               
return "OK!";
          
else
               
return "Impossible!";
      }
}


哈希应用---字符统计

import java.util.HashMap;

public class CountWords {
    
public static void count(String target) {
        String[] array 
= target.split(" ");//以空格来分隔

        HashMap
<String,Integer> map = new HashMap<String,Integer>();
        
for (String ss : array) {
            
if (map.containsKey(ss)) {
                map.put(ss, map.get(ss)
+1);
            } 
else {
                map.put(ss, 
1);
            }
        }
        System.out.println(map);
    }
    
public static void main(String[] args) {
        String testString 
= "kuikui is good man! yes ! kuikui is good man .";
        CountWords.count(testString);
    }

}

字符串和正则 
主要有StreamTokenizer, String.split,StringTokenizer,

前两者可以使用正则作为参数,后者只能用直接分隔符作为参数

正则可以在jdk中找到

(http://hi.baidu.com/ecgql/blog/item/f176882b0c66affbe6cd40b5.html
http://www.javaeye.com/subject/Regular-Expression),

也可以使用开源包Jakarta-ORO(http://www.ccw.com.cn/htm/app/aprog/01_7_31_4.asp)

特别地:
在使用String.split方法分隔字符串时,分隔符如果用到一些特殊字符,可能会得不到我们预期的结果。

我们看jdk doc中说明

public String[] split(String regex)

 Splits 
this string around matches of the given regular expression. 

参数regex是一个 regular
-expression的匹配模式而不是一个简单的String,他对一些特殊的字符可能会出现你预想不到的结果,比如测试下面的代码:

用竖线 
| 分隔字符串,你将得不到预期的结果

    String[] aa 
= "aaa|bbb|ccc".split("|");
    
//String[] aa = "aaa|bbb|ccc".split("\\|"); 这样才能得到正确的结果

    
for (int i = 0 ; i <aa.length ; i++ ) {
      System.out.println(
"--"+aa[i]);
    }

用竖 
* 分隔字符串运行将抛出java.util.regex.PatternSyntaxException异常,用加号 + 也是如此。

    String[] aa 
= "aaa*bbb*ccc".split("*");
    
//String[] aa = "aaa|bbb|ccc".split("\\*"); 这样才能得到正确的结果   

    
for (int i = 0 ; i <aa.length ; i++ ) {
      System.out.println(
"--"+aa[i]);
    }

显然,
+ * 不是有效的模式匹配规则表达式,用"\\*" "\\+"转义后即可得到正确的结果。

"|" 分隔串时虽然能够执行,但是却不是预期的目的,"\\|"转义后即可得到正确的结果。

还有如果想在串中使用
"\"字符,则也需要转义.首先要表达"aaaa\bbbb"这个串就应该用"aaaa\\bbbb",如果要分隔就应该这样才能得到正确结果:

String[] aa 
= "aaa\\bbb\\bccc".split("\\\\");


posted on 2007-11-04 12:55 fullfocus 阅读(1408) 评论(0)  编辑  收藏 所属分类: JAVA/J2EE

只有注册用户登录后才能发表评论。


网站导航: