如果您的输入文件每行包含一个颜色对,则使用BufferedReader(请参阅BufferedReader.readLine())逐行读取文件然后拆分每一行以提取颜色名称和十六进制值会更容易。
Reader reader = new BufferedReader(new FileReader("input.txt"));
String line;
while((line = reader.readLine()) != null) {
// Split line
// Add color name and hex value to your data structure
}
有很多方法可以分割这样一条线。例如。你可以使用String.split()。
或者你可以通过String.indexOf()搜索空格字符位置,然后提取String.substring()的部分。
对于您的数据模型,也有很多选择。 HashMap 可能是个好主意,因为您始终拥有映射到十六进制值的颜色名称。您确定每个颜色名称在您的文件中都是唯一的吗?
但仅使用HashMap 您将丢失原始订单。更合适的数据结构是LinkedHashMap,它在内部使用双向链表来保留插入顺序。
所以有了LinkedHashMap,你就完成了你的第一个用例(原始订单)。遍历LinkedHashMap 就完成了。
迭代Map 的最佳方法是使用entrySet():
for(Entry<String, String> colorPair : map.entrySet()) {
String colorName = colorPair.getKey();
String colorHex = colorPair.getValue();
// Process colorName and colorHex ...
}
对于字母顺序,您可以获取地图keySet() 的所有条目并将它们放入ArrayList 并使用Collections.sort()。然后遍历ArrayList 并通过映射检索十六进制值。听起来有点复杂。但这是可行的。
但困难的部分来自自然颜色顺序(您的意思是按十六进制值排序,对吗?)。因此,您可以通过Map.values() 获取所有颜色的十六进制值,对它们进行排序并...
您不能反过来使用地图。在这里,您想通过十六进制值检索颜色名称,但颜色名称是地图的键。我假设您不想使用第三方库,并且 Java 的标准库中没有双向映射。
当然你可以使用第二张还原地图,但也许我们应该重新考虑一开始的选择,使用地图作为数据模型。
更简单的方法是创建自己的ColorPair 类,然后使用简单的ArrayList 来保存所有颜色对。插入所有颜色对后,列表具有原始顺序。然后您可以使用Collections.sort() 按颜色名称排序,然后按颜色十六进制值排序。
这将是一个简单的ColorPair 实现:
class ColorPair {
private String name;
private String hex;
public ColorPair(String name, String hex) {
this.name = name;
this.hex = hex;
}
public String getName() {
return name;
}
public String getHex() {
return hex;
}
@Override
public String toString() {
return String.format("[ColorPair name=%s, hex=%s]", name, hex);
}
}
您可以在此处实现Comparable 接口并为您的ColorPair 实例定义默认顺序。但是您需要两个不同的订单,因此无论如何您都必须使用 Comparator 类来处理Collections.sort()。 (您可以将其作为练习来尝试。)
因此,假设您有一个方法 parseColorPair() 获取输入文件的一行并返回一个 ColorPair 实例,您可以像这样读取文件:
Reader reader = new BufferedReader(new FileReader("input.txt"));
String line;
List<ColorPair> colorPairs = new ArrayList<ColorPair>();
while((line = reader.readLine()) != null) {
colorPairs.add(parseColorPair(line));
}
// Output in original order
for(ColorPair colorPair: colorPairs) {
System.out.println(colorPair); // using our custom ColorPair.toString() method
}
要按颜色名称对列表进行排序,您必须实现接口Comparator<ColorPair>。
Collections.sort(colorPairs, new Comparator<ColorPair>() {
public int compare(ColorPair colorPair1, ColorPair colorPair2) {
// Compare the names of colorPair1 and colorPair2 and return
// a negative value, if colorPair1.getName() is lesser than colorPair2's name.
// return a positive value (greater than 0) if the first name is greater than
// the second name and return 0 if the names are equal.
// Hint: Look for String.compare()
}
});
你不能在这里使用没有Comparator 的Collections.sort() 版本,因为你的ColorPair 类没有实现Comparable<ColorPair> 接口。
按十六进制值排序类似。 Comparator 实现的不同之处仅在于比较十六进制值而不是名称。
因此,使用正确的数据结构可以直接实现用例,但为给定问题找到正确的数据结构并非易事。