Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java-training
Overview
Overview
Details
Activity
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
shuhu.hou
java-training
Commits
b16f7cb9
You need to sign in or sign up before continuing.
Commit
b16f7cb9
authored
Oct 29, 2018
by
陈文顺
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
java 8 Stream
parent
0cc3d288
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
84 additions
and
17 deletions
+84
-17
demo1/src/main/java/cn/freemud/demo/po/User.java
+16
-0
demo1/src/main/resources/logback.xml
+0
-12
demo1/src/test/java/cn/freemud/demo/GuavaTests.java
+68
-5
No files found.
demo1/src/main/java/cn/freemud/demo/po/User.java
View file @
b16f7cb9
...
...
@@ -11,11 +11,19 @@ public class User {
private
String
username
;
Integer
age
;
public
User
(
Long
id
,
String
username
)
{
this
.
id
=
id
;
this
.
username
=
username
;
}
public
User
(
Long
id
,
String
username
,
Integer
age
)
{
this
.
id
=
id
;
this
.
username
=
username
;
this
.
age
=
age
;
}
public
Long
getId
()
{
return
id
;
}
...
...
@@ -32,6 +40,14 @@ public class User {
this
.
username
=
username
;
}
public
Integer
getAge
()
{
return
age
;
}
public
void
setAge
(
Integer
age
)
{
this
.
age
=
age
;
}
@Override
public
String
toString
()
{
return
JSON
.
toJSONString
(
this
);
...
...
demo1/src/main/resources/logback.xml
View file @
b16f7cb9
...
...
@@ -11,21 +11,10 @@
</encoder>
</appender>
<!--<appender name="FREEMUD_API" class="ch.qos.logback.core.rolling.RollingFileAppender">-->
<!--<file>/data/log/mall/order/order.log</file>-->
<!--<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">-->
<!--<fileNamePattern>/data/log/mall/order/order-%d{yyyy-MM-dd}.%i.log</fileNamePattern>-->
<!--<maxFileSize>10MB</maxFileSize>-->
<!--</rollingPolicy>-->
<!--<encoder>-->
<!--<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>-->
<!--</encoder>-->
<!--</appender>-->
<root
level=
"info"
>
<appender-ref
ref=
"STDOUT"
/>
<!--<appender-ref ref="FREEMUD_API"/>-->
</root>
</configuration>
\ No newline at end of file
demo1/src/test/java/cn/freemud/demo/GuavaTests.java
View file @
b16f7cb9
package
cn
.
freemud
.
demo
;
import
cn.freemud.demo.po.User
;
import
cn.freemud.demo.zookeeper.Q
;
import
com.google.common.base.Function
;
import
com.google.common.base.Joiner
;
import
com.google.common.base.Splitter
;
...
...
@@ -10,10 +11,10 @@ import org.junit.Test;
import
java.text.DateFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.
Collection
;
import
java.util.
Collections
;
import
java.util.
Date
;
import
java.util.
List
;
import
java.util.
*
;
import
java.util.
function.Supplier
;
import
java.util.
stream.Collectors
;
import
java.util.
stream.Stream
;
/**
* Created by chenwenshun on 2018/9/27.
...
...
@@ -124,6 +125,7 @@ public class GuavaTests {
List
<
User
>
userList
=
index
.
get
(
"tony"
);
System
.
out
.
println
(
userList
);
}
...
...
@@ -131,15 +133,76 @@ public class GuavaTests {
* 集合运算主要有:并集、交集、差集。
*/
@Test
public
void
test
51
()
{
public
void
test
6
()
{
List
<
Integer
>
list1
=
Lists
.
newArrayList
(
1
,
2
,
2
,
1
,
4
,
5
,
4
,
3
);
List
<
Integer
>
list2
=
Lists
.
newArrayList
(
1
,
2
,
3
,
7
,
8
,
9
);
Set
set
=
new
HashSet
(
list1
);
Sets
.
SetView
<
Integer
>
union
=
Sets
.
union
(
Sets
.
newHashSet
(
list1
),
Sets
.
newHashSet
(
list2
));
Sets
.
SetView
<
Integer
>
difference
=
Sets
.
difference
(
Sets
.
newHashSet
(
list1
),
Sets
.
newHashSet
(
list2
));
Sets
.
SetView
<
Integer
>
intersection
=
Sets
.
intersection
(
Sets
.
newHashSet
(
list1
),
Sets
.
newHashSet
(
list2
));
System
.
out
.
println
(
union
);
System
.
out
.
println
(
difference
);
System
.
out
.
println
(
intersection
);
list1
.
parallelStream
().
forEach
(
System
.
out
::
println
);
list1
.
stream
().
forEach
(
System
.
out
::
println
);
int
a
=
list1
.
stream
().
max
(
Comparator
.
comparing
(
e
->
e
)).
get
();
int
sum
=
list1
.
stream
().
mapToInt
(
e
->
e
).
sum
();
System
.
out
.
println
(
sum
);
}
/**
* java 8 Stream
*/
@Test
public
void
test7
(){
List
<
User
>
users
=
Lists
.
newArrayList
();
users
.
add
(
new
User
(
1L
,
"haha"
));
users
.
add
(
new
User
(
2L
,
"bella"
));
users
.
add
(
new
User
(
3L
,
"tina"
));
users
.
add
(
new
User
(
4L
,
"tom"
));
users
.
add
(
new
User
(
5L
,
"jerry"
));
users
.
add
(
new
User
(
6L
,
"tony"
));
users
.
add
(
new
User
(
7L
,
"tony"
));
List
<
Q
>
names
=
users
.
parallelStream
()
// .filter(u -> u.getUsername().equals("tony"))
// .sorted(Comparator.comparing(User::getId).reversed())
.
sorted
((
user1
,
user2
)
->
user1
.
getId
().
compareTo
(
user2
.
getId
()))
.
map
(
user
->
{
Q
q
=
new
Q
(
user
.
getId
().
intValue
());
return
q
;
})
.
collect
(
Collectors
.
toList
());
System
.
out
.
println
(
names
);
Map
<
Long
,
User
>
userMap
=
users
.
stream
().
collect
(
Collectors
.
toMap
(
user
->
user
.
getId
(),
user
->
user
))
;
System
.
out
.
println
(
userMap
);
/**
* 非唯一索引
*/
Map
<
Integer
,
List
<
User
>>
integerListMap
=
Stream
.
generate
(
new
UserSupplier
())
.
limit
(
100
)
.
collect
(
Collectors
.
groupingBy
(
User:
:
getAge
));
System
.
out
.
println
(
integerListMap
);
}
private
class
UserSupplier
implements
Supplier
<
User
>{
private
long
index
=
0
;
private
Random
random
=
new
Random
();
@Override
public
User
get
()
{
return
new
User
(
index
++,
"StormTestUser"
+
random
.
nextInt
(
1000
),
random
.
nextInt
(
100
));
}
}
...
...
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