main문
class Main {
public static void main(String[] args) {
new ver1().run();
System.out.printf("\n");
new ver2().run();
System.out.printf("\n");
int[] arr = new int[]{33, 2, 55, 4, 51, 6, 71, 18, 29, 10};
Object result = Arrays.stream(arr)
.filter(e -> e % 2 == 0)
.map(e -> e * 2)
.boxed()
.collect(Collectors.<Integer>toList());
System.out.printf("%s\n", Arrays.toString(arr));
System.out.printf("%s", result);
}
}
List + for문
class ver1{
public void run(){
int[] arr ={33,2,55,4,51,6,71,18,29,10};
List <Integer> result = new ArrayList<>();
for (int n : arr) if (n % 2 == 0) result.add(n);
for (int i=0; i<result.size(); i++) {
result.set( i , result.get(i)*2 );
}
System.out.printf("%s\n", Arrays.toString(arr));
System.out.printf("%s", result);
}
}
Stream
class ver2{
public void run() {
int[] arr = new int[]{33, 2, 55, 4, 51, 6, 71, 18, 29, 10};
Object result = Arrays.stream(arr)
.filter(e -> e % 2 == 0)
.map(e -> e * 2)
.boxed()
.collect(Collectors.<Integer>toList());
System.out.printf("%s\n", Arrays.toString(arr));
System.out.printf("%s", result);
}
}