java基礎

          關于java的集合類,以及HashMap中Set的用法!2007-08-07 16:17關于java的集合類,以及HashMap中Set的用法!  
           
          2005-10-22 14:47:43 Sat | 閱讀(547)次  
           
           
          package com.tiantian;

          import java.util.*;

          public class JAVAContainer {

               
          public static void main(String[] args) throws Exception {
                   
          //     ArrayList
                   {
                       ArrayList arraylist 
          = new ArrayList();
                       arraylist.add(
          0"end");//指定索引加入值
                       
          //     需注意的是,如果現有2個值,我加入索引為5的那么就會出現異常
                       for (int i = 0; i < 2; i++{
                           arraylist.add(i, String.valueOf(i));
                       }

                       System.out.println(
          "ArrayList:");
                       
          for (int i = 0; i < arraylist.size(); i++{
                           System.out.print(arraylist.get(i) 
          + ";");
                       }

                       arraylist.add(
          "0");//直接加入值到ArrayList的最后
                       arraylist.add("0");
                       System.out.print(
          " ArrayList's lastIndexOf("0") is "
                               
          + arraylist.lastIndexOf("0"));
                   }

                   
          //     Arrays
                   {
                       String[] array 
          = new String[] "a""b""c" };
                       List list 
          = Arrays.asList(array);
                       System.out.println(
          " Arrays:");
                       
          for (int i = 0; i < list.size(); i++{
                           System.out.print(list.get(i) 
          + ";");
                       }

                       System.out.print(
          " Arrays's length is " + array.length);//打印數組的長度
                   }

                   
          //     Collections
                   {
                       String[] array 
          = new String[] "a""b""c" };
                       List list 
          = Arrays.asList(array);
                       Collections.fill(list, 
          "Fill");//用Fill填充全部元素
                       System.out.println(" Collections:");
                       
          for (int i = 0; i < list.size(); i++{
                           System.out.print(list.get(i) 
          + ";");
                       }

                       array 
          = new String[] "1""2""3" };
                       List list2 
          = Arrays.asList(array);
                       Collections.copy(list, list2);
          //拷貝list2的數據進list
                       System.out.println(" " + list);
                       Collections.swap(list, 
          21);//調換索引為1和2的元素的位置
                       System.out.println(list);
                   }

                   
          //     EventObject
                   {
                       String s 
          = "hello";
                       String s2 
          = s;
                       EventObject eventobject 
          = new EventObject(s);//一個準容器類型,確切的歸類它不是容器
                       System.out.println("EventObject:");
                       System.out.println(eventobject.getSource());
                       System.out.println(eventobject.equals(s2));
                   }

                   
          //     HashMap
                   {
                       HashMap hashmap 
          = new HashMap();//一個速度最快的容器
                       hashmap.put("0""c");
                       hashmap.put(
          "1""a");
                       hashmap.put(
          "2""b");
                       hashmap.put(
          "3""a");
                       System.out.println(
          "HashMap:");
                       System.out.println(hashmap);
          //該容器有其內部的排序方式
                       Set set = hashmap.keySet();//獲取全部鍵
                       Iterator iterator = set.iterator();
                       
          while (iterator.hasNext()) {
                           System.out.print(hashmap.get(iterator.next()) 
          + ";");
                       }

                   }

                   
          //     HashSet
                   {
                       HashSet hashset 
          = new HashSet();//一個絕對不能重復的類型
                       hashset.add("c");
                       hashset.add(
          "b");
                       hashset.add(
          "a");
                       hashset.add(
          "a");
                       hashset.add(
          "b");
                       System.out.println(
          " HashSet:");
                       System.out.println(hashset);
                       Iterator iterator 
          = hashset.iterator();//取出元素
                       while (iterator.hasNext()) {
                           System.out.print(iterator.next() 
          + ";");
                       }

                   }

                   
          //     Hashtable
                   {
                       Hashtable hashtable 
          = new Hashtable();//一個完全可以由其他容器替換的老容器類型
                       hashtable.put("0""c");
                       hashtable.put(
          "1""a");
                       hashtable.put(
          "3""c");
                       hashtable.put(
          "2""b");
                       System.out.println(
          " Hashtable:");
                       Enumeration enumeration 
          = hashtable.elements();//獲取元素,Enumeration已經不是主流,Iterator是它的下一代替代品
                       while (enumeration.hasMoreElements()) {
                           System.out.print(enumeration.nextElement() 
          + ";");
                       }

                   }

                   
          //     IdentityHashMap
                   {
                       IdentityHashMap identityhashmap 
          = new IdentityHashMap();
                       identityhashmap.put(
          "0""c");
                       identityhashmap.put(
          "1""a");
                       identityhashmap.put(
          "3""b");
                       identityhashmap.put(
          "2""a");
                       System.out.println(
          " IdentityHashMap:");
                       System.out.println(identityhashmap);
                       System.out.println(identityhashmap.containsKey(
          "3"));//是否包含這個鍵
                       System.out.println(identityhashmap.containsValue("a"));//是否包含值
                       Set set = identityhashmap.entrySet();//傳為Set類型
                       System.out.println(set);
                       set 
          = identityhashmap.keySet();//全部鍵
                       System.out.println(set);
                   }

                   
          //     LinkedHashMap
                   {
                       LinkedHashMap linkedhashmap 
          = new LinkedHashMap();
                       linkedhashmap.put(
          "0""b");
                       linkedhashmap.put(
          "2""a");
                       linkedhashmap.put(
          "1""c");
                       linkedhashmap.put(
          "3""b");
                       System.out.println(
          "LinkedHashMap:");
                       System.out.println(linkedhashmap);
                       System.out.println(linkedhashmap.containsKey(
          "2"));//是否包含這個鍵
                       System.out.println(linkedhashmap.containsValue("c"));//是否包含值
                       Set set = linkedhashmap.keySet();
                       Iterator iterator 
          = set.iterator();
                       
          while (iterator.hasNext()) {
                           System.out.print(linkedhashmap.get(iterator.next()) 
          + ";");
                       }

                   }

                   
          //     LinkedHashSet
                   {
                       LinkedHashSet linkedhashset 
          = new LinkedHashSet();//它包含了幾種Set的屬性但卻沒有自己的特色
                       linkedhashset.add("c");
                       linkedhashset.add(
          "a");
                       linkedhashset.add(
          "a");
                       linkedhashset.add(
          "b");
                       System.out.println(
          " LinkedHashSet:");
                       System.out.println(linkedhashset);
                       System.out.println(linkedhashset.contains(
          "a"));//是否包含對象
                       Iterator iterator = linkedhashset.iterator();
                       
          while (iterator.hasNext()) {
                           System.out.print(iterator.next() 
          + ";");
                       }

                   }

                   
          //     LinkedList
                   {
                       LinkedList linkedlist 
          = new LinkedList();//自由使用是它的特色
                       linkedlist.add("a");
                       linkedlist.add(
          1"c");
                       linkedlist.addLast(
          "b");
                       linkedlist.addFirst(
          "d");
                       System.out.println(
          " LinkedList:");
                       System.out.println(linkedlist);
                       
          //     linkedlist.clear();//該方法清空容器
                       
          //     linkedlist.remove(0);//刪除索引為0的元素
                       
          //     linkedlist.remove("d");//刪除值為d的元素
                       
          //     linkedlist.removeFirst();//刪除第一個元素
                       
          //     linkedlist.removeLast();//刪除最后一個元素
                       for (int i = 0; i < linkedlist.size(); i++{
                           System.out.print(linkedlist.get(i) 
          + ";");
                       }

                   }

                   
          //     Stack
                   {
                       Stack stack 
          = new Stack();//堆棧
                       stack.add("b");
                       stack.add(
          0"c");
                       stack.push(
          "d");
                       stack.add(
          "e");
                       stack.push(
          "a");
                       Enumeration enumeration 
          = stack.elements();
                       System.out.println(
          " Stack:");
                       
          while (enumeration.hasMoreElements()) {
                           System.out.print(enumeration.nextElement() 
          + ";");
                       }

                       
          //     后進先出
                       System.out.println(" " + stack.peek());
                       System.out.println(stack.pop());
                       System.out.println(stack.contains(
          "d"+ ";" + stack.contains("a"));//是否包含該元素,有趣的事情發生了
                       System.out.println(stack.search("c"));//非常有用的屬性,檢索,但是由后向前的排列
                   }

                   
          //     TreeMap
                   {
                       TreeMap treemap 
          = new TreeMap();
                       treemap.put(
          "0""d");
                       treemap.put(
          "2""a");
                       treemap.put(
          "1""b");
                       treemap.put(
          "3""c");
                       System.out.println(
          " TreeMap:");//可以對鍵排序
                       System.out.println(treemap);
                       System.out.println(treemap.firstKey());
          //返回第一個鍵
                       Set set = treemap.keySet();
                       Iterator iterator 
          = set.iterator();
                       
          while (iterator.hasNext()) {
                           System.out.print(treemap.get(iterator.next()) 
          + ";");
                       }

                   }

                   
          //     TreeSet
                   {
                       TreeSet treeset 
          = new TreeSet();//自動排序內容
                       treeset.add("b");
                       treeset.add(
          "a");
                       treeset.add(
          "c");
                       treeset.add(
          "d");
                       System.out.println(
          " TreeSet:");
                       System.out.println(treeset);
                       System.out.println(treeset.first());
          //返回第一個元素
                       Iterator iterator = treeset.iterator();
                       
          while (iterator.hasNext()) {
                           System.out.print(iterator.next() 
          + ";");
                       }

                   }

                   
          //     Vector
                   {
                       Vector vector 
          = new Vector();
                       vector.add(
          0"b");
                       vector.add(
          "a");
                       vector.addElement(
          "d");
                       vector.add(
          "c");
                       System.out.println(
          " Vector:");
                       System.out.println(vector);
                       vector.set(
          2"h");//替換掉指定索引的元素
                       System.out.println(vector);
                       Object[] str 
          = vector.toArray();
                       
          for (int i = 0; i < str.length; i++{
                           System.out.print(str[i] 
          + ";");
                       }

                       vector.setSize(
          2);//重新設置大小為2
                       System.out.println(" " + vector);
                   }

                   
          //     WeakHashMap
                   {
                       WeakHashMap weakhashmap 
          = new WeakHashMap();
                       weakhashmap.put(
          "1""b");
                       weakhashmap.put(
          "2""c");
                       weakhashmap.put(
          "0""d");
                       weakhashmap.put(
          "3""a");
                       System.out.println(
          " WeakHashMap:");
                       System.out.println(weakhashmap);
                       System.out.println(weakhashmap.containsKey(
          "3"));//是否包含鍵
                       System.out.println(weakhashmap.containsValue("d"));//是否包含值
                       Set set = weakhashmap.entrySet();
                       Iterator iterator 
          = set.iterator();
                       
          while (iterator.hasNext()) {
                           System.out.print(iterator.next() 
          + ";");
                       }

                       
          //     weakhashmap.remove("2");//刪除該鍵對應的值
                       
          //     weakhashmap.get("1");//獲取指定鍵的值
                   }

               }

           
           
           

          posted on 2008-01-25 08:45 youngturk 閱讀(221) 評論(0)  編輯  收藏 所屬分類: 新認識筆記

          <2008年1月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          公告

          this year :
          1 jQuery
          2 freemarker
          3 框架結構
          4 口語英語

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          EJB學習

          Flex學習

          learn English

          oracle

          spring MVC web service

          SQL

          Struts

          生活保健

          解析文件

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 休宁县| 高要市| 信宜市| 乳源| 安图县| 黄冈市| 鄱阳县| 乌什县| 井冈山市| 军事| 麻城市| 石嘴山市| 江西省| 额济纳旗| 宁蒗| 龙岩市| 临漳县| 台安县| 济南市| 磐安县| 上蔡县| 北流市| 济宁市| 麦盖提县| 平湖市| 沽源县| 贡觉县| 修武县| 齐齐哈尔市| 建始县| 含山县| 怀柔区| 武平县| 罗城| 崇义县| 腾冲县| 五常市| 新田县| 资中县| 凤城市| 南陵县|