0% found this document useful (0 votes)
8 views

Java 8

Java buffer loader class

Uploaded by

apuravb16
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java 8

Java buffer loader class

Uploaded by

apuravb16
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Occurrence of each character using stream

import java.util.Arrays;

import java.util.Map;

import java.util.function.Function;

import java.util.stream.Collectors;

public class Main {

public static void main(String[] args) {

String str="hellofromcts";

String[] strArr = str.split("");

System.out.println(Arrays.toString(strArr));

Map<String, Long> map = Arrays.stream(strArr)

.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

System.out.println(map);

===================================

Find all duplicate elements in given string

import java.util.Arrays;

import java.util.List;

import java.util.Map;

import java.util.function.Function;

import java.util.stream.Collectors;

public class Main2 {

public static void main(String[] args) {

String str="hellofromcts";
String[] strArr = str.split("");

List<String> list = Arrays.stream(strArr)

.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))

.entrySet()

.stream()

.filter(x -> x.getValue() > 1)

.map(Map.Entry::getKey)

.collect(Collectors.toList());

System.out.println(list);

===================================

Find unique elements

package src;

import java.util.Arrays;

import java.util.List;

import java.util.Map;

import java.util.function.Function;

import java.util.stream.Collectors;

import java.util.stream.Stream;

public class Main4 {

public static void main(String[] args) {

String str="hellofromcts";

String[] strArr = str.split("");

List<String> uniquelist = Arrays.stream(strArr)


.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))

.entrySet()

.stream()

.filter(x -> x.getValue() == 1) // important

.map(Map.Entry::getKey)

.collect(Collectors.toList());

System.out.println(uniquelist);

==============================

Find all odd elements from integer array

public class Main3 {

public static void main(String[] args) {

Stream<Integer> arr = Stream.of(1, 2, 3, 4, 5);

List<Integer> list = arr.filter(e -> e % 2 != 0).collect(Collectors.toList());

System.out.println(list);

=================================================

To find first non repeat element from given string,Use of LinkedHashMap::new

package src;

package src;
import java.util.Arrays;

import java.util.LinkedHashMap;

import java.util.function.Function;

import java.util.stream.Collectors;

public class Main6 {

public static void main(String[] args) {

String str="hellofromcts";

String[] strArr = str.split("");

String firstNonreapeatElement = Arrays.stream(strArr)

.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new,
Collectors.counting()))

.entrySet()

.stream()

.filter(x -> x.getValue() == 1)

.findFirst()

.get().getKey();

System.out.println(firstNonreapeatElement);

=============================
Find second highest number from given array

// First sorted in descending order and skip first element

package src;

import java.util.Arrays;

import java.util.Comparator;

import java.util.LinkedHashMap;

import java.util.List;

import java.util.function.Function;

import java.util.stream.Collectors;

public class Main7 {

public static void main(String[] args) {

int [] numbers={5,9,11,2,8,21,1}; //Its premitive so need to convert to wrapper class by boxed()

Integer number = Arrays.stream(numbers)

.boxed()

.sorted(Comparator.reverseOrder())

.skip(1)

.findFirst()

.get();

System.out.println(number);

==========================================
Find longest string in given array

package src;

import java.util.Arrays;

import java.util.Comparator;

import java.util.Optional;

public class Main8 {

public static void main(String[] args) {

String [] strArr={"java","microservices","Spring boot","Jpa"};

System.out.println(Arrays.toString(strArr));

String longeststring = Arrays.stream(strArr)

.reduce((word1, word2) -> word1.length() > word2.length() ? word1 : word2)

.get();

System.out.println(longeststring);

==============================================

To find all elements from array who starts with 1

package src;

import java.util.Arrays;

import java.util.List;

import java.util.stream.Collectors;

public class Main9 {

public static void main(String[] args) {

int [] numbers ={5,9,11,2,8,21,1};

String strNumbers = numbers.toString();

List list= Arrays.stream(numbers)

.boxed()
.map(s->s+"")

.filter(s->s.startsWith("1"))

.collect(Collectors.toList());

System.out.println(list);

=======================

Use of String.join method

Input : "1", "2", "3", "4"

Output : 1-2-3-4

import java.util.Arrays;

import java.util.List;

public class Main10 {

public static void main(String[] args) {

List<String> numbers = Arrays.asList("1", "2", "3", "4");

String result= String.join("-",numbers);

System.out.println(result);

============================

Skip and limit use case based example

package src;

import java.io.InputStream;

import java.util.Arrays;

import java.util.List;

import java.util.stream.IntStream;
public class Main11 {

public static void main(String[] args) {

IntStream.rangeClosed(1,10) // Use IntStream to specify range

.skip(1)

.limit(8)

.forEach(System.out::println);

=============================

Sort list and map

======================

map() and flatmap()

===================

Sum of integer using IntStream

package testy;

import java.util.stream.IntStream;

public class Main

public static void main(String[] args) {

int total= IntStream.rangeClosed(1,5)

.map(Integer:: valueOf)

.sum();
System.out.println(total);

=======================================

Unique elements

package testy;

import java.util.Arrays;

import java.util.List;

import java.util.stream.Collectors;

public class Main2

public static void main(String[] args) {

List<String> names = Arrays.asList("AAA", "BBB", "CCC", "DDD", "AAA");

System.out.println(names);

List list=names.stream().distinct().collect(Collectors.toList());

System.out.println(list);

You might also like