Java 数据结构

一.数据结构

  1. 枚举—Enumeration
  2. 位集合—BitSet
  3. 向量—Vector
  4. 栈—Stack
  5. 字典—Dictionary
  6. 哈希表—Hashtable
  7. 属性—Properties

1.枚举—Enumeration

基本使用案例:

public enum TypesOf {
    //项目类型 1.服务类型 2.工程类型 3.货物类型
    SERVICE("服务类型",1),ENGINEERING("工程类型",2),GOODS("货物类型",3);
    private String name;
    private int index;
    private TypesOf( String name , int index ){
        this.name = name ;
        this.index = index ;
    }
    
    public static  String geTypesOfTName(int index){
        TypesOf[] tf=TypesOf.values();
        for (int i = 0; i < tf.length; i++) {
            if (tf[i].index == index){
              return tf[i].name;
            }
        }
        return "no data";
    }

}
public class Test {
    public static void main(String[] args){

        System.out.println(TypesOf.geTypesOfTName(1));
        //输出结果: 服务类型
    }
}

总结:枚举一般多用于常量。

2.位集合—BitSet

位集合类实现了一组可以单独设置和清除的位或标志。
该类在处理一组布尔值的时候非常有用,你只需要给每个值赋值一"位",然后对位进行适当的设置或清除,就可以对布尔值进行操作了。

3.向量—Vector

vector和数组有点像他的相似之处是Vector对象的元素也能通过索引访问,不同之处是数字需要指定大小,而vector是根据需要自动变化的声明时不需要指定大小
Vector是线程安全的
在这里插入图片描述初值默认为10
在这里插入图片描述如果实际容量超过Vector初始值,就以10的一倍扩大
话不多说,上代码那么还有一种情况就是我们初始给Vector定值为5
代码解释一切也就是说Vector容量是根据容量的一倍作为增长的
问题一:假设我们要从一些数据中找出是否包含某个指定名称比如是否包含 “王6锤”
如果数据量很大用其他集合就比较慢,而这个时候用Vector.contains(Object o)就很快得出结论
在这里插入图片描述在这里插入图片描述### 4.栈—Stack
先进后出,当你添加一个新元素时,就将新元素放在其他元素的顶部

public class StackTest {
    static void showpush(Stack<Integer> st, int a) {
        st.push(new Integer(a));
        System.out.println("push(" + a + ")");
        System.out.println("stack: " + st);
    }

    static void showpop(Stack<Integer> st) {
        System.out.print("pop -> ");
        Integer a = (Integer) st.pop();
        System.out.println(a);
        System.out.println("stack: " + st);
    }

    public static void main(String args[]) {
        Stack<Integer> st = new Stack<Integer>();
       for (int i=0;i<10;i++){
           showpush(st,i);
       }

        System.out.println("stack: " + st);
        for (int i=0;i<10;i++){
            showpop(st);
        }
        
    }
}

运行结果

push(0)
stack: [0]
push(1)
stack: [0, 1]
push(2)
stack: [0, 1, 2]
push(3)
stack: [0, 1, 2, 3]
push(4)
stack: [0, 1, 2, 3, 4]
push(5)
stack: [0, 1, 2, 3, 4, 5]
push(6)
stack: [0, 1, 2, 3, 4, 5, 6]
push(7)
stack: [0, 1, 2, 3, 4, 5, 6, 7]
push(8)
stack: [0, 1, 2, 3, 4, 5, 6, 7, 8]
push(9)
stack: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
stack: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
pop -> 9
stack: [0, 1, 2, 3, 4, 5, 6, 7, 8]
pop -> 8
stack: [0, 1, 2, 3, 4, 5, 6, 7]
pop -> 7
stack: [0, 1, 2, 3, 4, 5, 6]
pop -> 6
stack: [0, 1, 2, 3, 4, 5]
pop -> 5
stack: [0, 1, 2, 3, 4]
pop -> 4
stack: [0, 1, 2, 3]
pop -> 3
stack: [0, 1, 2]
pop -> 2
stack: [0, 1]
pop -> 1
stack: [0]
pop -> 0
stack: []

