Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
Java8Demo
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Pinky Sabu
Java8Demo
Commits
a600359b
Commit
a600359b
authored
Feb 07, 2023
by
Pinky Sabu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Java 8 POCS [Pinky Sabu]
parent
d2373eec
Changes
14
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
242 additions
and
0 deletions
+242
-0
BinaryOperatorExamples.class
build/classes/java/main/BinaryOperatorExamples.class
+0
-0
ConsumerExamples.class
build/classes/java/main/ConsumerExamples.class
+0
-0
FunctionExamples.class
build/classes/java/main/FunctionExamples.class
+0
-0
PredicateExamples.class
build/classes/java/main/PredicateExamples.class
+0
-0
SupplierExamples.class
build/classes/java/main/SupplierExamples.class
+0
-0
UnaryOPeratorExamples.class
build/classes/java/main/UnaryOPeratorExamples.class
+0
-0
Domain.class
build/classes/java/main/domain/Domain.class
+0
-0
BinaryOperatorExamples.java
src/main/java/BinaryOperatorExamples.java
+28
-0
ConsumerExamples.java
src/main/java/ConsumerExamples.java
+32
-0
FunctionExamples.java
src/main/java/FunctionExamples.java
+40
-0
PredicateExamples.java
src/main/java/PredicateExamples.java
+71
-0
SupplierExamples.java
src/main/java/SupplierExamples.java
+33
-0
UnaryOPeratorExamples.java
src/main/java/UnaryOPeratorExamples.java
+21
-0
Domain.java
src/main/java/domain/Domain.java
+17
-0
No files found.
build/classes/java/main/BinaryOperatorExamples.class
0 → 100644
View file @
a600359b
File added
build/classes/java/main/ConsumerExamples.class
0 → 100644
View file @
a600359b
File added
build/classes/java/main/FunctionExamples.class
0 → 100644
View file @
a600359b
File added
build/classes/java/main/PredicateExamples.class
0 → 100644
View file @
a600359b
File added
build/classes/java/main/SupplierExamples.class
0 → 100644
View file @
a600359b
File added
build/classes/java/main/UnaryOPeratorExamples.class
0 → 100644
View file @
a600359b
File added
build/classes/java/main/domain/Domain.class
0 → 100644
View file @
a600359b
File added
src/main/java/BinaryOperatorExamples.java
0 → 100644
View file @
a600359b
import
domain.Domain
;
import
java.util.Arrays
;
import
java.util.Comparator
;
import
java.util.List
;
import
java.util.function.BiFunction
;
import
java.util.function.BinaryOperator
;
import
java.util.stream.Collectors
;
public
class
BinaryOperatorExamples
{
public
static
void
main
(
String
[]
args
)
{
biFunction
();
binaryOperator
();
}
private
static
void
binaryOperator
()
{
BinaryOperator
<
Integer
>
binary
=
(
x
,
y
)
->
x
+
y
;
binary
.
apply
(
2
,
3
);
}
private
static
void
biFunction
()
{
BiFunction
<
Integer
,
Integer
,
Integer
>
result
=
(
x
,
y
)
->
x
+
y
;
System
.
out
.
println
(
result
.
apply
(
2
,
3
));
}
}
src/main/java/ConsumerExamples.java
0 → 100644
View file @
a600359b
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.function.BiConsumer
;
import
java.util.function.Consumer
;
public
class
ConsumerExamples
{
public
static
void
main
(
String
[]
args
)
{
simpleConsumer
();
biConsumer
();
inbuiltBiConsumer
();
}
private
static
void
inbuiltBiConsumer
()
{
Map
<
String
,
String
>
map
=
new
HashMap
<>();
map
.
put
(
"1"
,
"1"
);
map
.
put
(
"2"
,
"2"
);
map
.
forEach
((
x
,
y
)->
System
.
out
.
println
(
"inbuiltBiConsumer"
+
x
.
concat
(
y
)));
}
private
static
void
biConsumer
()
{
BiConsumer
<
String
,
String
>
biConsumer
=
(
x
,
y
)
->
x
.
concat
(
y
);
System
.
out
.
println
(
biConsumer
);
}
private
static
void
simpleConsumer
()
{
Consumer
<
String
>
consumer
=
x
->
System
.
out
.
println
(
x
);
consumer
.
accept
(
"Hello "
);
}
}
src/main/java/FunctionExamples.java
0 → 100644
View file @
a600359b
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.StringJoiner
;
import
java.util.function.BiFunction
;
import
java.util.function.Function
;
public
class
FunctionExamples
{
public
static
void
main
(
String
[]
args
)
{
simpleFunction
();
chainFunction
();
biFunction
();
biFunctionWithFunction
();
}
private
static
void
biFunctionWithFunction
()
{
BiFunction
<
Integer
,
Integer
,
Integer
>
biFunction
=
(
x
,
y
)
->
x
+
y
;
Function
<
Integer
,
String
>
function
=
x
->
"Hello"
.
concat
(
String
.
valueOf
(
x
));
System
.
out
.
println
(
" biFunctionWithFunction :"
+
biFunction
.
andThen
(
function
).
apply
(
2
,
3
));
}
private
static
void
biFunction
()
{
BiFunction
<
Integer
,
Integer
,
List
<
Integer
>>
biFunction
=
(
x
,
y
)
->
Arrays
.
asList
(
x
+
y
);
System
.
out
.
println
(
biFunction
.
apply
(
2
,
3
));
}
private
static
void
chainFunction
()
{
Function
<
String
,
Integer
>
function1
=
x
->
x
.
length
();
Function
<
Integer
,
Integer
>
function2
=
x
->
x
*
3
;
System
.
out
.
println
(
function1
.
andThen
(
function2
).
apply
(
"Hello Test"
));
}
private
static
void
simpleFunction
()
{
Function
<
String
,
Integer
>
function
=
x
->
x
.
length
();
System
.
out
.
println
(
function
.
apply
(
"Test"
));
}
}
src/main/java/PredicateExamples.java
0 → 100644
View file @
a600359b
import
domain.Domain
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.function.BiPredicate
;
import
java.util.function.Predicate
;
import
java.util.stream.Collectors
;
public
class
PredicateExamples
{
public
static
void
main
(
String
[]
args
)
{
predicate
();
predicateAnd
();
predicateOr
();
predicateNegate
();
predicateChaining
();
biPredicate
();
biPredicateAsFunctionArgument
();
}
private
static
void
biPredicateAsFunctionArgument
()
{
List
<
Domain
>
domainList
=
Arrays
.
asList
(
new
Domain
(
"google.com"
,
1
),
new
Domain
(
"i-am-spammer.com"
,
10
),
new
Domain
(
"mkyong.com"
,
0
),
new
Domain
(
"microsoft.com"
,
2
));
BiPredicate
<
String
,
Integer
>
biPredicate
=
(
x
,
y
)
->
{
return
(
x
.
equals
(
"google.com"
)
||
y
==
0
);
};
System
.
out
.
println
(
domainList
.
stream
()
.
filter
(
domain
->
biPredicate
.
test
(
domain
.
getName
(),
domain
.
getScore
()))
.
collect
(
Collectors
.
toList
()));
}
private
static
void
biPredicate
()
{
BiPredicate
<
String
,
Integer
>
biPredicate
=
(
x
,
y
)
->
x
.
length
()
==
y
;
System
.
out
.
println
(
biPredicate
.
test
(
"Bat"
,
3
));
}
private
static
void
predicateChaining
()
{
Predicate
<
String
>
startsWithA
=
x
->
x
.
startsWith
(
"A"
);
Predicate
<
String
>
startsWithB
=
x
->
x
.
startsWith
(
"B"
);
System
.
out
.
println
(
startsWithA
.
or
(
startsWithB
).
test
(
"Bat"
));
}
private
static
void
predicateNegate
()
{
List
<
String
>
list
=
Arrays
.
asList
(
"A"
,
"AA"
,
"AAA"
,
"B"
,
"BB"
,
"BBB"
);
Predicate
<
String
>
startsWith
=
x
->
x
.
startsWith
(
"A"
);
System
.
out
.
println
(
list
.
stream
().
filter
(
startsWith
.
negate
()).
collect
(
Collectors
.
toList
()));
}
private
static
void
predicateOr
()
{
List
<
String
>
list
=
Arrays
.
asList
(
"A"
,
"AA"
,
"AAA"
,
"B"
,
"BB"
,
"BBB"
);
Predicate
<
String
>
startsWith
=
x
->
x
.
startsWith
(
"A"
);
Predicate
<
String
>
length
=
x
->
x
.
length
()
>
2
;
System
.
out
.
println
(
list
.
stream
().
filter
(
startsWith
.
or
(
length
)).
collect
(
Collectors
.
toList
()));
}
private
static
void
predicate
()
{
List
<
Integer
>
list
=
Arrays
.
asList
(
1
,
2
,
3
,
4
,
5
,
6
,
7
);
Predicate
<
Integer
>
noGreaterThan
=
x
->
x
>
5
;
System
.
out
.
println
(
">>"
+
list
.
stream
().
filter
(
noGreaterThan
).
sorted
().
collect
(
Collectors
.
toList
()));
}
private
static
void
predicateAnd
()
{
List
<
Integer
>
list
=
Arrays
.
asList
(
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
,
13
,
14
,
15
,
16
,
17
,
18
,
19
);
Predicate
<
Integer
>
greaterThan
=
x
->
x
>
10
;
Predicate
<
Integer
>
lessThan
=
x
->
x
<
18
;
System
.
out
.
println
(
list
.
stream
().
filter
(
greaterThan
.
and
(
lessThan
)).
collect
(
Collectors
.
toList
()));
}
}
src/main/java/SupplierExamples.java
0 → 100644
View file @
a600359b
import
domain.Domain
;
import
java.time.LocalDateTime
;
import
java.util.Optional
;
import
java.util.function.Supplier
;
public
class
SupplierExamples
{
public
static
void
main
(
String
[]
args
)
{
simpleExample
();
supplierToReturnObject
();
}
private
static
void
supplierToReturnObject
()
{
System
.
out
.
println
(
factory
(
Domain:
:
new
));
System
.
out
.
println
(
factory
(()
->
new
Domain
(
"hi"
,
10
)));
}
private
static
Domain
factory
(
Supplier
<?
extends
Domain
>
s
)
{
Domain
domain
=
s
.
get
();
if
(!
Optional
.
ofNullable
(
domain
.
getName
()).
isPresent
())
{
domain
.
setName
(
"Hello"
);
domain
.
setScore
(
100
);
}
return
domain
;
}
private
static
void
simpleExample
()
{
Supplier
<
LocalDateTime
>
supplier
=
()
->
LocalDateTime
.
now
();
System
.
out
.
println
(
supplier
.
get
());
}
}
src/main/java/UnaryOPeratorExamples.java
0 → 100644
View file @
a600359b
import
java.util.function.Function
;
import
java.util.function.UnaryOperator
;
public
class
UnaryOPeratorExamples
{
public
static
void
main
(
String
[]
args
)
{
function
();
unaryOperator
();
}
private
static
void
unaryOperator
()
{
UnaryOperator
<
Integer
>
data
=
x
->
x
*
2
;
System
.
out
.
println
(
data
.
apply
(
2
));
}
private
static
void
function
()
{
Function
<
Integer
,
Integer
>
function
=
x
->
x
*
2
;
System
.
out
.
println
(
function
.
apply
(
2
));
}
}
src/main/java/domain/Domain.java
0 → 100644
View file @
a600359b
package
domain
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.Getter
;
import
lombok.NoArgsConstructor
;
import
lombok.Setter
;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public
class
Domain
{
String
name
;
Integer
score
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment