Commit f81ee5c2 by 景园园

Merge branch 'master' of http://gitlab.freemud.com/yuanyuan.jing/java-training into 1l-jingyuanyuan

parents bb0a122d b16f7cb9
...@@ -11,11 +11,19 @@ public class User { ...@@ -11,11 +11,19 @@ public class User {
private String username; private String username;
Integer age;
public User(Long id, String username) { public User(Long id, String username) {
this.id = id; this.id = id;
this.username = username; this.username = username;
} }
public User(Long id, String username, Integer age) {
this.id = id;
this.username = username;
this.age = age;
}
public Long getId() { public Long getId() {
return id; return id;
} }
...@@ -32,6 +40,14 @@ public class User { ...@@ -32,6 +40,14 @@ public class User {
this.username = username; this.username = username;
} }
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override @Override
public String toString() { public String toString() {
return JSON.toJSONString(this); return JSON.toJSONString(this);
......
...@@ -11,21 +11,10 @@ ...@@ -11,21 +11,10 @@
</encoder> </encoder>
</appender> </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"> <root level="info">
<appender-ref ref="STDOUT"/> <appender-ref ref="STDOUT"/>
<!--<appender-ref ref="FREEMUD_API"/>-->
</root> </root>
</configuration> </configuration>
\ No newline at end of file
package cn.freemud.demo; package cn.freemud.demo;
import cn.freemud.demo.po.User; import cn.freemud.demo.po.User;
import cn.freemud.demo.zookeeper.Q;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
...@@ -10,10 +11,10 @@ import org.junit.Test; ...@@ -10,10 +11,10 @@ import org.junit.Test;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Collection; import java.util.*;
import java.util.Collections; import java.util.function.Supplier;
import java.util.Date; import java.util.stream.Collectors;
import java.util.List; import java.util.stream.Stream;
/** /**
* Created by chenwenshun on 2018/9/27. * Created by chenwenshun on 2018/9/27.
...@@ -124,6 +125,7 @@ public class GuavaTests { ...@@ -124,6 +125,7 @@ public class GuavaTests {
List<User> userList = index.get("tony"); List<User> userList = index.get("tony");
System.out.println(userList); System.out.println(userList);
} }
...@@ -131,15 +133,76 @@ public class GuavaTests { ...@@ -131,15 +133,76 @@ public class GuavaTests {
* 集合运算主要有:并集、交集、差集。 * 集合运算主要有:并集、交集、差集。
*/ */
@Test @Test
public void test51() { public void test6() {
List<Integer> list1 = Lists.newArrayList(1, 2, 2, 1, 4, 5, 4, 3); List<Integer> list1 = Lists.newArrayList(1, 2, 2, 1, 4, 5, 4, 3);
List<Integer> list2 = Lists.newArrayList(1, 2, 3, 7, 8, 9); 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> union = Sets.union(Sets.newHashSet(list1), Sets.newHashSet(list2));
Sets.SetView<Integer> difference = Sets.difference(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)); Sets.SetView<Integer> intersection = Sets.intersection(Sets.newHashSet(list1), Sets.newHashSet(list2));
System.out.println(union); System.out.println(union);
System.out.println(difference); System.out.println(difference);
System.out.println(intersection); 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));
}
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment