问题
1.问:你打算写一个使用几个基本集合接口的程序:Set
,List
,Queue
,和Map
。您不确定哪种实现效果最佳,因此您决定使用通用实现,直到更好地了解程序在现实世界中的工作方式。这些是哪些实现?
答:
Set: HashSet
List: ArrayList
Queue: LinkedList
Map: HashMap
2.问题:如果您需要一个提供按值排序的迭代的Set
实现,则应使用哪个类?
答案:TreeSet
保证排序后的集合按元素的升序排列,根据元素的自然顺序或Comparator
提供的元素进行排序。
3.问题:您使用哪个类访问包装器实现?
答:
您使用Collections
类,该类提供可对集合进行操作或返回集合的静态方法。
练习题
1.练习:编写一个程序,该程序将第一个命令行参数指定的文本文件读入List
。然后,程序应从文件中打印随机行,即第二个命令行参数指定的打印行数。编写程序,以便一次分配所有正确大小的集合,而不是在读取文件时逐渐扩展它。提示:要确定文件中的行数,请使用 java.io.File.length
获取文件的大小,然后除以假设的平均线尺寸。
答:
由于我们是随机访问List
,因此我们将使用ArrayList
。我们通过将文件大小除以50来估计行数。然后我们将该数字加倍,因为高估比低估更有效。
import java.util.*;
import java.io.*;
public class FileList {
public static void main(String[] args) {
final int assumedLineLength = 50;
File file = new File(args[0]);
List<String> fileList =
new ArrayList<String>((int)(file.length() / assumedLineLength) * 2);
BufferedReader reader = null;
int lineCount = 0;
try {
reader = new BufferedReader(new FileReader(file));
for (String line = reader.readLine(); line != null;
line = reader.readLine()) {
fileList.add(line);
lineCount++;
}
} catch (IOException e) {
System.err.format("Could not read %s: %s%n", file, e);
System.exit(1);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {}
}
}
int repeats = Integer.parseInt(args[1]);
Random random = new Random();
for (int i = 0; i < repeats; i++) {
System.out.format("%d: %s%n", i,
fileList.get(random.nextInt(lineCount - 1)));
}
}
}
该程序实际上将大部分时间都花在读取文件上,因此预先分配ArrayList
对其性能几乎没有影响。当程序在不干预I / O的情况下重复创建大的ArrayList
对象时,预先指定初始容量可能会很有用。