Home >Java >javaTutorial >Analyze Java data structure example code
Although the enumeration (Enumeration) interface itself does not belong to a data structure, it is widely used in the category of other data structures. The Enumeration interface defines a way to retrieve consecutive elements from a data structure. For example, the enumeration defines a method called nextElement, which is used to get the next element of a data structure containing multiple elements.
This traditional interface has been replaced by Iterator. Although Enumeration has not been abandoned, it is rarely used in modern code. Nonetheless, it is used in methods defined by traditional classes such as Vector and Properties, in addition to some API classes, and is widely used in applications. The following table summarizes some methods declared by Enumeration:
Serial number | Methods and descriptions |
---|---|
boolean hasMoreElements(), tests whether this enumeration contains more elements | |
Object nextElement(), if this enumeration object has at least If there is an element available, the next element of this enumeration is returned |
Serial number | Method and description |
---|---|
1 | void and(BitSet set), for this target bit set and parameters Bit set performs logical AND operation |
2 | void andNot(BitSet set), clears all bits in this BitSet, and the corresponding bits are already in the specified BitSet. Set |
3 | int cardinality( ), returns the number of digits set to true in this BitSet |
4 | void clear(), set all bits in this BitSet to false |
5 | void clear(int index), set the index at the specified bit is set to false |
6 | void clear(int startIndex, int endIndex), will the specified startIndex (inclusive) to the specified toIndex (exclusive) range bit is set to false |
7 | Object clone( ), copies this BitSet and generates a new BitSet equal to it |
8 | boolean equals(Object bitSet), compare this object with the specified object |
9 | void flip(int index ), sets the bit at the specified index to the complement of its current value |
10 | void flip(int startIndex, int endIndex), sets the specified fromIndex( Each bit in the range inclusive) to the specified toIndex (exclusive) is set to the complement of its current value |
11 | boolean get(int index), Returns the bit value at the specified index |
12 | BitSet get(int startIndex, int endIndex), returns a new BitSet, which is obtained from fromIndex( The bits in the range from (including) to toIndex (excluding) form |
13 | int hashCode( ), returns the hash code value of this bit set |
14 | boolean intersects(BitSet bitSet), if there is a bit set to true in the specified BitSet, and it is also set to true in this BitSet, then return true |
15 | boolean isEmpty( ), if this BitSet does not contain any bits set to true, returns true |
16 | int length(), returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus 1 |
17 | int nextClearBit(int startIndex), returns the index of the first bit set to false, which occurs at or after the specified start index |
18 | int nextSetBit(int startIndex), returns the index of the first bit set to true, which occurs at or after the specified start index |
19 | void or(BitSet bitSet), perform logical OR operation on this bit set and bit set parameters |
20 | void set(int index), will specify the index Set the bit at the specified index to true |
21 | void set(int index, boolean v), set the bit at the specified index to the specified value |
22 | void set(int startIndex, int endIndex), sets the bits in the range from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to true |
23 | void set(int startIndex, int endIndex, boolean v), sets the bits in the range from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to The specified value |
24 | int size( ), returns the number of bits of space actually used when this BitSet represents the bit value |
25 | String toString( ), returns the string representation of this bit set |
26 | void xor(BitSet bitSet), for This bitset and bitset parameters perform a logical exclusive OR operation |
实例:
public class Test { public static void main(String args[]) throws IOException { BitSet bits1 = new BitSet(16); BitSet bits2 = new BitSet(16); // 设置一些位 for(int i=0; i<16; i++) { if((i%2) == 0) bits1.set(i); if((i%5) != 0) bits2.set(i); } System.out.println("位集合1初始模式: "); System.out.println(bits1); System.out.println("\n位集合2初始模式: "); System.out.println(bits2); // 对此目标位 set 和参数位 set 执行逻辑与操作 bits2.and(bits1); System.out.println("\n位集合2 与 位集合1 执行逻辑与操作 "); System.out.println(bits2); // 对此位 set 和位 set 参数执行逻辑或操作 bits2.or(bits1); System.out.println("\n位集合2 与 位集合1 执行逻辑或操作: "); System.out.println(bits2); // 对此位 set 和位 set 参数执行逻辑异或操作 bits2.xor(bits1); System.out.println("\n位集合2 与 位集合1 执行逻辑异或操作 "); System.out.println(bits2); } } // 程序编译运行结果如下: // 位集合1初始模式: // {0, 2, 4, 6, 8, 10, 12, 14} // 位集合2初始模式: // {1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14} // 位集合2 与 位集合1 执行逻辑与操作 // {2, 4, 6, 8, 12, 14} // 位集合2 与 位集合1 执行逻辑或操作: // {0, 2, 4, 6, 8, 10, 12, 14} // 位集合2 与 位集合1 执行逻辑异或操作 // {}
向量(Vector)类和传统数组非常相似,但是Vector的大小能根据需要动态的变化。和数组一样,Vector对象的元素也能通过索引访问。使用Vector类最主要的好处就是在创建对象的时候不必给对象指定大小,它的大小会根据需要动态的变化。
Vector 与 ArrayList的区别:
Vector 是同步访问的,所以线程就会安全,但是同时也会带来弊端就是效率就会降低,但 Arraylist 恰恰相反,这也就导致Arraylist的效率比 Vector 高。
在进行扩容时,Vector 会增长为原来数组长的一倍,而 Arraylist 只会增长为原来的一半,所以Arraylist节约内存空间。
Vector 包含了许多传统的方法,这些方法不属于集合框架。
Vector 类支持 4 种构造方法:
// 第一种构造方法创建一个默认的向量,默认大小为 10 Vector() // 第二种构造方法创建指定大小的向量 Vector(int size) // 第三种构造方法创建指定大小的向量,并且增量用 incr 指定。增量表示向量每次增加的元素数目 Vector(int size,int incr) // 第四种构造方法创建一个包含集合 c 元素的向量 Vector(Collection c)
除了从父类继承的方法外 Vector 还定义了以下方法:
Serial number | Method and description |
---|---|
1 | void add(int index, Object element) , insert the specified element at the specified position of this vector |
2 | boolean add(Object o) , add the specified element to the end of this vector |
3 | boolean addAll(Collection c), adds all elements in the specified Collection to the end of this vector, adding these elements in the order returned by the iterator of the specified collection |
4 | boolean addAll(int index, Collection c), inserts all elements in the specified Collection into this vector at the specified position |
5 | void addElement(Object obj) , adds the specified component to the end of this vector, increasing its size by 1 |
6 | int capacity(), returns the current capacity of this vector |
7 | void clear(), removes all elements from this vector |
8 | Object clone(), returns a copy of the vector |
9 | boolean contains(Object elem) , returns true if this vector contains the specified element |
10 | boolean containsAll(Collection c) , returns true if this vector contains all elements in the specified Collection, then Returns true |
11 | void copyInto(Object[] anArray), copies the components of this vector to the specified array |
12 | Object elementAt(int index) , returns the component at the specified index |
13 | Enumeration elements() , returns this vector Enumeration of components |
14 | void ensureCapacity(int minCapacity) , increase the capacity of this vector (if necessary) to ensure that it can hold at least the minimum capacity The number of components specified by the parameter |
15 | boolean equals(Object o), compares the specified object for equality with this vector |
16 | Object firstElement() , returns the first component of this vector (the item at index 0) |
17 | Object get(int index), returns the element at the specified position in the vector |
int hashCode(), returns the hash code value of this vector | |
int indexOf(Object elem) , returns the index of the first occurrence of the specified element in this vector, if this vector does not contain the element, returns -1 | |
int indexOf(Object elem, int index), returns the index of the specified element that appears for the first time in this vector, searches forward from index, if not If the element is found, -1 | |
void insertElementAt(Object obj, int index) is returned, inserting the specified object as a component in this vector into the specified At index | |
boolean isEmpty(), tests whether this vector does not contain components | |
Object lastElement(), returns the last component of this vector | |
int lastIndexOf(Object elem), returns the last specified element that appears in this vector the index of The index of the specified element that appears, search in reverse direction from index, if the element is not found, return -1 | |
Object remove(int index), move Remove the element at the specified position in this vector | |
boolean remove(Object o) , remove the first occurrence of the specified element in this vector, if the vector If the element is not included, the element remains unchanged | |
boolean removeAll(Collection c), removes all elements contained in the specified Collection from this vector | |
void removeAllElements(), removes all components from this vector and sets its size to zero | |
boolean removeElement(Object obj) , removes the first (lowest index) occurrence of the variable from this vector | |
void removeElementAt(int index) , delete the component at the specified index | |
protected void removeRange(int fromIndex, int toIndex) , remove its index from this vector All elements located between fromIndex (inclusive) and toIndex (exclusive) | |
boolean retainAll(Collection c) , in this vector only those contained in Specify the element in the Collection | |
Object set(int index, Object element) and replace the element at the specified position in this vector with the specified element | |
void setElementAt(Object obj, int index), sets the component at the specified index of this vector to the specified object | |
void setSize(int newSize) , sets the size of this vector | |
int size() , returns the groups in this vector Number of items | |
List subList(int fromIndex, int toIndex), returns a partial view of this List, the elements range from fromIndex (inclusive) to toIndex (exclusive) ) | |
Object[] toArray(), returns an array containing all the elements in this vector stored in the proper order | |
40 | Object[] toArray(Object[] a) ,返回一个数组,包含此向量中以恰当顺序存放的所有元素;返回数组的运行时类型为指定数组的类型 |
41 | String toString() ,返回此向量的字符串表示形式,其中包含每个元素的 String 表示形式 |
42 | void trimToSize() ,对此向量的容量进行微调,使其等于向量的当前大小 |
实例:
public class Test { public static void main(String args[]) throws IOException { // 初始大小为3,增量为2 Vector v = new Vector(3, 2); System.out.println("初始大小: " + v.size()); System.out.println("初始容量: " + v.capacity()); v.addElement(new Integer(1)); v.addElement(new Double(5.45)); v.addElement(new Double(6.08)); v.addElement(new Integer(7)); System.out.println("四次添加后的容量: " + v.capacity()); v.addElement(new Float(9.4)); System.out.println("当前容量: " + v.capacity()); v.addElement(new Integer(10)); System.out.println("当前容量: " + v.capacity()); System.out.println("第一元素: " + (Integer)v.firstElement()); System.out.println("最后一个元素: " + (Integer)v.lastElement()); v.addElement(new Integer(3)); // 向量包含3 if(v.contains(new Integer(3))) { System.out.println("向量包含 3"); } // 列举向量中的元素 Enumeration vEnum = v.elements(); System.out.println("\n向量中的元素:"); while(vEnum.hasMoreElements()) { System.out.print(vEnum.nextElement() + " "); } System.out.println(); } } // 程序编译运行结果如下: // 初始大小: 0 // 初始容量: 3 // 四次添加后的容量: 5 // 当前容量: 5 // 当前容量: 7 // 第一元素: 1 // 最后一个元素: 10 // 向量包含 3 // 向量中的元素: // 1 5.45 6.08 7 9.4 10 3
栈(Stack)实现了一个后进先出(LIFO)的数据结构。你可以把栈理解为对象的垂直分布的栈,当你添加一个新元素时,就将新元素放在其他元素的顶部。当你从栈中取元素的时候,就从栈顶取一个元素。换句话说,最后进栈的元素最先被取出。
栈是Vector的一个子类,栈只定义了默认构造函数,用来创建一个空栈。栈除了包括由Vector定义的所有方法,也定义了自己的一些方法:
序号 | 方法及说明 |
---|---|
1 | boolean empty() ,测试栈是否为空 |
2 | Object peek( ) ,查看栈顶部的对象,但不从栈中移除它 |
3 | Object pop( ) ,移除栈顶部的对象,并作为此函数的值返回该对象 |
4 | Object push(Object element) ,把项压入堆栈顶部 |
5 | int search(Object element) ,返回对象在堆栈中的位置,以 1 为基数 |
字典(Dictionary) 类是一个抽象类,它定义了键映射到值的数据结构。当你想要通过特定的键而不是整数索引来访问数据的时候,这时候应该使用Dictionary。由于Dictionary类是抽象类,所以它只提供了键映射到值的数据结构,而没有提供特定的实现。
Dictionary类已经过时了。在实际开发中,你可以实现 Map 接口来获取键/值的存储功能。
Hashtable类提供了一种在用户定义键结构的基础上来组织数据的手段。例如,在地址列表的哈希表中,你可以根据邮政编码作为键来存储和排序数据,而不是通过人名。哈希表键的具体含义完全取决于哈希表的使用情景和它包含的数据。
Hashtable是原始的java.util的一部分, 是一个Dictionary具体的实现 。然而,Java 2 重构的Hashtable实现了 Map 接口,因此,Hashtable 现在集成到了集合框架中。它和HashMap类很相似,但是它支持同步。
像HashMap一样,Hashtable在哈希表中存储键/值对。当使用一个哈希表,要指定用作键的对象,以及要链接到该键的值。然后,该键经过哈希处理,所得到的散列码被用作存储在该表中值的索引。
Hashtable定义了四个构造方法:
// 默认构造方法 public Hashtable() // 创建指定大小的哈希表 public Hashtable(int initialCapacity) // 创建了一个指定大小的哈希表,并且通过fillRatio指定填充比例 // 填充比例必须介于0.0和1.0之间,它决定了哈希表在重新调整大小之前的充满程度 public Hashtable(int initialCapacity, float loadFactor) // 创建了一个以 t 中元素为初始化元素的哈希表,哈希表的容量被设置为 t 的两倍 public Hashtable(Map<? extends K, ? extends V> t)
Hashtable中除了从Map接口中定义的方法外,还定义了以下方法:
序号 | 方法及说明 |
---|---|
1 | void clear( ) ,将此哈希表清空,使其不包含任何键 |
2 | Object clone( ) ,创建此哈希表的浅表副本 |
3 | boolean contains(Object value) ,测试此映射表中是否存在与指定值关联的键 |
4 | boolean containsKey(Object key) ,测试指定对象是否为此哈希表中的键 |
5 | boolean containsValue(Object value) ,如果此 Hashtable 将一个或多个键映射到此值,则返回 true |
6 | Enumeration elements( ) ,返回此哈希表中的值的枚举 |
7 | Object get(Object key) ,返回指定键所映射到的值,如果此映射不包含此键的映射,则返回 null. 更确切地讲,如果此映射包含满足 (key.equals(k)) 的从键 k 到值 v 的映射,则此方法返回 v;否则,返回 null |
8 | boolean isEmpty( ) ,测试此哈希表是否没有键映射到值 |
9 | Enumeration keys( ) ,返回此哈希表中的键的枚举 |
10 | Object put(Object key, Object value) ,将指定 key 映射到此哈希表中的指定 value |
11 | void rehash( ) ,增加此哈希表的容量并在内部对其进行重组,以便更有效地容纳和访问其元素 |
12 | Object remove(Object key) ,从哈希表中移除该键及其相应的值 |
13 | int size( ) ,返回此哈希表中的键的数量 |
14 | String toString( ) ,返回此 Hashtable 对象的字符串表示形式,其形式为 ASCII 字符 ", " (逗号加空格)分隔开的、括在括号中的一组条目 |
Properties 继承于 Hashtable。Properties 类表示了一个持久的属性集。属性列表中每个键及其对应值都是一个字符串。Properties 类被许多Java类使用。例如,在获取环境变量时它就作为System.getProperties()方法的返回值。
Properties 定义如下实例变量.这个变量持有一个 Properties 对象相关的默认属性列表:
protected Properties defaults;
Properties类定义了两个构造方法:
// 第一个构造方法没有默认值 public Properties() // 第二个构造方法使用propDefault 作为默认值 public Properties(Properties defaults)
除了从 Hashtable 中所定义的方法,Properties 还定义了以下方法:
序号 | 方法及说明 |
---|---|
1 | String getProperty(String key), 用指定的键在此属性列表中搜索属性 |
2 | String getProperty(String key, String defaultProperty),用指定的键在属性列表中搜索属性 |
3 | void list(PrintStream streamOut),将属性列表输出到指定的输出流 |
4 | void list(PrintWriter streamOut),将属性列表输出到指定的输出流 |
5 | void load(InputStream streamIn) throws IOException,从输入流中读取属性列表(键和元素对) |
6 | Enumeration propertyNames( ),按简单的面向行的格式从输入字符流中读取属性列表(键和元素对) |
7 | Object setProperty(String key, String value),调用 Hashtable 的方法 put |
8 | void store(OutputStream streamOut, String description),以适合使用 load(InputStream)方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流 |
The above is the detailed content of Analyze Java data structure example code. For more information, please follow other related articles on the PHP Chinese website!