JAVA筆記_集合物件, 泛型, 序列化


集合物件

java集合像一個容器,可以存下任意型別的資料且長度可變
依照結構可以分為兩類:

  • 單列集合Collection
    有兩個子介面 List(有序可重複),Set(無序不可重複)
  • 雙列集合Map
    有key/value對,key是唯一,主要子介面為HashMap

泛型集合物件

集合當中可以存有不同型態的資料,省去了很多型別轉換的麻煩

HashSet - Set的實作

  • 使用hashing方式進行新增,刪除,修改,和存取物件元素
  • 元素排列與插入順序不一定相同
  • HashSet裡的元素都是唯一的

創立物件

HashSet<String> hset = new HashSet<String>();  //泛型型態是String
HashSet<E>(int)  //建立空的HashSet物件 int是元素數

元素添加, 都以object類別型態加入 (可以有不同型別的元素)

hashset.add("abc");
hashset.add(1);
hashset.add('a');
int[] abc={10,11,12};
hashset.add(abc);
Cat cat1=new Cat("asd", 2);
hashset.add(cat1);

是否為空
hset.isEmpty();

顯示元素數量
hset.size();

是否有特定元素e存在
hset.contains(e);

元素e刪除
hset.remove(e);

HashSet清空
hset.clear();

HashMap - SortedMap介面實作

為鍵值對的形式, 鍵不可以重複 一個鍵對應到一個值,並且允許值為null
建立物件
HashMap< K , V >(int) hmap = new HashMap< K , V >();

新增元素
hmap.put("key1",value1);

是否為空
hmap.isEmpty();

物件元素數量
hmap.size();

特定keyK是否存在
hmap.get(K);

取得key K的對應值
hmap.get(K);

元素刪除
hmap.remove(K);

HashSet清空
hmap.clear();

拜訪物件中的所有元素

1.疊代器

public class Test1 {
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("3", "Jack");
        map.put("1", "Rock");
        map.put("2", "Tom");
        Set keySet = map.keySet();//獲取鍵的集合
        Iterator iterator = keySet.iterator();//迭代鍵的集合
        while (iterator.hasNext()) {
            Object key = map.keySet();
            Object value = map.get(key);//獲取每個鍵所對應的值
            System.out.println(key + ":" + value);
        }
    }
}

2.foreach

public class Test1 {
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("3", "Jack");
        map.put("1", "Rock");
        map.put("2", "Tom");
        map.forEach((key, value) -> System.out.println(key + ":" + value));
    }
}

泛型方法

Generic Method可以將無型態作為參數, 以宣告方法為泛型方法

e.g.可以印出任何型態陣列

public static <E> void printArray(E[] arr){
    for(E ele:arr){
        System.out.println(ele);
    }
}

泛型類別

Generic Classes跟一般類別無異,只是類別名稱後面會跟著型態參數
e.g.
class class1<T>{...}

class Box<T>{
    private T t;
    public void set(T t){
        this.t=t;
    }
    public T get(){
        return t;
    }    
}


public class test{
    public static void main(String args[]){
        Box<Integer> intBox=new Box<Integer>();
        Box<String> strBox=new Box<String>();

        intBox.set(5);
        strBox.set("Hello World!");

        System.out.println(intBox.get()); //5
        System.out.println(strBox.get()); //"Hello World!"
    }
}

序列化 Serialization

將泛型物件集合中的資料轉換成一組序列的位元組,用來存至檔案或是從檔案中讀出

要序列化的類別需要實作Serializable介面

public class Student implements Serializable{
    public String name;
    public String address;
    public transient int SSN;  //不想要序列化的屬性+transient
    public int score;

    public void printStudent(){
        System.out.println(name+"-"+address+"-"+SSN+"-"+score);
    }
}
public class test{
    public static void main(String args[]) throws Exception{
        Student s= new Student();
        s.name="賴";
        s.address="基隆市安樂";
        s.SSN=123;
        s.score=88;

        //物件存入檔案
        try{
            FileOutputStream fout= new FileOutputStream("student.ser");
            ObjectOutputStream out = new ObjectOutputStream(fout);
            out.writeObject(s);
            out.close();
            fout.close();
            s.printStudent(); //賴-基隆市安樂-123-88
        }

        Student s1=null;

        //從檔案讀取文件
        try{
            FileInputStream fin = new FileInputStream("Student.ser");
            ObjectInputStream in = new ObjectInputStream(fin);
            s1=(Student) in.readObject();
            in.close();
            fin.close();
            s1.printStudent(); //賴-基隆市安樂-0-88
        }
    }
}







你可能感興趣的文章

個人CodeBase紀錄 - EP.0 引言

個人CodeBase紀錄 - EP.0 引言

金魚系列、RWD (下) - RWD 選單

金魚系列、RWD (下) - RWD 選單

CSS 預處理

CSS 預處理






留言討論