Java - 数据结构


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

  • 枚举
  • 位集
  • 向量
  • 字典
  • 哈希表
  • 特性

所有这些类现在都已成为遗留类,Java-2 引入了一个名为 Collections Framework 的新框架,该框架将在下一章中讨论 -

枚举

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

例如,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 类实现了一组可以单独设置和清除的位或标志。

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

例子

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

import java.util.BitSet; 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 % 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); // AND bits bits2.and(bits1); System.out.println("\nbits2 AND bits1: "); System.out.println(bits2); // OR bits bits2.or(bits1); System.out.println("\nbits2 OR bits1: "); System.out.println(bits2); // XOR bits 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 类与传统的 Java 数组类似,不同之处在于它可以根据需要增长以容纳新元素。

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

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

例子

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

import java.util.*; public class VectorDemo { public static void main(String args[]) { // initial size is 3, increment 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."); // enumerate the elements in the vector. 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 类实现元素的后进先出 (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字典keys()方法的用法。我们正在使用 Integer、Integer 的 Hashtable 对象创建一个字典实例。然后我们添加了一些元素。使用keys()方法检索枚举,然后迭代枚举以打印字典的键。

package com.tutorialspoint; import java.util.Enumeration; import java.util.Dictionary; import java.util.Hashtable; public class DictionaryDemo { public static void main(String[] args) { // create a new hashtable Dictionary<Integer, Integer> dictionary = new Hashtable<>(); // add 2 elements dictionary.put(1, 1); dictionary.put(2, 2); Enumeration<Integer> enumeration = dictionary.keys(); while(enumeration.hasMoreElements()) { System.out.println(enumeration.nextElement()); } } }

输出

2
1

哈希表

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

例如,在地址列表哈希表中,您可以根据邮政编码等键而不是人名来存储和排序数据。

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

例子

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

package com.tutorialspoint; import java.util.Hashtable; public class HashtableDemo { public static void main(String args[]) { // create hash table Hashtable<Integer,Integer> hashtable = new Hashtable<>(); // populate hash table 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 中的键获取值。我们创建了一个 Properties 对象。然后添加一些条目。使用 getProperty() 方法,检索并打印一个值。

package com.tutorialspoint; import java.util.Properties; public class PropertiesDemo { public static void main(String[] args) { Properties properties = new Properties(); //populate properties object properties.put("1", "tutorials"); properties.put("2", "point"); properties.put("3", "is best"); System.out.println("Properties elements: " + properties); System.out.println("Value: " + properties.getProperty("1")); } }

输出

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