You are on page 1of 11

Java 8 New Features

in Simple Way

Lambda Expressions
with
Collections

DURGASOFT
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
1  040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com

ArrayList 2. 11) System. Java 8 New Features in Simple Way Lambda Expressions with Collections Collection is nothing but a group of objects represented as a single entity. Set(I) 3. 12) } 13) } Output:[Sunny.add("Sunny").LinkedList. The important Collection types are: 1.out. Sunny] Note: List(may be ArrayList.com . Hyderabad . 10) l. Insertion order is preserved 2. 2  040 – 64 51 27 86. Collecttions. 2) class Test 3) { 4) public static void main(String[] args) 5) { 6) ArrayList<String> l = new ArrayList<String>(). If we want sorting for the list then we should use Collections class sort() method.println(l).ArrayList. 8) l.add("Chinny").Vector or Stack) never talks about sorting order. List(I) 2. 7) l. HUDA Maitrivanam.sort(list. List(I): If we want to represent a group of objects as a single entity where duplicate objects are allowed and insertion order is preserved then we shoud go for List. 80 96 96 96 96. Bunny. 92 46 21 21 43 | www. Duplicate objects are allowed The main implementation classes of List interface are: 1. 2 Floor.Comparator)==>meant for Customized Sorting Order nd DURGASOFT. LinkedList 3.sort(list)==>meant for Default Natural Sorting Order Collections. Stack Demo Program to describe List Properties: 1) import java.util. 9) l.add("Sunny"). # 202.durgasoft. Vector 4. Map(I) 1. Ameerpet.add("Bunny"). Chinny. 1.500038.

HashSet. Insertion order is not preserved 2. TreeMap etc nd DURGASOFT.500038. Duplicates are not allowed. HUDA Maitrivanam. 8) l.add("Bunny"). Set(I): If we want to represent a group of individual objects as a single entity where duplicate objects are not allowed and insertion order is not preserved then we should go for Set.out. 1.If we are trying to add duplicates then we won't get any error. Java 8 New Features in Simple Way 2. 11) System. if we want sorting order then we should go for: TreeSet 3. 9) l.add("Chinny"). Bunny. 92 46 21 21 43 | www. 3  040 – 64 51 27 86. 80 96 96 96 96. Sunny] Note: In the case of Set. Map(I): If we want to represent objects as key-value pairs then we should go for Map Eg: Rollno-->Name mobilenumber-->address The important implementation classes of Map are: 1. 12) } 13) } Output: [Chinny. HashSet 2. Hyderabad .add("Sunny").println(l). just add() method returns false.util.add("Sunny"). 2 Floor. 7) l. Ameerpet. 2) class Test 3) { 4) public static void main(String[] args) 5) { 6) HashSet<String> l = new HashSet<String>(). The following are important Set implementation classes 1.TreeSet etc Demo Program for Set: 1) import java. # 202.durgasoft. HashMap 2.com . 10) l.

13) } 14) } Output: {A=Apple."Boy").put("T". public int compare(obj1. 2) class Test 3) { 4) public static void main(String[] args) 5) { 6) HashMap<String. 92 46 21 21 43 | www. # 202.HashMap. 9) m.put("Durga".durgasoft. 7) m."Tiger").sort(list."Zebra").sort(list)==>meant for Default Natural Sorting Order For String objects: Alphabetical Order For Numbers : Ascending order Instead of Default natural sorting order if we want customized sorting order then we should go for Comparator interface. 11) m. HUDA Maitrivanam.String> m= new HashMap<String. Durga=Java} Sorted Collections: 1. Hyderabad . Java 8 New Features in Simple Way Demo Program for Map: 1) import java."Apple"). 12) System.String>().put("Z". If we want sorting for the list then we should use Collections class sort() method.Comparator)==>meant for Customized Sorting Order nd DURGASOFT."Java"). Sorted Set 3.Vector or Stack) never talks about sorting order.util. 4  040 – 64 51 27 86.com . Sorted List: List(may be ArrayList. 10) m. Z=Zebra.out. Ameerpet.println(m). Sorted Map 1. T=Tiger. 8) m. 80 96 96 96 96. Sorted List 2. B=Boy.obj2) returns -ve iff obj1 has to come before obj2 returns +ve iff obj1 has to come after obj2 returns 0 iff obj1 and obj2 are equal Collections.put("B".LinkedList. Collecttions. 2 Floor.500038. Comparator interface contains only one abstract method: compare() Hence it is Functional interface.put("A".

