Showing posts with label programs. Show all posts
Showing posts with label programs. Show all posts

Sunday, 8 June 2025

Nth highest salary from a given list using Java 8 Streams or second highest salary

 

Nth highest salary from a given list using Java 8 Streams. Below is a step-by-step program that demonstrates this using distinct sorting and skipping elements.


Java Program: Find the Nth Highest Salary


import java.util.Arrays;

import java.util.List;

import java.util.Optional;


public class NthHighestSalary {

    public static void main(String[] args) {

        List<Integer> salaries = Arrays.asList(50000, 60000, 75000, 40000, 75000, 90000, 60000);

        int n = 4; // Change this value to find the nth highest salary


        Optional<Integer> nthHighest = salaries.stream()

                .distinct() // Remove duplicate salaries

                .sorted((a, b) -> Integer.compare(b, a)) // Sort salaries in descending order

                .skip(n - 1) // Skip (n-1) highest salaries

                .findFirst(); // Get the nth highest salary


        System.out.println(n + "th highest salary: " + nthHighest.orElse(null));

    }

}


Exaplnation 

Create a list of salaries: Some salaries may be duplicate.


Use .stream(): Convert the list into a stream.


Remove duplicates: .distinct() ensures only unique salaries are considered.


Sort in descending order: .sorted((a, b) -> Integer.compare(b, a)) arranges salaries from highest to lowest.


Skip (n-1) highest salaries: .skip(n - 1) moves past the first (n-1) elements.


Get the next element: .findFirst() extracts the Nth highest salary.


Print the result: If the salary exists, it prints, otherwise null is returned




Second highest salary with given list


import java.util.Arrays;

import java.util.List;

import java.util.Optional;


public class SecondHighestNumber {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(12, 5, 8, 20, 15, 9, 20);


        Optional<Integer> secondHighest = numbers.stream()

                .distinct() // Remove duplicate numbers

                .sorted((a, b) -> Integer.compare(b, a)) // Sort in descending order

                .skip(1) // Skip the highest number

                .findFirst(); // Get the second highest


        System.out.println("Second highest number: " + secondHighest.orElse(null));

    }

}




2 ) 

import java.util.Arrays;

import java.util.List;

import java.util.Optional;


public class SecondHighestNumber {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(12, 5, 8, 20, 15, 9, 20);


        Optional<Integer> highest = numbers.stream().max(Integer::compare);

        Optional<Integer> secondHighest = numbers.stream()

                .filter(num -> !num.equals(highest.orElse(null))) // Remove the highest

                .max(Integer::compare); // Find the second highest


        System.out.println("Second highest number: " + secondHighest.orElse(null));

    }

}




3 .


public class SecondHighestSalaryWithSet {

    public static void main(String[] args) {

        List<Employee> employees = Arrays.asList(

            new Employee("Alice", 50000),

            new Employee("Bob", 60000),

            new Employee("Charlie", 70000),

            new Employee("David", 80000),

            new Employee("Eve", 90000),

            new Employee("Frank", 90000) // Duplicate salary

        );


        // Find the second highest salary using a Set to eliminate duplicates

        Optional<Double> secondHighestSalary = employees.stream()

            .map(emp -> emp.salary)

            .collect(Collectors.toSet())               // Collect to a Set to remove duplicates

            .stream()

            .sorted(Comparator.reverseOrder())

            .skip(1)

            .findFirst();


        // Print the result

        System.out.println("Second Highest Salary: " + secondHighestSalary.orElse(-1));

    }

}






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);
    }
}