隨筆-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
          主站蜘蛛池模板: 胶州市| 晋城| 祁东县| 邳州市| 肃宁县| 宁阳县| 米易县| 互助| 枣强县| 景德镇市| 三台县| 北流市| 商城县| 三河市| 盐源县| 海口市| 同江市| 永善县| 临安市| 白水县| 北宁市| 平武县| 景泰县| 湘潭县| 沈阳市| 贞丰县| 沙湾县| 鄂伦春自治旗| 康平县| 高淳县| 社旗县| 西华县| 甘谷县| 西昌市| 西吉县| 姚安县| 犍为县| 章丘市| 海伦市| 韩城市| 武鸣县|