Java 集合遗留类
原文:https://www.studytonight.com/java/legacy-classes-and-interface.php
早期版本的 java 不包括 Collections 框架。它只定义了几个提供存储对象方法的类和接口。当集合框架被添加到 J2SE 1.2 中时,原始的类被重新设计以支持集合接口。这些类也被称为遗留类。JDK 5 重新设计了所有遗留类和接口,以支持泛型。一般来说,遗留类是受支持的,因为仍然有一些代码使用它们。
以下是由 java.util 包定义的遗留类
- 词典
Hashtable
- 性能
- 堆
- 矢量
只有一个遗留接口叫做枚举
注意:所有的遗留类都是同步的
枚举接口
- 枚举接口定义通过对象集合进行枚举(一次获取一个)的方法。
- 该接口被迭代器接口取代。
- 但是有些遗留类如向量、属性定义了几种使用枚举接口的方法。
- 它规定了以下两种方法
boolean hasMoreElements() *//It returns true while there are still more elements to extract,
and returns false when all the elements have been enumerated.*
Object nextElement() *//It returns the next object in the enumeration i.e. each call to nextElement() method
obtains the next object in the enumeration. It throws NoSuchElementException when the
enumeration is complete.*
向量类
- Vector 类似于
ArrayList
,代表一个动态数组。 - 向量和
ArrayList
有两个区别。首先,Vector 是同步的,而 ArrayList 不是,其次,它包含许多不属于 Collections Framework 的遗留方法。 - 随着 JDK 5 的发布,Vector 也实现了 Iterable。这意味着 Vector 与集合完全兼容,并且 Vector 可以通过 for-each 循环迭代其内容。
向量类有以下四个构造器
Vector() *//This creates a default vector, which has an initial size of 10.* Vector(int size) *//This creates a vector whose initial capacity is specified by size.* Vector(int size, int incr) *//This creates a vector whose initial capacity is specified by size and whose increment is specified by incr. The increment specifies the number of elements to allocate each time when a vector is resized for addition of objects.* Vector(Collection c) *//This creates a vector that contains the elements of collection c.*
Vector 定义了几种遗留方法。让我们看看由 Vector 类定义的一些重要的遗留方法。
| 方法 | 描述 | | 空添加元素(E 元素) | 向矢量中添加元素 | | e 元素(int index) | 返回指定索引处的元素 | | 枚举元素() | 返回向量中元素的枚举 | | 和 firstElement() | 返回向量中的第一个元素 | | 和 lastellemon _) | 返回向量中的最后一个元素 | | void removeAllElements() | 移除向量的所有元素 |
向量的例子
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Vector<Integer> ve = new Vector<Integer>();
ve.add(10);
ve.add(20);
ve.add(30);
ve.add(40);
ve.add(50);
ve.add(60);
Enumeration<Integer> en = ve.elements();
while(en.hasMoreElements())
{
System.out.println(en.nextElement());
}
}
}
10 20 30 40 50 60
Hashtable
类
- 像 HashMap 一样,Hashtable 也存储键/值对。然而键和值都不能为空。
- HashMap 和 Hashtable 还有一个区别就是 Hashtable 是同步的,而 HashMap 不是。
Hashtable
有以下四个构造器Hashtable() *//This is the default constructor. The default size is 11.* Hashtable(int size) *//This creates a hash table that has an initial size specified by size.* Hashtable(int size, float fillratio) *//This creates a hash table that has an initial size specified by size and a fill ratio specified by fillRatio. This ratio must be between 0.0 and 1.0, and it determines how full the hash table can be before it is resized upward. Specifically, when the number of elements is greater than the capacity of the hash table multiplied by its fill ratio, the hash table is expanded. If you do not specify a fill ratio, then 0.75 is used.* Hashtable(Map< ? extends K, ? extends V> m) *//This creates a hash table that is initialized with the elements in m. The capacity of the hash table is set to twice the number of elements in m. The default load factor of 0.75 is used.*
Hashtable
示例
import java.util.*;
class HashTableDemo
{
public static void main(String args[])
{
Hashtable<String,Integer> ht = new Hashtable<String,Integer>();
ht.put("a",new Integer(100));
ht.put("b",new Integer(200));
ht.put("c",new Integer(300));
ht.put("d",new Integer(400));
Set st = ht.entrySet();
Iterator itr=st.iterator();
while(itr.hasNext())
{
Map.Entry m=(Map.Entry)itr.next();
System.out.println(itr.getKey()+" "+itr.getValue());
}
}
}
a 100 b 200 c 300 d 400
Hashtable
和HashMap
的区别
| Hashtable
| HashMap
|
| Hashtable
类是同步的。 | HashMap
未同步。 |
| 由于线程安全,Hashtable
比HashMap
慢 | HashMap 的工作速度更快。 |
| 键和值都不能为空 | 键和值都可以为空 |
| 随着时间的推移,表格的顺序保持不变。 | 不能保证映射的顺序随着时间的推移保持不变。 |
属性类
- 属性类扩展了
Hashtable
类。 - 用于维护键和值均为字符串的值列表
属性类定义两个构造器
Properties() *//This creates a Properties object that has no default values* Properties(Properties propdefault) *//This creates an object that uses propdefault for its default values.*
属性相对于
Hashtable
的一个优点是,我们可以指定一个默认属性,当没有值与某个键相关联时,该属性会很有用。
注意:在这两种情况下,属性列表都为空
- 在属性类中,您可以指定一个默认属性,如果没有值与某个键相关联,将返回该属性。
属性类示例
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Properties pr = new Properties();
pr.put("Java", "James Ghosling");
pr.put("C++", "Bjarne Stroustrup");
pr.put("C", "Dennis Ritchie");
pr.put("C#", "Microsoft Inc.");
Set< ?> creator = pr.keySet();
for(Object ob: creator)
{
System.out.println(ob+" was created by "+ pr.getProperty((String)ob) );
}
}
}
Java 是詹姆斯·戈林创造的 C++是比雅尼·斯特劳斯特鲁普创造的 C 是丹尼斯·里奇创造的 C#是微软公司创造的
栈类
- 栈类扩展了向量。
- 对于栈元素,它遵循后进先出原则。
它只定义了一个默认构造器
Stack() //This creates an empty stack
如果要将对象放在栈顶部,请调用 push()方法。如果要移除并返回顶部元素,请调用 pop()方法。如果在调用栈为空时调用 pop()方法,将引发 EmptyStackException。
您可以使用 peek()方法返回,但不能移除顶部对象。如果栈上没有任何内容,则 empty()方法返回 true。search()方法确定一个对象是否存在于栈中,并返回将它带到栈顶部所需的弹出次数。
栈示例
import java.util.*;
class StackDemo {
public static void main(String args[]) {
Stack st = new Stack();
st.push(11);
st.push(22);
st.push(33);
st.push(44);
st.push(55);
Enumeration e1 = st.elements();
while(e1.hasMoreElements())
System.out.print(e1.nextElement()+" ");
st.pop();
st.pop();
System.out.println("\nAfter popping out two elements");
Enumeration e2 = st.elements();
while(e2.hasMoreElements())
System.out.print(e2.nextElement()+" ");
}
}
11 22 33 44 55 弹出两个元素后 11 22 33
词典类
- 字典是抽象类。
- 它代表一个键/值对,操作非常像 Map。
- 虽然字典目前没有被弃用,但它被归类为过时的,因为它完全被映射类取代了。