Java 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 ten 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 first two 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);
Comments
Post a Comment