9) l.sort(l). 12) l. 14) } 15) else 16) { 17) return 0.util.out. 5.println("After Sorting:"+l).out. 15. 16) } 17) } Output: Before Sorting:[10.500038. 20] After Sorting:[0.util. 2) import java.util.TreeSet. Hyderabad . 2 Floor. 15) System. 10. Ameerpet.durgasoft.add(10).add(20). Java 8 New Features in Simple Way Demo Program to Sort elements of ArrayList according to Defaut Natural Sorting Order(Ascending Order): 1) import java. 92 46 21 21 43 | www. 5  040 – 64 51 27 86. 15. 2) import java. 20] Demo Program to Sort elements of ArrayList according to Customized Sorting Order(Descending Order): 1) import java. 13) System. 11) l. HUDA Maitrivanam. 5. 10) } 11) else if(I1>I2) 12) { 13) return -1.add(0).ArrayList.Comparator. 3) class MyComparator implements Comparator<Integer> 4) { 5) public int compare(Integer I1. 80 96 96 96 96. 0. 18) } 19) } 20) } 21) class Test nd DURGASOFT.Collections. # 202. 8) l.add(5).com .util. 10) l.add(15). 3) class Test 4) { 5) public static void main(String[] args) 6) { 7) ArrayList<Integer> l = new ArrayList<Integer>(). 14) Collections.Integer I2) 6) { 7) if(I1<I2) 8) { 9) return +1.println("Before Sorting:"+l).

9) } 10) } 11) class Test 12) { 13) public static void main(String[] args) 14) { 15) ArrayList<Integer> l = new ArrayList<Integer>().com . 18) l. 31) System. 30) l. 2 Floor.add(10).Collections. Hyderabad . 22) Collections.add(0). 17) l. 28) l. 21) System.500038. 24) } 25) } nd DURGASOFT. 32) } 33) } //Descending order Comparator Output: [20. 6  040 – 64 51 27 86.println("Before Sorting:"+l).Comparator.durgasoft. 92 46 21 21 43 | www.util.println(l).add(15). 19) l.add(20).new MyComparator()).util.util. # 202.add(0).ArrayList. 2) import java. 23) System.add(10). 15. 4) class MyComparator implements Comparator<Integer> 5) { 6) public int compare(Integer I1. HUDA Maitrivanam. 26) l. 27) l.add(5).out.add(15). Ameerpet. 10.println("After Sorting:"+l). 16) l.out.out. 5.add(20). 3) import java. Java 8 New Features in Simple Way 22) { 23) public static void main(String[] args) 24) { 25) TreeSet<Integer> l = new TreeSet<Integer>(new MyComparator()). 20) l.add(5).sort(l. 80 96 96 96 96. 29) l.Integer I2) 7) { 8) return (I1>I2)?-1:(I1<I2)?1:0. 0] Shortcut way: 1) import java.

10. This TreeSet object meant for default natural sorting order 2. 13) System. 7  040 – 64 51 27 86. Sorted Set In the case of Set. 2) import java.durgasoft. 5.sort(l.out. we can replace its implementation with Lambda Expression Collections.Collections. 5. Hyderabad .out.I2)->(I1<I2)?1:(I1>I2)?-1:0). 14) Collections. 15) System. 10) l. 92 46 21 21 43 | www.add(0).add(20). 0. TreeSet t = new TreeSet(Comparator c). Demo Program to Sort elements of ArrayList according to Customized Sorting Order By using Lambda Expressions(Descending Order): 1) import java. 11) l.util.add(10).sort(l. 15. 0] 2. 9) l.add(5). 8) l. 16) } 17) } Output: Before Sorting:[10.ArrayList. HUDA Maitrivanam. 20] After Sorting:[20. 3) class Test 4) { 5) public static void main(String[] args) 6) { 7) ArrayList<Integer> l= new ArrayList<Integer>().add(15). This TreeSet object meant for Customized Sorting Order nd DURGASOFT.println("Before Sorting:"+l). # 202. 12) l.(I1. Ameerpet.util. 80 96 96 96 96.500038.println("After Sorting:"+l). TreeSet t = new TreeSet().com . 2 Floor.I2)->(I1<I2)?1:(I1>I2)?-1:0).(I1. if we want Sorting order then we should go for TreeSet 1. 15. Java 8 New Features in Simple Way Sorting with Lambda Expressions: As Comparator is Functional interface.

