File tree Expand file tree Collapse file tree 6 files changed +229
-0
lines changed
src/main/java/com/javaprogramto/java8 Expand file tree Collapse file tree 6 files changed +229
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .javaprogramto .java8 .listoflists ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .List ;
5
+ import java .util .stream .Collectors ;
6
+
7
+ public class ListOfListsIntegers {
8
+
9
+ public static void main (String [] args ) {
10
+
11
+ List <List <Integer >> listOfLists = new ArrayList <>();
12
+
13
+ List <Integer > innerList1 = new ArrayList <>();
14
+
15
+ innerList1 .add (1 );
16
+ innerList1 .add (2 );
17
+ innerList1 .add (3 );
18
+
19
+ List <Integer > innerList2 = new ArrayList <>();
20
+
21
+ innerList2 .add (4 );
22
+ innerList2 .add (5 );
23
+ innerList2 .add (6 );
24
+
25
+ listOfLists .add (innerList1 );
26
+ listOfLists .add (innerList2 );
27
+
28
+ // System.out.println("Integer List of lists - "+listOfLists);
29
+
30
+ for (List <Integer > list : listOfLists ) {
31
+
32
+ for (Integer i : list ) {
33
+ System .out .println (i );
34
+ }
35
+ }
36
+
37
+ listOfLists .forEach (
38
+ (List <Integer > innerList ) ->
39
+ innerList .forEach ((Integer item ) -> System .out .println (item )
40
+ )
41
+ );
42
+
43
+ List <Integer > ints = listOfLists .stream ()
44
+ .flatMap (list -> list .stream ())
45
+ .collect (Collectors .toList ());
46
+ System .out .println ("flat map ints - " +ints );
47
+
48
+ }
49
+
50
+ }
Original file line number Diff line number Diff line change
1
+ package com .javaprogramto .java8 .listoflists ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .List ;
5
+ import java .util .stream .Collectors ;
6
+
7
+ public class ListOfListsStrings {
8
+
9
+ public static void main (String [] args ) {
10
+
11
+ List <List <String >> listOfLists = new ArrayList <>();
12
+
13
+ List <String > innerList1 = new ArrayList <>();
14
+
15
+ innerList1 .add ("A" );
16
+ innerList1 .add ("B" );
17
+ innerList1 .add ("C" );
18
+
19
+ List <String > innerList2 = new ArrayList <>();
20
+
21
+ innerList2 .add ("D" );
22
+ innerList2 .add ("E" );
23
+ innerList2 .add ("F" );
24
+
25
+ listOfLists .add (innerList1 );
26
+ listOfLists .add (innerList2 );
27
+
28
+ // System.out.println("List of lists - "+listOfLists);
29
+
30
+ for (List <String > list : listOfLists ) {
31
+
32
+ for (String letter : list ) {
33
+ System .out .println (letter );
34
+ }
35
+ }
36
+
37
+ listOfLists .forEach (
38
+ (List <String > innerList ) ->
39
+ innerList .forEach ((String item ) -> System .out .println (item ))
40
+ );
41
+
42
+
43
+ List <String > strings = listOfLists .stream ()
44
+ .flatMap (list -> list .stream ())
45
+ .collect (Collectors .toList ());
46
+ System .out .println ("flat map strings - " +strings );
47
+ }
48
+ }
Original file line number Diff line number Diff line change
1
+ package com .javaprogramto .java8 .set .tostring ;
2
+
3
+ import java .util .HashSet ;
4
+ import java .util .Set ;
5
+
6
+ import org .apache .commons .lang3 .StringUtils ;
7
+
8
+ public class ApacheCommonsSetToStringExample {
9
+
10
+ public static void main (String [] args ) {
11
+
12
+ Set <String > hashSet = new HashSet <>();
13
+
14
+ hashSet .add ("list" );
15
+ hashSet .add ("set" );
16
+ hashSet .add ("map" );
17
+
18
+ String str1 = StringUtils .join (hashSet );
19
+
20
+ System .out .println ("with default delimiter" );
21
+ System .out .println ("hashset in string - " + str1 );
22
+
23
+ String str2 = StringUtils .join (hashSet , ":" );
24
+
25
+ System .out .println ("with custom delimiter" );
26
+ System .out .println ("hashset in string - " + str2 );
27
+
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ package com .javaprogramto .java8 .set .tostring ;
2
+
3
+ import java .util .HashSet ;
4
+ import java .util .LinkedHashSet ;
5
+ import java .util .Set ;
6
+
7
+ public class Java8SetToStringExample1 {
8
+
9
+ public static void main (String [] args ) {
10
+
11
+ Set <String > hashSet = new HashSet <>();
12
+
13
+ hashSet .add ("java" );
14
+ hashSet .add ("c++" );
15
+ hashSet .add ("python" );
16
+
17
+ String str1 = String .join (" " , hashSet );
18
+
19
+ System .out .println ("String.join() hashset in string - " + str1 );
20
+
21
+ Set <String > linkedHashSet = new LinkedHashSet <>();
22
+
23
+ linkedHashSet .add ("javascript" );
24
+ linkedHashSet .add ("typescript" );
25
+
26
+ String str2 = String .join (" " , linkedHashSet );
27
+ System .out .println ("String.join() linkedhashset in string - " + str2 );
28
+
29
+ System .out .println ("With different delimiter" );
30
+ String str3 = String .join ("**" , hashSet );
31
+
32
+ System .out .println ("String.join() hashset to string - " + str3 );
33
+
34
+ String str4 = String .join ("^^" , linkedHashSet );
35
+ System .out .println ("String.join() linkedhashset to string - " + str4 );
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ package com .javaprogramto .java8 .set .tostring ;
2
+
3
+ import java .util .HashSet ;
4
+ import java .util .LinkedHashSet ;
5
+ import java .util .Set ;
6
+ import java .util .stream .Collectors ;
7
+
8
+ public class Java8SetToStringExample2 {
9
+
10
+ public static void main (String [] args ) {
11
+
12
+ Set <String > hashSet = new HashSet <>();
13
+
14
+ hashSet .add ("java" );
15
+ hashSet .add ("c++" );
16
+ hashSet .add ("python" );
17
+
18
+ String str1 = hashSet .stream ().collect (Collectors .joining (":" , "{" , "}" ));
19
+
20
+
21
+ System .out .println ("Collectors.joining() hashset in string - " + str1 );
22
+
23
+ Set <String > linkedHashSet = new LinkedHashSet <>();
24
+
25
+ linkedHashSet .add ("javascript" );
26
+ linkedHashSet .add ("typescript" );
27
+
28
+ String str2 = linkedHashSet .stream ().collect (Collectors .joining (":" , "{" , "}" ));
29
+ System .out .println ("Collectors.joining() linkedhashset in string - " + str2 );
30
+
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ package com .javaprogramto .java8 .set .tostring ;
2
+
3
+ import java .util .HashSet ;
4
+ import java .util .LinkedHashSet ;
5
+ import java .util .Set ;
6
+
7
+ public class SetToStringExample {
8
+
9
+ public static void main (String [] args ) {
10
+
11
+ Set <String > hashSet = new HashSet <>();
12
+
13
+ hashSet .add ("java" );
14
+ hashSet .add ("c++" );
15
+ hashSet .add ("python" );
16
+
17
+ String str1 = hashSet .toString ();
18
+ str1 = str1 .replaceAll ("\\ ,|\\ [|\\ ]|\\ s" , " " );
19
+
20
+ System .out .println ("hashset in string - " + str1 );
21
+
22
+ Set <String > linkedHashSet = new LinkedHashSet <>();
23
+
24
+ linkedHashSet .add ("javascript" );
25
+ linkedHashSet .add ("typescript" );
26
+
27
+ String str2 = linkedHashSet .toString ();
28
+ str2 = str2 .replaceAll ("\\ ,|\\ [|\\ ]|\\ s" , " " );
29
+ System .out .println ("linkedhashset in string - " + str2 );
30
+
31
+ }
32
+
33
+ }
You can’t perform that action at this time.
0 commit comments