【发布时间】:2017-02-27 21:29:42
【问题描述】:
我有一个 .txt:
- 行:x、慕尼黑、c、e、f
- line: y, Berlin, ad, uf
- 行:z, Hamburg, of, bf
所有单词都用','分隔。
此 txt 中一行的前 2 个字符串用于构造函数。 3. 及以下字符串进入列表。 例如,关键是:构造函数形式的 a b 该值是一个 ArrayList,其中包含:c、e、f。所以 3 个元素。
String filePath = "....txt";
List<String> itemlist = new ArrayList<String>();
HashMap<Corporation, List> corp = new HashMap<Corporation, List>();
String line;
BufferedReader reader = new BufferedReader(new FileReader(filePath));
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",", 3);
if (parts.length >= 3) {
String name = parts[0]; // key=name, value= address
String address = parts[1];
String info = parts[2];
itemlist = new ArrayList<String>(Arrays.asList(info.split(",")));
Corporation cor = new Corporation(name, address);
corp.put(cor, itemlist);
}
}
一切正常。 现在我需要在 Map 的一个 ArrayLists 中搜索一个字符串。 例如,我搜索字符串“bf”并打印它找到了字符串以及使用地图右键的位置。例如“bf”:它将打印“bf found at "z, Hamburg"”。
for (Entry<Corporation, List> entry : corp.entrySet()) {
if (entry.getValue().equals("bf")) {
System.out.println("Found at " + );
System.out.println(entry.getKey());
} else {
System.out.println("String not found.");
}
}
我的这部分代码不能按我的意愿工作。我该怎么做?
【问题讨论】:
-
请定义“不起作用”输出/预期
标签: java string collections frameworks