Is there a simpler approach by which I can convert Pair to 2 dimensional array?
Here is what I have tried:
public int[][] func() {
List<Pair<Integer, Integer>> pairs = new ArrayList<>();
//this is example value
pairs.add(new Pair<>(1,2));
pairs.add(new Pair<>(2,3));
pairs.add(new Pair<>(3,4));
//I want to make array same size of pair's
int[][] arr = new int[pairs.size()][2];
for(int i =0 ; i < pairs.size(); i++) {
arr[i][0] = pairs.get(i).getKey();
arr[i][1] = pairs.get(i).getValue();
}
return arr;
}
With Stream
s:
int[][] arr = pairs.stream()
.map(pair -> new int[] {pair.getKey(),pair.getValue()})
.toArray(int[][]::new);
BTW, instead of using a javafx Pair
class, perhaps it would be better to use the more standard java.util.AbstractMap.SimpleEntry
class.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments