用于映射的Java 8流<;字符串,Set<;字符串>&燃气轮机;

用于映射的Java 8流<;字符串,Set<;字符串>&燃气轮机;,java,java-8,java-stream,Java,Java 8,Java Stream,我有一个Map myEntry.getValue().stream() .map(setEntry->myCustomMethod(myEntry.getKey(),setEntry,a+b))) .collect(Collectors.toList()); 试试这个: public void myMethod(Map<Integer, Set<Integer>> myMap, String a, String b) { List<Integer&g

我有一个Map。我想根据自定义方法所做的一些修改将其转换为整数列表

现在我正在使用两个for循环,我想知道是否有更好的方法使用java流来实现它

以下是我现有的代码:

public myMethod(Map<Integer, Set<Integer>> myMap, String a, int b) {
List<Integer> myIntegerList = new ArrayList<>();
    for (int i: myMap.keySet()) {
        for ( int j: myMap.get(i)) {
            myIntegerList.add(myCustomMethod(i, j, a.concat(b));
        }
    }
}

public Integer myCustomMethod(int x, int y, String result) {
...
...
...

return Integer;
}
public myMethod(Map myMap,字符串a,int b){
List myIntegerList=new ArrayList();
for(int i:myMap.keySet()){
for(intj:myMap.get(i)){
添加(myCustomMethod(i,j,a.concat(b));
}
}
}
公共整数myCustomMethod(整数x,整数y,字符串结果){
...
...
...
返回整数;
}
我想知道是否可以使用java stream()遍历整数集?

List myIntegerList=myMap.entrySet().stream()
.flatMap(myEntry->
myEntry.getValue().stream()
.map(setEntry->myCustomMethod(myEntry.getKey(),setEntry,a+b)))
.collect(Collectors.toList());
试试这个:

public void myMethod(Map<Integer, Set<Integer>> myMap, String a, String b) {
        List<Integer> myIntegerList = new ArrayList<>();
        for (int i: myMap.keySet()) 
            myIntegerList.addAll(myMap.get(i).stream().map(j -> myCustomMethod(i, j, a.concat(b))).collect(Collectors.toList()));
    }
public void myMethod(映射myMap,字符串a,字符串b){
List myIntegerList=new ArrayList();
for(int i:myMap.keySet())
myIntegerList.addAll(myMap.get(i.stream().map(j->myCustomMethod(i,j,a.concat(b))).collect(Collectors.toList());
}

我将变量“b”更改为String,因为concat需要String(但如果需要int,可以使用方法
Integer.toString(b)

现有代码未编译。返回类型丢失(应该是
void
),并且
a.concat()
不能应用于
int
参数(也许使用
a+b
进行连接是有意的,我不知道)。此外,我假设下一个方法应该声明为
public Integer myCustomMethod(int x,int y,String result)
整数
实例应该在
return
之后给出。通常认为迭代映射的条目集(例如,chrisrhyno2003的答案)比迭代键集然后使用
get()更好
获取每个键的值。显然,您也可以使用循环(
for(Map.Entry:myMap.entrySet())
)。
public void myMethod(Map<Integer, Set<Integer>> myMap, String a, String b) {
        List<Integer> myIntegerList = new ArrayList<>();
        for (int i: myMap.keySet()) 
            myIntegerList.addAll(myMap.get(i).stream().map(j -> myCustomMethod(i, j, a.concat(b))).collect(Collectors.toList()));
    }