获取时从栈中取元素的时候,就从栈顶取一个元素

5.字典—Dictionary

public class DictionaryTest{
    public static void main(String[] args) {

        Dictionary  diy=new Hashtable<String,Object>();
        diy.put("key",123456789);
        System.out.println(diy.get("key"));

    }
}

和Map大致一样是键值对的数据结构,该类已经过时。

6.哈希表—Hashtable

数据结构为键值对,线程安全,如果不追求线程安全介意使用HashMap,如果要追求性能以及线程安全介意使用java.util.concurrent.ConcurrentHashMap
1.Hashtable的存储结构 (数组+链表)

  /**
     * Constructs a new, empty hashtable with a default initial capacity (11)
     * and load factor (0.75).
     */
    public Hashtable() {
        this(11, 0.75f);
    }

默认初始容量哈希表值为“11”,和负载因子为0.75。
扩容的函数为2N+1
源代码

protected void rehash() {
        int oldCapacity = table.length;
        Entry<?,?>[] oldMap = table;

        // overflow-conscious code
        int newCapacity = (oldCapacity << 1) + 1;
        if (newCapacity - MAX_ARRAY_SIZE > 0) {
            if (oldCapacity == MAX_ARRAY_SIZE)
                // Keep running with MAX_ARRAY_SIZE buckets
                return;
            newCapacity = MAX_ARRAY_SIZE;
        }
        Entry<?,?>[] newMap = new Entry<?,?>[newCapacity];

        modCount++;
        threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
        table = newMap;

        for (int i = oldCapacity ; i-- > 0 ;) {
            for (Entry<K,V> old = (Entry<K,V>)oldMap[i] ; old != null ; ) {
                Entry<K,V> e = old;
                old = old.next;

                int index = (e.hash & 0x7FFFFFFF) % newCapacity;
                e.next = (Entry<K,V>)newMap[index];
                newMap[index] = e;
            }
        }
    }

rehash() :增加这个散列表的内部重组能力,从而更有效地适应和访问其条目。也就说当前容量不够就会调用此方法进行重组int newCapacity = (oldCapacity << 1) + 1;扩容直接位移<<1这样就是2的倍数
所以扩容的函数为2*N+1

什么是负载因子?
负载因子是在容量自动增加之前允许哈希表得到满足的度量。
通俗点讲就是Hsah表中元素的填满的程度加载因子越大,填满的元素越多,好处是,空间利用率高,但:冲突的机会加大了。反之,加载因子越小,填满的元素越少,好处是:冲突的机会减小了,但:空间浪费多了

7.属性—Properties

Properties 继承于 Hashtable.表示一个持久的属性集.属性列表中每个键及其对应值都是一个字符串。


public class PropertiesTest {
    public static void main(String args[]) {
        Properties capitals = new Properties();
        Set states;
        String str;
        capitals.setProperty("Illinois", "Springfield");
        capitals.put("Missouri", "Jefferson City");

        long startTime = System.currentTimeMillis();
       states = capitals.keySet(); // get set-view of keys
        Iterator itr = states.iterator();
        while(itr.hasNext()) {
            str=(String) itr.next();
            System.out.println("The capital of " +str + " is " + capitals.getProperty(str) + ".");
        }
        long endTime = System.currentTimeMillis();
        System.out.println("运行时间:" + (endTime - startTime) + "ms");
        
        str = capitals.getProperty("Florida", "Not Found");
        System.out.println("The capital of Florida is "+ str + ".");
    }
}

因为继承了Hashtable可以用 put种putAll方法可应用于Properties对象,强烈不鼓励使用它们,因为Hashtable它们允许调用者插入其键或值不是String
如果键或值不是String是非String那么Properties对象在调用方法时就会出错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值