Iterations
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.java8.iterate; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.stream.Stream; | |
/** | |
* | |
* @author takshila.vidushi | |
* | |
*/ | |
public class IterationExamples { | |
/** | |
* Obtaining a stream from (LIST) | |
*/ | |
public static void iterationgList(){ | |
System.out.println("---Obtaining a stream from (LIST)---"); | |
populateList().stream() | |
.forEach((x)->System.out.print(" "+x)); | |
} | |
/** | |
* Obtaining a stream from (MAP USING ENTRYSET) | |
*/ | |
public static void iterationgMap(){ | |
System.out.println("---Obtaining a stream from (MAP USING ENTRYSET)---"); | |
populateMap().entrySet() | |
.stream() | |
.forEach(x->System.out.println(x));; | |
} | |
/** | |
* Obtaining a stream from (MAP USING ENTRYSET) | |
*/ | |
public static void iterationgMap2(){ | |
Map<String,String> map = populateMap(); | |
for(Map.Entry<String, String> entry: map.entrySet()){ | |
System.out.println("map name is :"+entry.getKey()+"map entry is :"+entry.getValue()); | |
} | |
} | |
/** | |
* Obtaining a stream from (MAP USING KEYSET) | |
*/ | |
public static void iterationgMap3(){ | |
System.out.println("--Obtaining a stream from (MAP USING KEYSET)----"); | |
populateMap().keySet() | |
.stream() | |
.forEach(x->System.out.println(" "+x)); | |
} | |
/** | |
* Obtaining a stream from (MAP USING VALUES) | |
*/ | |
public static void iterationgMap4(){ | |
System.out.println("----Obtaining a stream from (MAP USING VALUES)-----"); | |
populateMap().values() | |
.stream() | |
.forEach(x->System.out.print(" "+x)); | |
} | |
/** | |
* Obtaining a stream from (STRING USING CHARS) | |
*/ | |
public static void iterationgChars(){ | |
System.out.println("--Obtaining a stream from (STRING USING CHARS)-----"); | |
"123456789sadasdas".chars().forEach(x->System.out.print(" "+(char)x)); | |
} | |
/** | |
* Obtaining a stream from (STRING USING SPLIT) | |
*/ | |
public static void iterationgString(){ | |
System.out.println("--Obtaining a stream from (STRING USING SPLIT)--"); | |
Stream.of(("hot summer cool winter ".split(" "))) | |
.forEach(x->System.out.print(" "+x)); | |
} | |
/** | |
* Obtaining a stream from (FROM AN ARRAY) | |
*/ | |
public static void iterationgArrays(){ | |
System.out.println("-----------Obtaining a stream from (FROM AN ARRAY)--"); | |
Integer[] array = {1,2,3,4,5,6,7,8,9,10}; | |
Stream.of(array).forEach(x->System.out.print(" "+x)); | |
} | |
/** | |
* Obtaining a stream from (OF VALUES) | |
*/ | |
public static void createStream(){ | |
System.out.println("-----------Obtaining a stream from (OF VALUES)--"); | |
Stream.of("one","two","three").forEach(x->System.out.println(x)); | |
} | |
/** | |
* Obtaining a stream from (FROM FUNCTION GENERATE) | |
*/ | |
public static void generatingStream(){ | |
System.out.println("-----------Obtaining a stream from (FROM FUNCTION GENERATE)---------"); | |
Stream.generate(()->{return Math.random();}) | |
.limit(20) | |
.forEach(x->System.out.println(x)); | |
} | |
/** | |
* Obtaining a stream from (FROM FUNCTION ITERATE) | |
*/ | |
public static void obtainStream(){ | |
System.out.println("-----------Obtaining a stream from (FROM FUNCTION ITERATE)------------"); | |
Stream.iterate(0,i->i+1) | |
.limit(30) | |
.forEach(x->System.out.println(x)); | |
} | |
/** | |
* Obtaining a stream from (FROM FUNCTION BUILDER) | |
*/ | |
public static void createStream_Builderfn(){ | |
System.out.println("-----------Obtaining a stream from (FROM FUNCTION BUILDER)-------"); | |
Stream.builder() | |
.add("one") | |
.add("two") | |
.add("three") | |
.build() | |
.forEach(p->System.out.println(p)); | |
} | |
/** | |
* Obtaining a stream from (ANOTHER STREAM) | |
*/ | |
public static void createStream_2(){ | |
System.out.println("-----------Obtaining a stream from (ANOTHER STREAM)----------------"); | |
populateList().stream() | |
.distinct() | |
.limit(2) | |
.sorted() | |
.forEach(x->System.out.println(x)); | |
} | |
public static void main(String args[]){ | |
iterationgList(); | |
System.out.println(); | |
iterationgMap(); | |
System.out.println(); | |
iterationgMap2(); | |
System.out.println(); | |
iterationgMap3(); | |
System.out.println(); | |
iterationgMap4(); | |
System.out.println(); | |
iterationgChars(); | |
System.out.println(); | |
iterationgString(); | |
System.out.println(); | |
iterationgArrays(); | |
System.out.println(); | |
createStream_Builderfn(); | |
System.out.println(); | |
createStream_2(); | |
} | |
private static List<String> populateList(){ | |
return Arrays.asList("value1","value2","value3","value4","value5","value6","value6","value7","value8","value9","value10"); | |
} | |
private static Map populateMap(){ | |
Map<String,String> map = new HashMap(); | |
map.put("mapKey1", "mapValue1"); | |
map.put("mapKey2", "mapValue2"); | |
map.put("mapKey3", "mapValue3"); | |
map.put("mapKey4", "mapValue4"); | |
map.put("mapKey5", "mapValue5"); | |
map.put("mapKey6", "mapValue6"); | |
return map ; | |
} | |
} |
Output:
No comments:
Post a Comment