Skip to content

Commit 8368a4c

Browse files
author
wangyibing
committed
add my practice
1 parent c92f84a commit 8368a4c

File tree

16 files changed

+377
-0
lines changed

16 files changed

+377
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.github.yibing.spring.strategy;
2+
3+
public class Context {
4+
private Strategy strategy;
5+
6+
public Context(Strategy strategy) {
7+
this.strategy = strategy;
8+
}
9+
10+
public int executeStrategy(int num1, int num2) {
11+
return strategy.doOperation(num1, num2);
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.github.yibing.spring.strategy;
2+
3+
public class OperationAdd implements Strategy {
4+
@Override
5+
public int doOperation(int num1, int num2) {
6+
return num1 + num2;
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.github.yibing.spring.strategy;
2+
3+
public class OperationMultiply implements Strategy {
4+
@Override
5+
public int doOperation(int num1, int num2) {
6+
return num1 * num2;
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.github.yibing.spring.strategy;
2+
3+
public class OperationSubtract implements Strategy {
4+
@Override
5+
public int doOperation(int num1, int num2) {
6+
return num1 + num2;
7+
}
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.github.yibing.spring.strategy;
2+
3+
public interface Strategy {
4+
public int doOperation(int num1, int num2);
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.github.yibing.spring.strategy;
2+
3+
public class StrategyDemo {
4+
public static void main(String[] args) {
5+
Context context = new Context(new OperationAdd());
6+
System.out.println("10+6=" + context.executeStrategy(10, 6));
7+
context = new Context(new OperationSubtract());
8+
System.out.println("10-6=" + context.executeStrategy(10, 6));
9+
}
10+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package org.github.yibing.spring.stream;
2+
3+
import org.junit.Test;
4+
5+
import java.util.*;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.Stream;
8+
9+
public class StreamDemo2 {
10+
static List<Employee> emps = Arrays.asList(new Employee(101, "张三", 28, 9999d),
11+
new Employee(101, "李四", 49, 666d),
12+
new Employee(102, "王五", 38, 333d),
13+
new Employee(103, "赵六", 12, 7777d),
14+
new Employee(104, "田七", 6, 222d)
15+
);
16+
17+
public static void main(String[] args) {
18+
/*
19+
* 创建stream的三种方式
20+
*/
21+
//1.集合 list.stream(),
22+
Stream<Employee> stream = emps.stream();
23+
Employee[] array = (Employee[]) emps.toArray();
24+
// 2.数组 Arrays.stream(array)
25+
Stream<Object> stream1 = Arrays.stream(array);
26+
// 3.Stream.of(T)
27+
Stream<List<Employee>> stream2 = Stream.of(StreamDemo2.emps);
28+
29+
30+
}
31+
32+
/**
33+
* 中间操作:
34+
* 筛选 filter()
35+
* 去重 distinct()
36+
* 取前面几条/跳过前面几条 limit()/skip()
37+
*/
38+
39+
@Test
40+
public void test1() {
41+
//中间操作------筛选
42+
emps.stream().filter((e) -> e.getAge() > 30).forEach(e -> System.out.println(e.getAge()));
43+
// 取前面几条
44+
emps.stream().filter(e -> e.getId() > 101).limit(3).forEach(System.out::println);
45+
// 需要重写hashcode()和equals 才能去重
46+
emps.stream().distinct().forEach(System.out::println);
47+
}
48+
49+
/**
50+
* 中间操作:
51+
* 映射,把集合中每个元素按照function映射为对应的类型
52+
*/
53+
@Test
54+
public void test2() {
55+
// 把每个元素按照function转换成相应类型,输出 还有mapToInteger,mapToLong等方法
56+
emps.stream().mapToDouble(Employee::getId).forEach(System.out::println);
57+
List<String> strList = Arrays.asList("a", "b", "c", "d");
58+
/*
59+
flapMap和map,传入同一个返回流的function,flatMap可以把所有流连成一个流
60+
*/
61+
Stream<Character> rStream = strList.stream().flatMap(StreamDemo2::filterCharacter);
62+
rStream.forEach(System.out::println);
63+
64+
Stream<Stream<Character>> stream = strList.stream().map(StreamDemo2::filterCharacter);
65+
stream.forEach(sm -> sm.forEach(System.out::print));
66+
}
67+
68+
public static Stream<Character> filterCharacter(String str) {
69+
List<Character> list = new ArrayList<>();
70+
for (Character ch : str.toCharArray()) {
71+
list.add(ch);
72+
}
73+
return list.stream();
74+
}
75+
76+
/**
77+
* 中间操作:
78+
* sorted() 默认顺序排序,引用类型要实现Comparable
79+
* sorted(Comparator comp) 带参数,指定排序规则
80+
*/
81+
@Test
82+
public void test3() {
83+
// 把每个emps映射为属性:name,组成一个新流,然后按name排序输出
84+
emps.stream().map(Employee::getName).sorted().forEach(System.out::println);
85+
// 带参数的sorted,按照年龄排序,若相等就比较名字
86+
emps.stream().sorted((x, y) -> {
87+
if (x.getAge() == y.getAge()) {
88+
return x.getName().compareTo(y.getName());
89+
} else {
90+
return Integer.compare(x.getAge(), y.getAge());
91+
}
92+
}).forEach(System.out::println);
93+
}
94+
95+
96+
/**
97+
* 终止操作: 查找与匹配,返回boolean
98+
* allMatch(),所有元素都匹配才返回true
99+
* anyMatch(),只要有一个或多个元素匹配就返回true
100+
* noneMatch(),都不匹配就返回true,否则false
101+
*/
102+
@Test
103+
public void test4() {
104+
// 每个员工的名字都包含三 ,才返回true
105+
boolean b = emps.stream().allMatch(e -> e.getName().contains("三"));
106+
System.out.println(b);
107+
boolean b1 = emps.stream().noneMatch(e -> e.getName().contains("九"));
108+
System.out.println(b1);
109+
110+
}
111+
112+
/**
113+
* 终止操作:查找与匹配:返回元素
114+
* findFirst:返回第一个Optional<>
115+
* findAny:返回任意一个元素Optional<>
116+
* count 返回元素个数
117+
* max 返回最大值
118+
* min 返回最小值
119+
*/
120+
121+
@Test
122+
public void test5() {
123+
Optional<Employee> first = emps.stream().findFirst();
124+
System.out.println(first.get());
125+
long count = emps.stream().count();
126+
System.out.println(count);
127+
}
128+
129+
130+
/**
131+
* 终止操作:规约reduce,将每个元素结合起来,得到一个值
132+
* reduce(BinaryOperator b)
133+
* reduce(T iden,BinaryOperator b) 返回Optional
134+
*/
135+
@Test
136+
public void test6() {
137+
// 规约,先转化成年龄集合,然后给他们求和
138+
Optional<Integer> sum = emps.stream().map(Employee::getAge).reduce(Integer::sum);
139+
System.out.println(sum.get());
140+
// 从 1 相加,相当于该集合多了一个 1求和
141+
Integer sum1 = emps.stream().map(Employee::getAge).reduce(1, Integer::sum);
142+
System.out.println(sum1);
143+
}
144+
145+
/**
146+
* 终止操作:收集
147+
* 转换成lit、set、Collection
148+
* 求和,求平均数
149+
*/
150+
@Test
151+
public void test7() {
152+
Set<Employee> set = emps.stream().collect(Collectors.toSet());
153+
System.out.println(set);
154+
List<Employee> list = emps.stream().collect(Collectors.toList());
155+
Double salarySum = emps.stream().collect(Collectors.summingDouble(Employee::getSalary));
156+
157+
}
158+
}

04spring/springboot01/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@
3939
<artifactId>spring-boot-starter-test</artifactId>
4040
<scope>test</scope>
4141
</dependency>
42+
<!-- <dependency>-->
43+
<!-- <groupId>com.rabbitmq</groupId>-->
44+
<!-- <artifactId>amqp-client</artifactId>-->
45+
<!-- <version>3.4.1</version>-->
46+
<!-- </dependency>-->
47+
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-amqp</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-web</artifactId>
55+
</dependency>
4256
</dependencies>
4357

4458
<build>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.github.yibing.springboot01.config;
2+
3+
import org.springframework.amqp.core.Queue;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
public class RabbitConfig {
9+
10+
@Bean
11+
public Queue queue() {
12+
return new Queue("q_test_1");
13+
}
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//package com.github.yibing.springboot01.consumer;
2+
//
3+
//import com.github.yibing.springboot01.util.MqConnectionUtil;
4+
//import com.rabbitmq.client.Channel;
5+
//import com.rabbitmq.client.Connection;
6+
//import com.rabbitmq.client.QueueingConsumer;
7+
//
8+
//import java.io.IOException;
9+
//
10+
//public class Consumer {
11+
// public static void main(String[] args) throws IOException, InterruptedException {
12+
// Connection connection = MqConnectionUtil.getConnection();
13+
// Channel channel = connection.createChannel();
14+
// // 声明队列
15+
// channel.queueDeclare("q_test_1", false, false, false, null);
16+
// // 创建消费队列
17+
// QueueingConsumer consumer = new QueueingConsumer(channel);
18+
// // 消费消息
19+
// channel.basicConsume("q_test_1", consumer);
20+
// // 转换消费的消息
21+
// while (true) {
22+
// QueueingConsumer.Delivery delivery = consumer.nextDelivery();
23+
// String message = new String(delivery.getBody());
24+
// System.out.println("收到消息:" + message);
25+
// }
26+
//
27+
// }
28+
//}

0 commit comments

Comments
 (0)