TreeMap t = new TreeMap(Comparator c).add(5).add(15). This TreeMap object meant for Customized Sorting Order of keys nd DURGASOFT. 20.add(20).TreeSet.add(5). 8  040 – 64 51 27 86. 13) System. Java 8 New Features in Simple Way Demo Program for Default Natural Sorting Order(Ascending Order): 1) import java. if we want default natural sorting order of keys then we should go for TreeMap. 80 96 96 96 96.add(0). 14) } 15) } Output: [25. 15.add(10). 10) t. 12) t. 2) class Test 3) { 4) public static void main(String[] args) 5) { 6) TreeSet<Integer> t = new TreeSet<Integer>(). Sorted Map: In the case of Map. 8) t. 10. 11) t. This TreeMap object meant for default natural sorting order of keys 2.I2)->(I1>I2)?-1:(I1<I2)?1:0).util. Hyderabad .durgasoft.add(10).out. 1. 12) System. 2) class Test 3) { 4) public static void main(String[] args) 5) { 6) TreeSet<Integer> t = new TreeSet<Integer>((I1. 10.TreeSet. 15. 11) t.add(15). 9) t.out. 7) t. 20] Demo Program for Customized Sorting Order(Descending Order): 1) import java. 13) } 14) } Output: [0. # 202. 7) t. 2 Floor.println(t). 5. 0] 3. 8) t.println(t). Ameerpet. TreeMap m = new TreeMap(). 10) t. 9) t. 92 46 21 21 43 | www. HUDA Maitrivanam.500038.add(25).add(0).com .util.add(20). 5.

300=Bunny."Vinny"). 2 Floor.out."Pinny")."Chinny"). 200=Chinny."Durga"). 14) } 15) } Output: {100=Durga. 9) m."Sunny"). nd DURGASOFT.println(m). Ameerpet. 700=Vinny} Demo Program for Customized Sorting Order(Descending Order): 1) import java.com . 8) m.put(400. 14) } 15) } Output: {700=Vinny.util. # 202.put(700."Chinny").put(300. 10) m.put(100. 2) class Test 3) { 4) public static void main(String[] args) 5) { 6) TreeMap<Integer. 12) m.put(600.put(300.put(200.put(100. Hyderabad . 600=Sunny. 13) System.put(200.String>(). 9) m."Bunny"). 100=Durga} Sorting for Customized class objects by using Lambda Expressions: 1) import java.TreeMap. 9  040 – 64 51 27 86."Durga").put(600.util. 11) m. 80 96 96 96 96. 13) System. 600=Sunny.String> m = new TreeMap<Integer. HUDA Maitrivanam. 8) m.put(400.String>((I1. 400=Pinny.util. Java 8 New Features in Simple Way Demo Program for Default Natural Sorting Order(Ascending Order): 1) import java. 200=Chinny. 2) class Test 3) { 4) public static void main(String[] args) 5) { 6) TreeMap<Integer."Vinny"). 3) class Employee 4) { 5) int eno. 7) m. 2) import java. 92 46 21 21 43 | www. 400=Pinny.put(700.durgasoft."Bunny")."Sunny").500038.util.Collections.String> m = new TreeMap<Integer."Pinny").out.println(m). 6) String ename. 7) m. 12) m. 300=Bunny.I2)->(I1<I2)?1:(I1>I2)?- 1:0).TreeMap. 11) m. 10) m.ArrayList.

23) l. 22) l. 33) } 34) } Output: Before Sorting: [100:Katrina. 29) System.durgasoft. 500:Alia.add(new Employee(100. 92 46 21 21 43 | www."Alia"))."Deepika")).out.500038. 600:Kareena.add(new Employee(400. 32) System."Sunny")).add(new Employee(300. Ameerpet.eno>e2. Java 8 New Features in Simple Way 7) Employee(int eno. 400:Sunny. 300:Mallika] After Sorting: [100:Katrina.println("Before Sorting:").com . 11) } 12) public String toString() 13) { 14) return eno+":"+ename."Kareena")).eno=eno.println(l). 400:Sunny. 10  040 – 64 51 27 86.println(l). 15) } 16) } 17) class Test 18) { 19) public static void main(String[] args) 20) { 21) ArrayList<Employee> l= new ArrayList<Employee>(). 10) this. 600:Kareena] nd DURGASOFT.eno<e2.add(new Employee(200. 300:Mallika.eno)?1:0).out. 80 96 96 96 96. 31) System. 28) System.e2)->(e1. 24) l. HUDA Maitrivanam.out. 2 Floor. Hyderabad ."Katrina")).(e1. 27) l.println("After Sorting:").ename=ename.add(new Employee(500. 500:Alia. 30) Collections. 200:Deepika. 26) l. 200:Deepika.eno)?-1:(e1.String ename) 8) { 9) this.add(new Employee(600.out."Mallika")). 25) l. # 202.sort(l.

gl/VmhM7t nd DURGASOFT. Oracle Java Certification: Shortest Way To Crack OCA 1Z0-808 Link: https://goo.durgasoft. Java 8 New Features In Simple Way Link: https://goo.gl/F2NfZi 3. # 202. Complete JDBC Programming Part-1 Link: https://goo. JPMS and More Link: https://goo.gl/uT9sav 5. 11  040 – 64 51 27 86. Ameerpet.gl/vcMKjz 2.com .500038. 2 Floor. HUDA Maitrivanam. Hyderabad . Complete JDBC Programming Part-2 Link: https://goo.gl/s9PP1p 4. 80 96 96 96 96. Java 8 New Features in Simple Way 1. 92 46 21 21 43 | www. Java 9 New Features In Simple Way: JShell.