隨筆-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 閱讀(1421) 評論(0)  編輯  收藏 所屬分類: JAVA/J2EE
          主站蜘蛛池模板: 广汉市| 武胜县| 颍上县| 阳谷县| 专栏| 利辛县| 台州市| 巴彦淖尔市| 宝鸡市| 广东省| 平果县| 民县| 巨鹿县| 清涧县| 渝中区| 五常市| 喀什市| 会昌县| 阳城县| 巴东县| 闻喜县| 仙游县| 沙湾县| 武平县| 绥宁县| 西畴县| 碌曲县| 营口市| 永定县| 砀山县| 周口市| 秭归县| 元阳县| 德州市| 贵阳市| 金乡县| 丹阳市| 安多县| 平南县| 扬州市| 克拉玛依市|