Java 将哈希映射的值分类到ArrayList

Java 将哈希映射的值分类到ArrayList,java,hashmap,Java,Hashmap,假设我有一个HashMap,字符串作为键,整数作为值 ("a",10) ("b",2) ("c",9) ("d",34) ("e",12) 我想将值大于10的键放入ArrayList。所以结果是 ("d","e") 因为它们的值大于10 我已经研究过这一点以及我发现的所有方法,将HashMap更改为TreeMap或其他内容,但这是为了做家庭作业,所以我无法更改HashMap,你不能比遍历所有元素做得更好。因此,解决方案非常简单: List<String> res = new Ar

假设我有一个HashMap,字符串作为键,整数作为值

("a",10)
("b",2)
("c",9)
("d",34)
("e",12)
我想将值大于10的键放入ArrayList。所以结果是

("d","e")
因为它们的值大于10


我已经研究过这一点以及我发现的所有方法,将HashMap更改为TreeMap或其他内容,但这是为了做家庭作业,所以我无法更改HashMap,你不能比遍历所有元素做得更好。因此,解决方案非常简单:

List<String> res = new ArrayList<String>();

for (String crtKey : map.keySet()) {
    Integer value = map.get(crtKey);

    if ( value != null && value > 10) 
        res.add(crtKey);
    }
}
List<String> res = new ArrayList<String>();

for (String crtKey : map.keySet()) {

    Integer value = map.get(crtKey);

    if ( value != null && value > 10) 
        res.add(crtKey);
    }
}
List res=new ArrayList();
for(字符串crtKey:map.keySet()){
整数值=map.get(crtKey);
如果(值!=null&&value>10)
res.add(crtKey);
}
}

迭代所有元素是最好的选择。因此,解决方案非常简单:

List<String> res = new ArrayList<String>();

for (String crtKey : map.keySet()) {
    Integer value = map.get(crtKey);

    if ( value != null && value > 10) 
        res.add(crtKey);
    }
}
List<String> res = new ArrayList<String>();

for (String crtKey : map.keySet()) {

    Integer value = map.get(crtKey);

    if ( value != null && value > 10) 
        res.add(crtKey);
    }
}
List res=new ArrayList();
for(字符串crtKey:map.keySet()){
整数值=map.get(crtKey);
如果(值!=null&&value>10)
res.add(crtKey);
}
}

好吧,既然你说这是一个家庭作业,我就不讲代码了,但我会告诉你基本的想法

只要在hashmap中循环,获取值并将其存储在一个列表中(如果值大于10)。这是最好的办法,也是最简单的解决办法

因此伪代码将类似于:

list = []
for each key in hashmap
  if hashmap(key) > 10
    list.append(key)

好吧,既然你说这是一个家庭作业,我不会给出代码,但是,我会给你一个基本的想法

只要在hashmap中循环,获取值并将其存储在一个列表中(如果值大于10)。这是最好的办法,也是最简单的解决办法

因此伪代码将类似于:

list = []
for each key in hashmap
  if hashmap(key) > 10
    list.append(key)
试试这个:

ArrayList<String> mylist = new ArrayList<String>();

for (String key : map.keySet()) {
    int value = map.get(key);
    if (key > 10) {
        mylist.add(key);
    }
}
ArrayList mylist=new ArrayList();
for(字符串键:map.keySet()){
int value=map.get(键);
如果(键>10){
mylist.add(键);
}
}
试试这个:

ArrayList<String> mylist = new ArrayList<String>();

for (String key : map.keySet()) {
    int value = map.get(key);
    if (key > 10) {
        mylist.add(key);
    }
}
ArrayList mylist=new ArrayList();
for(字符串键:map.keySet()){
int value=map.get(键);
如果(键>10){
mylist.add(键);
}
}

对于Java 8,您可以使用以下构造:

List<String> filteredKeys = map.entrySet().stream()
                               .filter(e -> e.getValue() > 10) //keep values > 10
                               .map(Entry::getKey) //we only want the keys
                               .collect(toList()); //put them in a list
List filteredKeys=map.entrySet().stream()
.filter(e->e.getValue()>10)//保留值>10
.map(Entry::getKey)//我们只需要这些键
.collect(toList())//把它们列在一个列表中

对于Java 8,您可以使用以下构造:

List<String> filteredKeys = map.entrySet().stream()
                               .filter(e -> e.getValue() > 10) //keep values > 10
                               .map(Entry::getKey) //we only want the keys
                               .collect(toList()); //put them in a list
List filteredKeys=map.entrySet().stream()
.filter(e->e.getValue()>10)//保留值>10
.map(Entry::getKey)//我们只需要这些键
.collect(toList())//把它们列在一个列表中
List res=new ArrayList();
for(字符串crtKey:map.keySet()){
整数值=map.get(crtKey);
如果(值!=null&&value>10)
res.add(crtKey);
}
}
List res=new ArrayList();
for(字符串crtKey:map.keySet()){
整数值=map.get(crtKey);
如果(值!=null&&value>10)
res.add(crtKey);
}
}
Map Map=newhashmap();
地图.put(a,10);;
地图放置(“b”,2);
地图.put(c,9);;
地图.put(d,34);;
地图.put(e,12);;
对于(Map.Entry:Map.entrySet()){
if(entry.getValue()>10){
System.out.println(entry.getKey());
}
}
Map Map=newhashmap();
地图.put(a,10);;
地图放置(“b”,2);
地图.put(c,9);;
地图.put(d,34);;
地图.put(e,12);;
对于(Map.Entry:Map.entrySet()){
if(entry.getValue()>10){
System.out.println(entry.getKey());
}
}

而仅仅迭代HashMap是不可行的?您只需要在映射上迭代,例如下面的循环:
for(map.Entry条目:map.entrySet())
。而仅仅迭代HashMap是不可行的?您只需要在映射上迭代,例如下面的循环:
for(Map.Entry:Map.entrySet())
。他不想检查键是否大于10,我想你搞错了。他不想检查键是否大于10,我想你搞错了