HashSet簡單實現

package com.yys.student;

import java.util.HashMap;

/**
 * Created by yys on 2017/5/11.
 */
public class SxtHashSet {
    HashMap map;
    private static final Object PRESENT = new Object();
    SxtHashSet(){
        map = new HashMap();
    }
    public int size(){
        return map.size();
    }
    public void put(Object o){
        map.put(o,PRESENT);//set的不重複就是利用了map裏面鍵對象不可重複
    }
    public static void main(String args[]){
        SxtHashSet hashSet = new SxtHashSet();
        hashSet.put("aaa");
        hashSet.put(new String("aaa"));
        System.out.println(hashSet.size());
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章