![txt文件示例] [1] http://dox.bg/files/dw?a=f49baaafee
![实际数字是多少] [2] http://dox.bg/files/dw?a=a911da0422
我收到了一个文本文件iot.txt,并在其中添加了红色并将其数据存储在Map<String, HashMap<String, Integer>>
。如您在文本文件中看到的,我们有一个精确的句号“ 2004-11-28-2004-12-04”,然后是语言(Java,Python等),最后是一个整数(对此语言感兴趣),这些语言的价值。仅如何循环浏览年份,更不用说具体的星期了。例如,迭代到2004年并获得最少的兴趣。迭代到2006年。另外,我有2004-2015年,所以每年都有快速方法循环吗?我想做这样的事情:
Map<String, HashMap<String, Integer>> everything = new HashMap<>();
HashMap<String, Integer> lang = new HashMap<>();
for(String yr: iot.keySet())
{
lang.put(yr, iot.get(yr).get("JavaScript").intValue());
everything.put(yr, lang);
for(String yr04 : iot.get("2004").keySet()) //this is where I have prob
{
int minValueInMap=(Collections.min(everything.values())); // there is smth wrong with "min"
if (entry.getValue()==minValueInMap) //entry cannot be resolved??
{
System.out.println(entry.getKey()); //entry cannot be resolved??
}
}
但是,由于我的键排序为“ 2004-11-28-2004-12-04”,因此无法完成。
我建议使用更好的数据结构来表示您的需求。但是,如果不能这样扩展HashMap,则如下所示:
public class MyHashMap<V> extends HashMap<String,V> {
public Set<String> keysStartingWith(String s) {
Set<String> result = new HashSet<String>();
for(String key : keySet()) {
if(key.startsWith(s)) {
result.add(key);
}
}
return result;
}
}
除了使用“地图”之外,您还可以使用:
MyHashMap<HashMap<String, Integer>> myHashMap = new MyHashMap<>();
HashMap<String, Integer> data;
for(String yearRangeKey : myHashMap.keysStartingWith("2014") {
data = myHashMap.get(yearRangeKey);
...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句