用于Map< String,Set< String>>的Java 8流 [英] Java 8 stream for Map <String, Set<String>>
问题描述
我有一张地图<整数,设置<整数>>。我想基于自定义方法完成的一些修改将其转换为整数列表。
现在我正在使用两个for循环,我想知道是否有更好的方法来使用java流
'p>下面是我的现有代码:
公共myMethod的(地图<整数,组<整数>> MYMAP,字符串一,int b){
List< Integer> myIntegerList = new ArrayList<>();
为(INT I:myMap.keySet()){
为(诠释J:myMap.get(ⅰ)){
myIntegerList.add(myCustomMethod(I,J,a.concat (b));
}
}
}
public Integer myCustomMethod(int x,int y,String result){
...
...
...
返回整数;
}
我想知道我们是否可以使用java stream()迭代整数集?
尝试这一个:
公共无效myMethod的(地图<整数,组<整数>> MYMAP,字符串一个,字符串b){
列表与LT;整数> myIntegerList =新的ArrayList<>();
为(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)
I have a Map< Integer, Set < Integer > >. I would like to convert it to a list of Integers based on some modification done by a custom method.
Right now I am using two for loops and I wanted to know if there's a better way to do it using java streams
Here's my existing code:
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;
}
I wanted to know if we could iterate through the set of integers using java stream() ?
Try this one:
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()));
}
I change variable "b" to String because concat need String (but if you need int , you can use method Integer.toString(b)
这篇关于用于Map< String,Set< String>>的Java 8流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!