Opening Hours :7AM to 9PM
import java.util.ArrayList; import java.util.List; public class Example{ public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Ajeet"); names.add("Negan"); names.add("Aditya"); names.add("Steve"); int count = 0; for (String str : names) { if (str.length() < 6) count++; } System.out.println("There are "+count+" strings with length less than 6"); } } Output: There are 3 strings with length less than 6
import java.util.ArrayList; import java.util.List; public class Example{ public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Ajeet"); names.add("Negan"); names.add("Aditya"); names.add("Steve"); //Using Stream and Lambda expression long count = names.stream().filter(str->str.length()<6).count(); System.out.println("There are "+count+" strings with length less than 6"); } } Output: There are 3 strings with length less than 6
import java.util.stream.*; public class Main { public static void main(String[] args){ Stream.iterate(1, count->count+1) .limit(10) .forEach(System.out::println); //.forEach(x->System.out.println(x)) } }
import java.util.stream.*; class Demo { public static void main(String args[]) { Stream.iterate(1,x->x+1) .filter(y->y%2==0) .limit(10) .forEach(System.out::println); } }
import java.util.stream.*; class Demo { public static void main(String args[]) { Stream.iterate(1,x->x+1) .filter(y->y%2==0) .map(p->p*2) .limit(10) .forEach(System.out::println); } }
import java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class Example { public static void main(String[] args) { List<String> names = Arrays.asList("Melisandre","Sansa","Jon","Daenerys","Joffery"); //Creating the stream of all names Stream<String> allNames = names.stream(); //Creating another stream by filtering long names using filter() Stream<String> longNames = allNames.filter(str -> str.length() > 6); //displaying the long names longNames.forEach(str->System.out.print(str+" ")); //longNames.forEach(System.out::print); } }
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Example { public static void main(String[] args) { List<String> names = Arrays.asList("Melisandre","Sansa","Jon","Daenerys","Joffery"); List<String> longnames = names.stream() // converting the list to stream .filter(str -> str.length() > 6) // filter the stream to create a new stream .collect(Collectors.toList()); // collect the final stream and convert it to a List longnames.forEach(System.out::println); } }
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Example { public static void main(String[] args) { List<String> names = Arrays.asList("Melisandre","Sansa","Jon","Daenerys","Joffery"); List<String> longnames = names.stream() .filter(str -> str.length() > 6 && str.length() < 8) //Multiple conditions .collect(Collectors.toList()); longnames.forEach(System.out::println); } }
import java.util.*; import java.util.stream.*; class strex9 { public static void main(String args[]) { Map<Integer,String> M=new HashMap<Integer,String>(); M.put(91,"hyd"); M.put(2,"del"); M.put(3,"hyd2"); M.put(24,"hyd3"); M.put(5,"hyd4"); Map<Integer,String> output=M.entrySet().stream() .filter(x->x.getKey().intValue()>3) .collect(Collectors.toMap(p->p.getKey(),p->p.getValue())); System.out.println(output); } }
import java.util.*; import java.util.stream.*; class strex10 { public static void main(String args[]) { Map<Integer,String> M=new HashMap<Integer,String>(); M.put(91,"hyd"); M.put(2,"del"); M.put(3,"del"); M.put(24,"hyd3"); M.put(5,"hyd4"); Map<<Integer,String> output=M.entrySet().stream() .filter(x-> "del".equals(x.getValue())) .collect(Collectors.toMap(p->p.getKey(),p->p.getValue())); System.out.println(output); } }
import java.util.*; import java.util.stream.*; class strex11 { public static void main(String args[]) { Map<Integer,String>M=new HashMap<Integer,String>(); M.put(91,"hyd"); M.put(2,"del"); M.put(3,"del"); M.put(24,"hyd3"); M.put(5,"dhyd4"); Map<Integer,String> output=M.entrySet().stream() .filter(y->y.getKey().intValue()>2) .filter(x->x.getValue().startsWith("d")) .collect(Collectors.toMap(p->p.getKey(),p->p.getValue())); System.out.println(output); } }
import java.util.*; import java.util.stream.*; class strex12 { public static void main(String args[]) { List<String> ar = Arrays.asList("Java", "Stream", null, "Filter", null); List<String> op=ar.stream().collect(Collectors.toList()); op.forEach(x->System.out.println(x)); } }
import java.util.*; import java.util.stream.*; class strex13 { public static void main(String args[]) { List<String> ar = Arrays.asList("Java", "Stream", null, "Filter", null); List<String> op=ar.stream() .filter(s->s!=null) .collect(Collectors.toList()); op.forEach(x->System.out.println(x)); } }
import java.util.*; import java.util.stream.*; class strex14 { public static void main(String args[]) { List<Integer> ar = Arrays.asList( 12,13, null, 21, null); List<Integer> op=ar.stream() .filter(s->s!=null) .map(d->d*d) .collect(Collectors.toList()); op.forEach(x->System.out.println(x)); } }
import java.util.Map; import java.util.HashMap; public class Example { public static void main(String[] args) { Map<Integer, String> hmap = new HashMap<Integer, String>(); hmap.put(1, "Monkey"); hmap.put(2, "Dog"); hmap.put(3, "Cat"); hmap.put(4, "Lion"); hmap.put(5, "Tiger"); hmap.put(6, "Bear"); /* forEach to iterate and display each key and value pair * of HashMap. */ hmap.forEach((key,value)->System.out.println(key+" - "+value)); /* forEach to iterate a Map and display the value of a particular * key */ hmap.forEach((key,value)->{ if(key == 4){ System.out.println("Value associated with key 4 is: "+value); } }); /* forEach to iterate a Map and display the key associated with a * particular value */ hmap.forEach((key,value)->{ if("Cat".equals(value)){ System.out.println("Key associated with Value Cat is: "+key); } }); } }
import java.util.List; import java.util.ArrayList; public class Example { public static void main(String[] args) { List<String> fruits = new ArrayList<String>(); fruits.add("Apple"); fruits.add("Orange"); fruits.add("Banana"); fruits.add("Pear"); fruits.add("Mango"); //lambda expression in forEach Method fruits.forEach(str->System.out.println(str)); } }
import java.util.List; import java.util.ArrayList; public class Example { public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Maggie"); names.add("Michonne"); names.add("Rick"); names.add("Merle"); names.add("Governor"); names.stream() //creating stream .filter(f->f.startsWith("M")) //filtering names that starts with M .forEach(System.out::println); //displaying the stream using forEach } }
import java.util.List; import java.util.ArrayList; public class Example { public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Maggie"); names.add("Michonne"); names.add("Rick"); names.add("Merle"); names.add("Governor"); //forEach - the output would be in any order System.out.println("Print using forEach"); names.stream() .filter(f->f.startsWith("M")) .parallel() .forEach(n->System.out.println(n)); /* forEachOrdered - the output would always be in this order: * Maggie, Michonne, Merle */ System.out.println("Print using forEachOrdered"); names.stream() .filter(f->f.startsWith("M")) .parallel() .forEachOrdered(n->System.out.println(n)); } }
java.lang.Object | |___java.util.stream.Collectors
import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; public class Example { public static void main(String[] args) { List<String> names = Arrays.asList("Jon", "Ajeet", "Steve", "Ajeet", "Jon", "Ajeet"); Map<String, Long> map = names.stream().collect( Collectors.groupingBy( Function.identity(), Collectors.counting() ) ); System.out.println(map); } }
import java.util.stream.Collectors; import java.util.List; import java.util.ArrayList; class Student{ int id; String name; int age; public Student(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } } public class Example { public static void main(String[] args) { List<Student> studentlist = new ArrayList<Student>(); //Adding Students studentlist.add(new Student(11,"Jon",22)); studentlist.add(new Student(22,"Steve",18)); studentlist.add(new Student(33,"Lucy",22)); studentlist.add(new Student(44,"Sansa",23)); studentlist.add(new Student(55,"Maggie",18)); //Fetching student names as List List<String> names = studentlist.stream() .map(n->n.name) .collect(Collectors.toList()); System.out.println(names); } }
import java.util.stream.Collectors; import java.util.List; import java.util.Set; import java.util.ArrayList; class Student{ int id; String name; int age; public Student(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } } public class Example { public static void main(String[] args) { List<Student> studentlist = new ArrayList<Student>(); //Adding Students studentlist.add(new Student(11,"Jon",22)); studentlist.add(new Student(22,"Steve",18)); studentlist.add(new Student(33,"Lucy",22)); studentlist.add(new Student(44,"Sansa",23)); studentlist.add(new Student(55,"Maggie",18)); //Fetching student data as a Set Set<Student> students = studentlist.stream() .filter(n-> n.id>22) .collect(Collectors.toSet()); //Iterating Set for(Student stu : students) { System.out.println(stu.id+" "+stu.name+" "+stu.age); } } }
import java.util.stream.Collectors; import java.util.List; import java.util.ArrayList; class Student{ int id; String name; int age; public Student(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } } public class Example { public static void main(String[] args) { List<Student> studentlist = new ArrayList>Student>(); //Adding Students studentlist.add(new Student(11,"Jon",22)); studentlist.add(new Student(22,"Steve",18)); studentlist.add(new Student(33,"Lucy",22)); studentlist.add(new Student(44,"Sansa",23)); studentlist.add(new Student(55,"Maggie",18)); //Getting the average Age Double avgAge = studentlist.stream() .collect(Collectors.averagingInt(s->s.age)); System.out.println("Average Age of Students is: "+avgAge); } }
import java.util.StringJoiner; public class Example { public static void main(String[] args) { // Passing Hyphen(-) as delimiter StringJoiner mystring = new StringJoiner("-"); // Joining multiple strings by using add() method mystring.add("Logan"); mystring.add("Magneto"); mystring.add("Rogue"); mystring.add("Storm"); // Displaying the output String System.out.println(mystring); } }
import java.util.StringJoiner; public class Example { public static void main(String[] args) { /* Passing comma(,) as delimiter and opening bracket * "(" as prefix and closing bracket ")" as suffix */ StringJoiner mystring = new StringJoiner(",", "(", ")"); // Joining multiple strings by using add() method mystring.add("Negan"); mystring.add("Rick"); mystring.add("Maggie"); mystring.add("Daryl"); // Displaying the output String System.out.println(mystring); } }
import java.util.StringJoiner; public class Example { public static void main(String[] args) { /* Passing comma(,) as delimiter and opening bracket * "(" as prefix and closing bracket ")" as suffix */ StringJoiner mystring = new StringJoiner(",", "(", ")"); mystring.add("Negan"); mystring.add("Rick"); mystring.add("Maggie"); mystring.add("Daryl"); System.out.println("First String: "+mystring); /* Passing hyphen(-) as delimiter and string "pre" * as prefix and string "suff" as suffix */ StringJoiner myanotherstring = new StringJoiner("-", "pre", "suff"); myanotherstring.add("Sansa"); myanotherstring.add("Imp"); myanotherstring.add("Jon"); myanotherstring.add("Ned"); System.out.println("Second String: "+myanotherstring); /* Merging both the strings * The important point to note here is that the output string will be * having the delimiter prefix and suffix of the first string (the string * which is calling the merge method of StringJoiner) */ StringJoiner mergedString = mystring.merge(myanotherstring); System.out.println(mergedString); } }
import java.util.StringJoiner; public class Example { public static void main(String[] args) { //Comma(,) as delimiter StringJoiner mystring = new StringJoiner(","); /* Using setEmptyValue() method, we can set the default value * of a StringJoiner instance, so if the StringJoiner is empty * and we print the value of it, this default value will be * displayed */ mystring.setEmptyValue("This is a default String"); /* We have not added any string to StringJoiner yet so * this should display the default value of StringJoiner */ System.out.println("Default String: "+mystring); // Adding strings to StringJoiner mystring.add("Apple"); mystring.add("Banana"); mystring.add("Orange"); mystring.add("Kiwi"); mystring.add("Grapes"); System.out.println(mystring); /* The length() method of StringJoiner class returns the * length of the string (the number of characters in the * StringJoiner instance) */ int length = mystring.length(); System.out.println("Length of the StringJoiner: "+length); /* The toString() method is used for converting a StringJoiner * instance to a String. */ String s = mystring.toString(); System.out.println(s); } }
public class Example { public static void main(String[] args) { String[] str = new String[10]; //Getting the substring String str2 = str[9].substring(2, 5); //Displaying substring System.out.print(str2); } }
//Importing Optional class import java.util.Optional; public class Example { public static void main(String[] args) { String[] str = new String[10]; OptionalisNull = Optional.ofNullable(str[9]); if(isNull.isPresent()){ //Getting the substring String str2 = str[9].substring(2, 5); //Displaying substring System.out.print("Substring is: "+ str2); } else{ System.out.println("Cannot get the substring from an empty string"); } str[9] = "AgraIsCool"; Optional isNull2 = Optional.ofNullable(str[9]); if(isNull2.isPresent()){ //Getting the substring String str2 = str[9].substring(2, 5); //Displaying substring System.out.print("Substring is: "+ str2); } else{ System.out.println("Cannot get the substring from an empty string"); } } }
import java.util.List; import java.util.*; public class Main { public static void main(String[] args) { List<Integer> n = Arrays.asList(1,2,3,4,5,6); n.stream() .map(i->i*2) .forEach(System.out::println); } }
import java.util.List; import java.util.*; import java.util.stream.Stream; public class Main { public static void main(String[] args) { List<Integer> n = Arrays.asList(1,2,3,4); n.stream() .flatMap(i->Stream.of(i*2,i*3,i*4)) .forEach(System.out::println); } }