Saturday, 14 December 2024

Find the even and odd numbers using streams and multiply even numbers with 3 and odd one with 2

 

Find the even and odd numbers in list along with multiply even number with 3 and odd number with 

To achieve this using Java 8 Streams, there are multiple ways to process the list of integers, separate the odd and even numbers, and perform the required transformations (multiplying even numbers by 3 and odd numbers by 2

1 )


import java.util.*;

import java.util.stream.*;


public class Main {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);


        List<Integer> result = numbers.stream()

            .map(n -> (n % 2 == 0) ? n * 3 : n * 2) // Multiply even by 3 and odd by 2

            .collect(Collectors.toList()); // Collect the results into a list


        System.out.println(result);

    }

}




2 ) import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
        List<Integer> result = new ArrayList<>();

        numbers.stream()
            .forEach(n -> {
                if (n % 2 == 0) {
                    result.add(n * 3); // Multiply even by 3
                } else {
                    result.add(n * 2); // Multiply odd by 2
                }
            });

        System.out.println(result);
    }
}


3 )  import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        List<Integer> result = numbers.stream()
            .flatMap(n -> Stream.of((n % 2 == 0) ? n * 3 : n * 2)) // FlatMap example
            .collect(Collectors.toList());

        System.out.println(result);
    }
}

4 ) import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        List<Integer> result = numbers.stream()
            .mapToInt(n -> (n % 2 == 0) ? n * 3 : n * 2) // Use mapToInt for primitive operations
            .boxed() // Box the result back into Integer
            .collect(Collectors.toList());

        System.out.println(result);
    }
}


5 ) import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
        
        Optional<List<Integer>> result = Optional.ofNullable(numbers)
            .filter(list -> !list.isEmpty())
            .map(list -> list.stream()
                .map(n -> (n % 2 == 0) ? n * 3 : n * 2)
                .collect(Collectors.toList()));

        result.ifPresent(System.out::println); // Output the result if present
    }
}

6) import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        Map<Boolean, List<Integer>> grouped = numbers.stream()
            .collect(Collectors.groupingBy(n -> n % 2 == 0)); // Group by even or odd

        List<Integer> result = new ArrayList<>();
        
        grouped.get(true).forEach(n -> result.add(n * 3)); // Multiply even by 3
        grouped.get(false).forEach(n -> result.add(n * 2)); // Multiply odd by 2

        System.out.println(result);
    }
}

7 ) 
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        List<Integer> result = numbers.stream()
            .collect(Collectors.mapping(n -> (n % 2 == 0) ? n * 3 : n * 2, Collectors.toList()));

        System.out.println(result);
    }
}


No comments:

Post a Comment