Java 实用程序包提供的数据结构非常强大,并且执行广泛的功能。这些数据结构由以下接口和类组成 

  • Enumeration
  • BitSet
  • Vector
  • Stack
  • Dictionary
  • Hashtable
  • Properties

所有这些类现在都是遗留类,Java-2 引入了新的类称为集合框架的框架,将在下一章中讨论 

枚举

枚举接口本身并不是一种数据结构,但它在其他数据结构的上下文中非常重要数据结构。 Enumeration 接口定义了一种从数据结构中检索连续元素的方法。

例如,Enumeration 定义了一个名为 nextElement 的方法,用于获取包含多个元素的数据结构中的下一个元素。

示例

以下示例显示向量枚举的用法。

import java.util.Vector;
import java.util.Enumeration;

public class EnumerationTester {

   public static void main(String args[]) {
      Enumeration<String> days;
      Vector<String> dayNames = new Vector<>();
      
      dayNames.add("Sunday");
      dayNames.add("Monday");
      dayNames.add("Tuesday");
      dayNames.add("Wednesday");
      dayNames.add("Thursday");
      dayNames.add("Friday");
      dayNames.add("Saturday");
      days = dayNames.elements();
      
      while (days.hasMoreElements()) {
         System.out.println(days.nextElement()); 
      }
   }
} 

输出

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday 

要了解有关此接口的更多详细信息,查看枚举

BitSet

BitSet 类实现了一组位或标志,可以单独设置和清除。

当您需要跟踪一组布尔值时,此类非常有用;您只需为每个值分配一个位,并根据需要设置或清除它。

示例

以下程序说明了此数据结构支持的几种方法 -

import java.util.BitSet;
public class BitSetDemo {

  public static void main(String args[]) {
      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("Initial pattern in bits1: ");
      System.out.println(bits1);
      System.out.println("\nInitial pattern in bits2: ");
      System.out.println(bits2);

      // 与位
      bits2.and(bits1);
      System.out.println("\nbits2 AND bits1: ");
      System.out.println(bits2);

      // 或位
      bits2.or(bits1);
      System.out.println("\nbits2 OR bits1: ");
      System.out.println(bits2);

      //异或位
      bits2.xor(bits1);
      System.out.println("\nbits2 XOR bits1: ");
      System.out.println(bits2);
   }
} 

输出

Initial pattern in bits1:
{0, 2, 4, 6, 8, 10, 12, 14}

Initial pattern in bits2:
{1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14}

bits2 AND bits1:
{2, 4, 6, 8, 12, 14}

bits2 OR bits1:
{0, 2, 4, 6, 8, 10, 12, 14}

bits2 XOR bits1:
{} 

Vector

Vector 类与传统的 Java 数组类似,不同之处在于它可以根据需要增长以容纳新元素。

与数组一样,可以通过向量的索引来访问 Vector 对象的元素。

使用 Vector 类的好处是,您不必担心将其设置为特定的值。创建时的大小;它会在必要时自动缩小和增长。

示例

以下程序说明了该集合支持的几种方法 -

import java.util.*;
public class VectorDemo {

   public static void main(String args[]) {
      //初始大小为3,增量为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("\nElements in vector:");
      
      while(vEnum.hasMoreElements())
         System.out.print(vEnum.nextElement() + " ");
      System.out.println();
   }
} 

输出

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) 堆栈。

您可以将堆栈从字面上视为垂直堆栈物体;当您添加一个新元素时,它会堆叠在其他元素的顶部。

当您从堆栈中拉出一个元素时,它会从顶部出来。换句话说,添加到堆栈中的最后一个元素是第一个返回的元素。

示例

以下程序说明了此集合支持的几种方法 -

import java.util.*;
public class StackDemo {

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

   static void showpop(Stack 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 st = new Stack();
      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");
      }
   }
} 

输出

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 

字典

Dictionary 类是一个抽象类,它定义用于将键映射到值的数据结构。

当您希望能够通过特定键而不是整数索引访问数据时,这非常有用。

由于 Dictionary 类是抽象的,因此它仅提供键映射数据结构的框架而不是具体的实现。

示例

下面的示例显示了 Java 字典的 keywords() 方法的用法。我们正在使用 Integer、Integer 的 Hashtable 对象创建一个字典实例。然后我们添加了一些元素。使用keys()方法检索枚举,然后迭代枚举以打印字典的键。

package com.yxjc123;

import java.util.Enumeration;
import java.util.Dictionary;
import java.util.Hashtable;

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

      //创建一个新的哈希表
      Dictionary<Integer, Integer> dictionary = new Hashtable<>();

      //添加2个元素
      dictionary.put(1, 1);
      dictionary.put(2, 2);

      Enumeration<Integer> enumeration = dictionary.keys();

      while(enumeration.hasMoreElements()) {
         System.out.println(enumeration.nextElement());
      }
   }
} 

输出

2
1 

哈希表

哈希表类提供了一种基于某些用户定义的键结构来组织数据的方法。

例如,在地址列表哈希表中,您可以基于诸如邮政编码之类的键而不是基于人名。

哈希表中键的具体含义完全取决于哈希表的用途及其包含的数据。

示例

以下示例显示了使用 Java Hashtable contains() 方法来检查 Hashtable 中是否存在值。我们创建了一个 Integer,Integer 的 Hashtable 对象。然后添加一些条目,打印表格并使用 contains() 我们检查表格中的两个值。

package com.yxjc123;

import java.util.Hashtable;

public class HashtableDemo {
   public static void main(String args[]) {
      
      //创建哈希表
      Hashtable<Integer,Integer> hashtable = new Hashtable<>();

      //填充哈希表
      hashtable.put(1, 1);
      hashtable.put(2, 2);
      hashtable.put(3, 3); 

      System.out.println("Initial table elements: " + hashtable);
      System.out.println("Hashtable contains 2 as value: " + hashtable.contains(2));
      System.out.println("Hashtable contains 4 as value: " + hashtable.contains(4));
   }    
} 

输出

Initial table elements: {3=3, 2=2, 1=1}
Hashtable contains 2 as value: true
Hashtable contains 4 as value: false 

属性

Properties 是 Hashtable 的子类。它用于维护值列表,其中键是字符串,值也是字符串。

许多其他 Java 类都使用 Properties 类。例如,它是 System.getProperties( ) 在获取环境值时返回的对象类型。

示例

下面的示例展示了 Java Properties getProperty(String key) 的用法方法根据属性中的键获取值。我们创建了一个 Properties 对象。然后添加一些条目。使用 getProperty() 方法检索并打印一个值。

package com.yxjc123;

import java.util.Properties;

public class PropertiesDemo {
   public static void main(String[] args) {
      Properties properties = new Properties();

      //填充属性对象
      properties.put("1", "yxjc");
      properties.put("2", "123");
      properties.put("3", "is best");

      System.out.println("Properties elements: " + properties);
      System.out.println("Value: " + properties.getProperty("1"));
   }
} 

输出

Properties elements: {1=yxjc, 2=point, 3=is best}
Value: yxjc