Java工具包提供了强大的数据结构。在Java中的数据结构主要包括以下几种接口和类:
枚举(Enumeration)
枚举(Enumeration)接口虽然它本身不属于数据结构,但是它在其他数据结构的范畴里应用很广。枚举(The Enumeration)接口定义了一种从数据结构中取回连续元素的方式。
例如,枚举定义了一个叫nextElement的方法,该方法用来得到一个包含多元素的数据结构的下一个元素。
package pkg2020华南虎;
import java.util.Vector;
import java.util.Enumeration;
/**
*
* @author yl
*/
public class EnumerationDemo {
public static void main(String[] args) {
Enumeration<String> days;
Vector<String> dayNames = new Vector<String>();
dayNames.add("Sunday");
dayNames.add("Monday");
dayNames.add("Tueaday");
dayNames.add("Wednsday");
dayNames.add("Thursday");
dayNames.add("Friday");
dayNames.add("Saturday");
days = dayNames.elements();
while (days.hasMoreElements()) {
System.out.println(days.nextElement());
}
}
}
位集合(BitSet)
位集合类实现了一组可以单独设置和清除的位或者标志。
该类在处理一组布尔值的时候非常有用,你只需要给每个值赋值一“位”,然后对位进行适当的设置或者清除,就可以对布尔值进行操作了。
package pkg2020华南虎;
import java.util.BitSet;
/**
*
* @author yl
*/
public class BitSetDemo {
public static void main(String[] args) {
BitSet bits1 = new BitSet(16);
BitSet bits2 = new BitSet(16);
//set some bits
for (int i = 0; i < 16; i++) {
if ((i % 2) == 0) {
bits1.set(i);
}
if ((i % 2) != 0) {
bits2.set(i);
}
}
System.out.println("Initial pattern in bits1:");
System.out.println(bits1);
System.out.println("Initial pattern in bits2");
System.out.println(bits2);
//AND bits
bits2.and(bits1);
System.out.println("bits2 and bits1 :");
System.out.println(bits2);
//OR bits
bits2.or(bits1);
System.out.println("bits2 or bits1:");
System.out.println(bits2);
//XOR bits
bits2.xor(bits1);
System.out.println("bits2 xor bits1");
System.out.println(bits2);
}
}
/*一个Bitset类创建一种特殊类型的数组来保存位值。Bitset中数组大小会随需要增加。这和位向量(Vector of bits)比较类似*/
向量(Vector)
向量(Vector)类和传统数组非常相似,但是Vector的大小能根据需要动态的变化。
和数组一样,Vector对象的元素也能通过索引访问。
使用Vector类最主要的好处就是在创建对象的时候不必给对象指定大小,它的大小会根据需要动态的变化。
package pkg2020华南虎;
import java.util.*;
/**
*
* @author yl
*/
public class VectorDemo02 {
public static void main(String[] args) {
//initial size is 3,imcrement is 2
Vector v = new Vector(3, 2);
System.out.println("Initial size:" + v.size());
System.out.println("Initial capacity:"
+ v.capacity());
v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
System.out.println("Capacity after four additions:"
+ v.capacity());
v.addElement(new Double(5.45));
System.out.println("Current capacity:"
+ v.capacity());
v.addElement(new Double(6.08));
v.addElement(new Integer(7));
System.out.println("Current capacity:"
+ v.capacity());
v.addElement(new Float(9.4));
v.addElement(new Integer(10));
System.out.println("Current capacity:"
+ v.capacity());
v.addElement(new Integer(11));
v.addElement(new Integer(12));
System.out.println("First element:"
+ (Integer) v.firstElement());
System.out.println("Last element:"
+ (Integer) v.lastElement());
if (v.contains(new Integer(3))) {
System.out.println("Vector contains 3.");
}
Enumeration vEnum = v.elements();
System.out.println("
Elements in vector:");
while (vEnum.hasMoreElements()) {
System.out.println(vEnum.nextElement() + " ");
}
System.out.println();
}
}
结果:
run: Initial size:0 Initial capacity:3 Capacity after four additions:5 Current capacity:5 Current capacity:7 Current capacity:9 First element:1 Last element:12 Vector contains 3. Elements in vector: 1 2 3 4 5.45 6.08 7 9.4 10 11 12
栈(Stack)
栈(Stack)实现了一个后进先出(LIFO)的数据结构。
你可以把栈理解为对象的垂直分布的栈,当你添加一个新元素时,就将新元素放在其他元素的顶部。
当你从栈中获取元素,就从栈顶取一个元素,换句话说,最后进栈的元素最先被取出。
package pkg2020华南虎;
import java.util.*;
/**
*
* @author yl
*/
public class StackDemo {
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.println("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>();
System.out.println("stack:" + st);
showpush(st, 42);
showpush(st, 66);
showpush(st, 99);
showpop(st);
showpop(st);
showpop(st);
try {
showpop(st);
} catch (EmptyStackException e) {
System.out.println("empty stack");
}
}
}
结果:
run: stack:[] push(42) stack:[42] push(66) stack:[42, 66] push(99) stack:[42, 66, 99] pop-> 99 stack:[42, 66] pop-> 66 stack:[42] pop-> 42 stack:[] pop-> empty stack 成功构建 (总时间: 0 秒)
字典(Dictionary)
字典(Dictionary)类是一个抽象类,它定义了键映射到值得数据结构。
当你想要通过特定的键而不是整数索引来访问数据的时候,这时候应该使用DIctionary。
由于Dictionary类是抽象类,所以它只提供了键映射到值得数据结构,而没有提供特定得实现。
Dictionary类已经过时了。在实际开发中,你可以实现Map接口来获取键/值得存储功能。
哈希表(Hashtable)
Hashtable类提供了一种在用户定义键结构的基础上来组织数据的手段。
例如,在地址列表的哈希表中,你可以根据邮政编码作为键来存储和排序数据,而不是通过人名。
哈希表键的具体含义完全取决于哈希表的使用场景和它包含的数据。
package pkg2020华南虎;
import java.util.*;
/**
*
* @author yl
*/
public class HashTableDemo {
public static void main(String[] args) {
//Create a hash map
Hashtable balance = new Hashtable();
Enumeration names;
String str;
double bal;
balance.put("Zara", new Double(3434.34));
balance.put("Mahnaz", new Double(123.22));
balance.put("Ayan", new Double(1378.00));
balance.put("Daisy", new Double(99.22));
balance.put("Qadir", new Double(-19.08));
//Show all balances in hash table.
names = balance.keys();
while (names.hasMoreElements()) {
str = (String) names.nextElement();
System.out.println(str + ":"
+ balance.get(str));
}
System.out.println();
//Deposit 1,000 into Zara's account
bal = ((Double) balance.get("Zara")).doubleValue();
balance.put("Zara", new Double(bal + 1000));
System.out.println("Zara's new balance:" + balance.get("Zara"));
}
}
结果:
run: Qadir:-19.08 Zara:3434.34 Mahnaz:123.22 Daisy:99.22 Ayan:1378.0 Zara's new balance:4434.34 成功构建 (总时间: 0 秒)
属性(Properties)
Properities继承于Hashtable.Properties类表示了一个持久的属性集,属性列表中每个键及其对应值都是一个字符串。
Properties类被许多Java类使用。例如,在获取环境变量时它就作为System.getProperties()方法的返回值。
package pkg2020华南虎;
import java.util.*;
/**
*
* @author yl
*/
public class PropDemo {
public static void main(String[] args) {
Properties capitals = new Properties();
Set states;
String str;
capitals.put("Illinois", "Springfield");
capitals.put("Missouri", "Jefferson City");
capitals.put("Washington", "Olympia");
capitals.put("California", "Sacramento");
capitals.put("Indiana", "Indianapolis");
//Show all states and capitals in hashtable.
states = capitals.keySet();
Iterator itr = states.iterator();
while (itr.hasNext()) {
str = (String) itr.next();
System.out.println("The capital of"
+ str + "is" + capitals.getProperty(str) + "."
);
}
System.out.println();
str = capitals.getProperty("Florida", "Not Found");
System.out.println("The capital of Florida is"
+ str + "."
);
}
}