Untitled
unknown
plain_text
a year ago
11 kB
4
Indexable
1. **Given a list of employees, find the employee with the highest salary using streams and lambda expressions.** ```java Employee highestPaidEmployee = employees.stream() .max(Comparator.comparing(Employee::getSalary)) .orElseThrow(NoSuchElementException::new); ``` 2. **Given a list of integers, find the maximum value using streams.** ```java int max = numbers.stream() .max(Integer::compare) .orElseThrow(NoSuchElementException::new); ``` 3. **Convert a list of strings to uppercase using streams.** ```java List<String> upperCaseNames = names.stream() .map(String::toUpperCase) .collect(Collectors.toList()); ``` 4. **Calculate the average of a list of integers using streams.** ```java double average = numbers.stream() .mapToInt(Integer::intValue) .average() .orElse(0); ``` 5. **Find the second highest number in a list of integers using streams.** ```java int secondHighest = numbers.stream() .sorted(Comparator.reverseOrder()) .skip(1) .findFirst() .orElseThrow(NoSuchElementException::new); ``` 6. **Group a list of employees by department using streams.** ```java Map<String, List<Employee>> employeesByDepartment = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment)); ``` 7. **Find all unique characters in a list of strings using streams.** ```java Set<Character> uniqueChars = strings.stream() .flatMapToInt(String::chars) .mapToObj(c -> (char) c) .collect(Collectors.toSet()); ``` 8. **Count the occurrences of each character in a string using streams.** ```java Map<Character, Long> charCount = str.chars() .mapToObj(c -> (char) c) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); ``` 9. **Find the first non-repeated character in a string using streams.** ```java char firstNonRepeated = str.chars() .mapToObj(c -> (char) c) .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting())) .entrySet() .stream() .filter(entry -> entry.getValue() == 1) .map(Map.Entry::getKey) .findFirst() .orElseThrow(NoSuchElementException::new); ``` 10. **Partition a list of integers into even and odd using streams.** ```java Map<Boolean, List<Integer>> partitioned = numbers.stream() .collect(Collectors.partitioningBy(n -> n % 2 == 0)); ``` 11. **Find the length of the longest string in a list using streams.** ```java int maxLength = strings.stream() .mapToInt(String::length) .max() .orElse(0); ``` 12. **Remove duplicates from a list of integers using streams.** ```java List<Integer> uniqueNumbers = numbers.stream() .distinct() .collect(Collectors.toList()); ``` 13. **Find the sum of all salaries of employees using streams.** ```java double totalSalary = employees.stream() .mapToDouble(Employee::getSalary) .sum(); ``` 14. **Convert a list of integers to a comma-separated string using streams.** ```java String result = numbers.stream() .map(String::valueOf) .collect(Collectors.joining(", ")); ``` 15. **Find the employee with the longest name using streams.** ```java Employee employeeWithLongestName = employees.stream() .max(Comparator.comparingInt(e -> e.getName().length())) .orElseThrow(NoSuchElementException::new); ``` 16. **Check if all employees have a salary greater than a certain amount using streams.** ```java boolean allMatch = employees.stream() .allMatch(e -> e.getSalary() > 50000); ``` 17. **Check if any employee is from a specific department using streams.** ```java boolean anyMatch = employees.stream() .anyMatch(e -> "HR".equals(e.getDepartment())); ``` 18. **Get a list of all employee names sorted alphabetically using streams.** ```java List<String> sortedNames = employees.stream() .map(Employee::getName) .sorted() .collect(Collectors.toList()); ``` 19. **Convert a list of integers to a set using streams.** ```java Set<Integer> numberSet = numbers.stream() .collect(Collectors.toSet()); ``` 20. **Calculate the product of all integers in a list using streams.** ```java int product = numbers.stream() .reduce(1, (a, b) -> a * b); ``` 21. **Find the employee with the shortest name using streams.** ```java Employee employeeWithShortestName = employees.stream() .min(Comparator.comparingInt(e -> e.getName().length())) .orElseThrow(NoSuchElementException::new); ``` 22. **Sort a list of employees by salary in descending order using streams.** ```java List<Employee> sortedBySalaryDesc = employees.stream() .sorted(Comparator.comparing(Employee::getSalary).reversed()) .collect(Collectors.toList()); ``` 23. **Filter a list of strings to include only those that contain a specific substring using streams.** ```java List<String> filteredStrings = strings.stream() .filter(s -> s.contains("specific")) .collect(Collectors.toList()); ``` 24. **Find the difference between the maximum and minimum values in a list of integers using streams.** ```java int difference = numbers.stream() .collect(Collectors.summarizingInt(Integer::intValue)) .getMax() - numbers.stream() .collect(Collectors.summarizingInt(Integer::intValue)) .getMin(); ``` 25. **Remove all null values from a list using streams.** ```java List<String> nonNullStrings = strings.stream() .filter(Objects::nonNull) .collect(Collectors.toList()); ``` 26. **Convert a list of strings to a map with the string as the key and its length as the value using streams.** ```java Map<String, Integer> stringLengthMap = strings.stream() .collect(Collectors.toMap(Function.identity(), String::length)); ``` 27. **Find the sum of squares of all integers in a list using streams.** ```java int sumOfSquares = numbers.stream() .mapToInt(n -> n * n) .sum(); ``` 28. **Concatenate a list of strings into a single string using streams.** ```java String concatenated = strings.stream() .collect(Collectors.joining()); ``` 29. **Get a list of distinct employee departments using streams.** ```java List<String> distinctDepartments = employees.stream() .map(Employee::getDepartment) .distinct() .collect(Collectors.toList()); ``` 30. **Filter out even numbers from a list of integers using streams.** ```java List<Integer> oddNumbers = numbers.stream() .filter(n -> n % 2 != 0) .collect(Collectors.toList()); ``` 31. **Count the number of strings with length greater than 5 using streams.** ```java long count = strings.stream() .filter(s -> s.length() > 5) .count(); ``` 32. **Find the employee with the earliest joining date using streams.** ```java Employee earliestJoinedEmployee = employees.stream() .min(Comparator.comparing(Employee::getJoiningDate)) .orElseThrow(NoSuchElementException::new); ``` 33. **Check if a list of integers contains only positive numbers using streams.** ```java boolean allPositive = numbers.stream() .allMatch(n -> n > 0); ``` 34. **Convert a list of employee objects to a list of their names using streams.** ```java List<String> employeeNames = employees.stream() .map(Employee::getName) .collect(Collectors.toList()); ``` 35. **Find the total number of characters in a list of strings using streams.** ```java int totalCharacters = strings.stream() .mapToInt(String::length) .sum(); ``` 36. **Get a list of employees sorted by their names using streams.** ```java List<Employee> sortedByName = employees.stream() .sorted(Comparator.comparing(Employee::getName)) .collect(Collectors.toList()); ``` 37. **Find the most frequently occurring element in a list using streams.** ```java int mostFrequent = numbers.stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) .entrySet() .stream() .max(Map.Entry.comparingByValue()) .map(Map.Entry::getKey) .orElseThrow(NoSuchElementException::
Editor is loading...
Leave a Comment