--
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tvidushi.DONE; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.regex.Pattern; | |
import java.util.stream.Stream; | |
public class _CommonOperation { | |
public static List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); | |
/** | |
* You are given a list of words & you have to count the number of elements | |
* */ | |
public static void countElements(){ | |
System.out.print("Stream.of( __,__,__,).count() : "+Stream.of("abc","bca","bac","dca","efg").count()); | |
} | |
/** | |
* You are given a list of words & you have to count the number of elements of defined length | |
* */ | |
public static void countElementsDefinedLength(){ | |
System.out.print("Stream.of( __,__,__,).filter().(Pattern.compile(---)).asPredicate()).count : "); | |
System.out.print( Stream.of("abc","bca","bac","dca","efgUI") | |
.filter(Pattern.compile("\\b\\w{3}\\b") //Predicate | |
.asPredicate()) | |
.count()); | |
} | |
/** | |
* You are given a list of ethencity of people. | |
* Find unique ethencities | |
*/ | |
public static void countDistinctElements() { | |
System.out.println("Count: ethicities listed list.stream().distinct().count()"); | |
List<String> list = Arrays.asList("white","white","hispanic", | |
"asian","asian","african-american", | |
"white","white","hispanic"); | |
System.out.println("Count: ethicities listed "+list.stream().distinct().count()); | |
} | |
/** | |
* You are given a list of ethencity of people. | |
* Find unique ethencities | |
*/ | |
public static void countElementInMultipleLists() { | |
Stream<String> class_1 = Stream.of("Aradhya", "Abhinav", "Avinash","Supriya"); | |
Stream<String> class_2 = Stream.of("Anirudh", "Vinay", "Kiara","Abhinav","Supriya"); | |
System.out.println("Stream.concat(class_1, class_2)"); | |
Stream<String> commonNames = Stream.concat(class_1, class_2); | |
commonNames.forEach(e->System.out.print(e+" ")); | |
} | |
/** | |
* You are given a list of ethencity of people. | |
* Find unique ethencities | |
*/ | |
public static void countDistinctElementInMultipleLists() { | |
Stream<String> class_1 = Stream.of("Aradhya", "Abhinav", "Avinash","Supriya"); | |
Stream<String> class_2 = Stream.of("Anirudh", "Vinay", "Kiara","Abhinav","Supriya"); | |
System.out.println("Stream.concat(class_1, class_2).distinct()"); | |
Stream<String> commonNames = Stream.concat(class_1, class_2).distinct(); | |
commonNames.forEach(e->System.out.print(e+" ")); | |
} | |
public static void sum_MapValues() { | |
Map<String, Integer> num = new HashMap(); | |
num.put("1", 125); | |
num.put("2", 125); | |
System.out.println("num.values().stream().mapToInt(i -> i).sum())"); | |
System.out.println(); | |
System.out.println(" "+num.values(). | |
stream(). | |
mapToInt(i -> i). | |
sum()); | |
} | |
public static void sum_reduce() { | |
System.out.println(); | |
System.out.println( | |
numbers.stream() | |
.reduce(0, (total, e) -> Integer.sum(total, e))); | |
// .reduce(0, Integer::sum)); | |
} | |
public static void sum_maptoint(String[] args) { | |
//given the values, double the even numbers and total. | |
System.out.println( | |
numbers.stream() | |
.filter(e -> e % 2 == 0) | |
.mapToInt(e -> e * 2) | |
.sum()); | |
} | |
public static void example_3() { | |
System.out.println("numbers.stream().reduce(0,(total,e)->Integer.sum(sum,e))"); | |
System.out.println( | |
numbers.stream() | |
.reduce(0, (total, e) -> Integer.sum(total, e))); | |
} | |
public static void concat_reduce() { | |
System.out.println( | |
numbers.stream() | |
.map(String::valueOf) | |
// .reduce("", (carry, str) -> carry.concat(str))); | |
.reduce("", String::concat)); | |
} | |
public static void sum_List() { | |
List<Integer> list = new ArrayList<>(Arrays.asList(100,200,300)); | |
System.out.println("_________"+list. | |
stream(). | |
mapToInt(i->i).sum()); | |
} | |
public static void main(String[] args) { | |
System.out.println(); | |
countElements(); | |
System.out.println(); | |
countElementsDefinedLength(); | |
System.out.println(); | |
countDistinctElements(); | |
System.out.println(); | |
countElementInMultipleLists() ; | |
System.out.println(); | |
System.out.println(); | |
countDistinctElementInMultipleLists(); | |
System.out.println(); | |
concat_reduce(); | |
System.out.println(); | |
sum_List() ; | |
} | |
} |
--
No comments:
Post a Comment