Java 8 Programs
Java 8 Programs
Program to ADD PREFIX and SUFFIX to the STRING?
- StringJoiner class, used to add prefix and suffix in a given String
StringJoiner stringJoiner = new StringJoiner(",", "#", "#");
stringJoiner.add("Interview");
stringJoiner.add("Questions");
stringJoiner.add("Answers");
System.out.println(stringJoiner);
Program to PRINT 10 RANDOM NUMBERs using forEach?
- Use Random class to generate Random number
Random random = new Random();
random.ints().limit(10).forEach(System.out::println);
Program to iterate a Stream using the forEach method?
List<String> str = Arrays.asList("Hello","Hola","Namaste","Hi");
str.stream().forEach(System.out::println);
Program to find the Minimum and Maximum number of a Stream?
To find minimum or maximum number in stream, use comparator.comparing() method which will take integer value from Stream.
Integer min = Stream.of(1, 2, 3, 4, 5, 6,7).min(Comparator.comparing(Integer::valueOf)).get();
Integer max = Stream.of(1, 2, 3, 4, 5, 6,7).max(Comparator.comparing(Integer::valueOf)).get();
Program to Print Strings whose length is greater than 3 in List?
List<String> stringList = Arrays.asList("Hello","Interview","Questions","Answers","Ram","for");
stringList.stream().filter(str -> str.length() > 3).forEach(System.out::println);
Program to multiply 3 to all element in list and print the list?
use map() function, will perform operation and will generate new stream with updated value
List<Integer> integerList = Arrays.asList(1,2,3,4,5,6,7);
System.out.println(integerList.stream().map(i -> i*3).collect(Collectors.toList()));
Program to perform concatenation on two Streams?
List<Integer> integerList1 = Arrays.asList(1,2,3,4);
List<Integer> integerList2 = Arrays.asList(5,6,7);
Stream<Integer> concatStream = Stream.concat(integerList1.stream(), integerList2.stream());
concatStream.forEach(System.out::print);
Program to REMOVE the DUPLICATE elements from the list?
To remove duplicate element form list, use Collectors.toSet()
List<Integer> integerList = Arrays.asList(1,2,3,4,1,2,3);
integerList.stream().collect(Collectors.toSet()).forEach(System.out::print);
Print current date and time, in Date and Time API?
To get current date and time we are using LocalDate, LocalTime, and LocalDateTime API provided
System.out.println("Current Date: " + java.time.LocalDate.now());
System.out.println("Current Time: " + java.time.LocalTime.now());
System.out.println("Current Date and Time: " + java.time.LocalDateTime.now());
Program to SORT the given list?
To Sort the integer element given in List, use sorted() method which will sort the List elements.
List<Integer> integerList = Arrays.asList(4,5,6,7,1,2,3);
integerList.stream().sorted().forEach(System.out::println); //AO
integerList.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println); //DO
To find FIND 2 BIGGEST elements:
integerList.stream().sorted(Comparator.reverseOrder()).limit(2).forEach(System.out::println);
To find statistics, max, min, count, sum,
integerList.stream().mapToInt((x)->x).summaryStatistics();
Program to get the SUM of all numbers present in a list?
List<Integer> integerList = Arrays.asList(4,5,6,7,1,2,3);
System.out.println(integerList.stream().mapToInt(Integer::intValue).sum());
Program to perfrom cube on list elements and filter numbers greater than 50.
List<Integer> integerList = Arrays.asList(4,5,6,7,1,2,3);
integerList.stream().map(i -> i*i*i).filter(i -> i>50).forEach(System.out::println);
Stream.reduce()
In Java 8, the Stream.reduce() combine elements of a stream and produces a single value.
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = Arrays.stream(numbers).reduce(0, Integer::sum);
int sum = Arrays.stream(numbers).reduce(0, (a, b) -> a + b);
int max1 = Arrays.stream(numbers).reduce(0, Integer::max);
int min1 = Arrays.stream(numbers).reduce(0, Integer::min);
Program to convert list to MAP?
int[] array = new int[] { 18, 67, 134, 87, 11, 11 };
List<Integer> list = Arrays.stream(array).boxed().collect(Collectors.toList());
Map<Integer, Long> map = list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
List<String> names = Arrays.asList("John", "Mary", "Bob");
Map<String, Integer> nameMap = names.stream().collect(Collectors.toMap(name -> name, name -> name.length()));
Program to apply groupingBy and Comparator in Employee List?
// find highest salary employee in each department
Map<String, Optional<Employee>> highestPaidByDept = empList.stream().collect(Collectors.groupingBy(Employee::getDepartment, Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary))));
// list of employees whose salary is less than 10000 and sort in descending order on name
empList.stream().filter(e -> e.getSalary() < 10000).sorted(Comparator.comparing(Employee::getName).reversed()).forEach(System.out::println);
Comments
Post a Comment