Browse Source

map

pull/1/head
马府强 2 years ago
parent
commit
6b32d89cf5
  1. 15
      celuemoshijingjian/src/main/java/com/woniu/celuejj/celue/ConcreteStrategyA.java
  2. 15
      celuemoshijingjian/src/main/java/com/woniu/celuejj/celue/ConcreteStrategyB.java
  3. 16
      celuemoshijingjian/src/main/java/com/woniu/celuejj/celue/ConcreteStrategyC.java
  4. 14
      celuemoshijingjian/src/main/java/com/woniu/celuejj/celue/Strategy.java
  5. 5
      celuemoshijingjian/src/main/java/com/woniu/celuejj/celue/StrategyRunner.java
  6. 23
      celuemoshijingjian/src/main/java/com/woniu/celuejj/celue/StrategyRunnerImpl.java
  7. 26
      celuemoshijingjian/src/main/java/com/woniu/celuejj/celue/StrategySelector.java
  8. 19
      celuemoshijingjian/src/main/java/com/woniu/celuejj/celue/TeT.java
  9. 19
      celuemoshijingjian/src/main/java/com/woniu/celuejj/factory/DefaultHashMap.java
  10. 74
      celuemoshijingjian/src/main/java/com/woniu/celuejj/factory/Test.java
  11. 11
      celuemoshijingjian/src/main/java/com/woniu/celuejj/factory/User.java
  12. BIN
      celuemoshijingjian/target/classes/com/woniu/celuejj/celue/ConcreteStrategyA.class
  13. BIN
      celuemoshijingjian/target/classes/com/woniu/celuejj/celue/ConcreteStrategyB.class
  14. BIN
      celuemoshijingjian/target/classes/com/woniu/celuejj/celue/ConcreteStrategyC.class
  15. BIN
      celuemoshijingjian/target/classes/com/woniu/celuejj/celue/Strategy.class
  16. BIN
      celuemoshijingjian/target/classes/com/woniu/celuejj/celue/StrategyRunner.class
  17. BIN
      celuemoshijingjian/target/classes/com/woniu/celuejj/celue/StrategyRunnerImpl.class
  18. BIN
      celuemoshijingjian/target/classes/com/woniu/celuejj/celue/StrategySelector.class
  19. BIN
      celuemoshijingjian/target/classes/com/woniu/celuejj/celue/TeT.class
  20. BIN
      celuemoshijingjian/target/classes/com/woniu/celuejj/factory/Test.class
  21. BIN
      celuemoshijingjian/target/classes/com/woniu/celuejj/factory/User.class
  22. 8
      usemap/.idea/.gitignore
  23. 14
      usemap/.idea/compiler.xml
  24. 20
      usemap/.idea/jarRepositories.xml
  25. 14
      usemap/.idea/misc.xml
  26. 2
      usemap/celuemoshijingjian.iml
  27. 21
      usemap/pom.xml
  28. 15
      usemap/src/main/java/com/woniu/celuejj/celue/ConcreteStrategyA.java
  29. 15
      usemap/src/main/java/com/woniu/celuejj/celue/ConcreteStrategyB.java
  30. 16
      usemap/src/main/java/com/woniu/celuejj/celue/ConcreteStrategyC.java
  31. 14
      usemap/src/main/java/com/woniu/celuejj/celue/Strategy.java
  32. 5
      usemap/src/main/java/com/woniu/celuejj/celue/StrategyRunner.java
  33. 23
      usemap/src/main/java/com/woniu/celuejj/celue/StrategyRunnerImpl.java
  34. 26
      usemap/src/main/java/com/woniu/celuejj/celue/StrategySelector.java
  35. 19
      usemap/src/main/java/com/woniu/celuejj/celue/TeT.java
  36. 19
      usemap/src/main/java/com/woniu/celuejj/factory/DefaultHashMap.java
  37. 74
      usemap/src/main/java/com/woniu/celuejj/factory/Test.java
  38. 11
      usemap/src/main/java/com/woniu/celuejj/factory/User.java
  39. BIN
      usemap/target/classes/com/woniu/celuejj/celue/ConcreteStrategyA.class
  40. BIN
      usemap/target/classes/com/woniu/celuejj/celue/ConcreteStrategyB.class
  41. BIN
      usemap/target/classes/com/woniu/celuejj/celue/ConcreteStrategyC.class
  42. BIN
      usemap/target/classes/com/woniu/celuejj/celue/Strategy.class
  43. BIN
      usemap/target/classes/com/woniu/celuejj/celue/StrategyRunner.class
  44. BIN
      usemap/target/classes/com/woniu/celuejj/celue/StrategyRunnerImpl.class
  45. BIN
      usemap/target/classes/com/woniu/celuejj/celue/StrategySelector.class
  46. BIN
      usemap/target/classes/com/woniu/celuejj/celue/TeT.class
  47. BIN
      usemap/target/classes/com/woniu/celuejj/factory/Test.class
  48. BIN
      usemap/target/classes/com/woniu/celuejj/factory/User.class

15
celuemoshijingjian/src/main/java/com/woniu/celuejj/celue/ConcreteStrategyA.java

@ -0,0 +1,15 @@
package com.woniu.celuejj.celue;
public class ConcreteStrategyA implements Strategy {
@Override
public String strategy() {
return StrategySelector.strategyA.getStrategy();
}
@Override
public void algorithm() {
System.out.println("process with strategyA...");
}
}

15
celuemoshijingjian/src/main/java/com/woniu/celuejj/celue/ConcreteStrategyB.java

@ -0,0 +1,15 @@
package com.woniu.celuejj.celue;
public class ConcreteStrategyB implements Strategy {
@Override
public String strategy() {
return StrategySelector.strategyB.getStrategy();
}
@Override
public void algorithm() {
System.out.println("process with strategyB...");
}
}

16
celuemoshijingjian/src/main/java/com/woniu/celuejj/celue/ConcreteStrategyC.java

@ -0,0 +1,16 @@
package com.woniu.celuejj.celue;
public class ConcreteStrategyC implements Strategy {
@Override
public String strategy() {
return StrategySelector.strategyC.getStrategy();
}
@Override
public void algorithm() {
System.out.println("process with strategyC...");
}
}

14
celuemoshijingjian/src/main/java/com/woniu/celuejj/celue/Strategy.java

@ -0,0 +1,14 @@
package com.woniu.celuejj.celue;
public interface Strategy {
/**
* 采用策略
*/
String strategy();
/**
* 计算方法逻辑
*/
void algorithm();
}

5
celuemoshijingjian/src/main/java/com/woniu/celuejj/celue/StrategyRunner.java

@ -0,0 +1,5 @@
package com.woniu.celuejj.celue;
public interface StrategyRunner {
void execute(String strategy);
}

23
celuemoshijingjian/src/main/java/com/woniu/celuejj/celue/StrategyRunnerImpl.java

@ -0,0 +1,23 @@
package com.woniu.celuejj.celue;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class StrategyRunnerImpl implements StrategyRunner {
private static final List<Strategy> STRATEGIES = Arrays.asList(new ConcreteStrategyA(), new ConcreteStrategyB(), new ConcreteStrategyC());
private static Map<String, Strategy> STRATEGY_MAP = new HashMap<>();
static {
STRATEGY_MAP = STRATEGIES.stream().collect(Collectors.toMap(Strategy::strategy, s -> s));
}
@Override
public void execute(String strategy) {
STRATEGY_MAP.get(strategy).algorithm();
}
}

26
celuemoshijingjian/src/main/java/com/woniu/celuejj/celue/StrategySelector.java

@ -0,0 +1,26 @@
package com.woniu.celuejj.celue;
public enum StrategySelector {
strategyA(1, "strategyA"),
strategyB(2, "strategyB"),
strategyC(3, "strategyC");
private Integer code;
private String strategy;
public String getStrategy() {
return strategy;
}
public Integer getCode() {
return code;
}
StrategySelector(Integer code, String strategy) {
this.code = code;
this.strategy = strategy;
}
}

19
celuemoshijingjian/src/main/java/com/woniu/celuejj/celue/TeT.java

@ -0,0 +1,19 @@
package com.woniu.celuejj.celue;
/**
* 商场搞活动
* 根据客户购买商品的金额
* 收费时给与不同的打折
* 比如
* 购买 金额>=2000 的打八折(0.8)
* 金额 500 ~ 1000 打九折(0.9)
* 购买金额 0 ~ 500 的九五折(0.95)
* 根据不同的金额走不同计算策略逻辑
*/
public class TeT {
public static void main(String[] args) {
}
}

19
celuemoshijingjian/src/main/java/com/woniu/celuejj/factory/DefaultHashMap.java

@ -0,0 +1,19 @@
package com.woniu.celuejj.factory;
import java.util.HashMap;
import java.util.function.Function;
import java.util.function.Supplier;
public class DefaultHashMap<K, V> extends HashMap<K, V> {
Function<K, V> function;
public DefaultHashMap(Supplier<V> supplier) {
this.function = k -> supplier.get();
}
@Override
@SuppressWarnings("unchecked")
public V get(Object key) {
return super.computeIfAbsent((K) key, this.function);
}
}

74
celuemoshijingjian/src/main/java/com/woniu/celuejj/factory/Test.java

@ -0,0 +1,74 @@
package com.woniu.celuejj.factory;
import java.util.*;
import java.util.stream.Collectors;
//Java8中方便又实用的Map函数
public class Test {
static List<User> users = Arrays.asList(
new User(1, 23),
new User(2, 25),
new User(3, 45),
new User(1, 49));
public static void main(String[] args) {
Map<Integer, List<User>> userMap = new HashMap<>();
for (User user : users) {
if (!userMap.containsKey(user.getPayType())) {
userMap.put(user.getPayType(), new ArrayList<>());
}
userMap.get(user.getPayType()).add(user);
}
System.out.println(userMap);
Map<Integer, List<User>> collectMap = users.
stream().
collect(Collectors.groupingBy(item -> item.getPayType()));
System.out.println(collectMap);
Map<Integer, List<User>> userMapNew = new HashMap<>();
for (User user : users) {
userMapNew.computeIfAbsent(user.getPayType(), k -> new ArrayList<>()).
add(user);
}
List<User> userNews = users;
Map<Integer, List<User>> userTypeMap = new DefaultHashMap<>(ArrayList::new);
for (User userNew : userNews) {
userTypeMap.get(userNew.getPayType())
.add(userNew);
}
}
}

11
celuemoshijingjian/src/main/java/com/woniu/celuejj/factory/User.java

@ -0,0 +1,11 @@
package com.woniu.celuejj.factory;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class User {
private Integer payType;
private Integer age;
}

BIN
celuemoshijingjian/target/classes/com/woniu/celuejj/celue/ConcreteStrategyA.class

Binary file not shown.

BIN
celuemoshijingjian/target/classes/com/woniu/celuejj/celue/ConcreteStrategyB.class

Binary file not shown.

BIN
celuemoshijingjian/target/classes/com/woniu/celuejj/celue/ConcreteStrategyC.class

Binary file not shown.

BIN
celuemoshijingjian/target/classes/com/woniu/celuejj/celue/Strategy.class

Binary file not shown.

BIN
celuemoshijingjian/target/classes/com/woniu/celuejj/celue/StrategyRunner.class

Binary file not shown.

BIN
celuemoshijingjian/target/classes/com/woniu/celuejj/celue/StrategyRunnerImpl.class

Binary file not shown.

BIN
celuemoshijingjian/target/classes/com/woniu/celuejj/celue/StrategySelector.class

Binary file not shown.

BIN
celuemoshijingjian/target/classes/com/woniu/celuejj/celue/TeT.class

Binary file not shown.

BIN
celuemoshijingjian/target/classes/com/woniu/celuejj/factory/Test.class

Binary file not shown.

BIN
celuemoshijingjian/target/classes/com/woniu/celuejj/factory/User.class

Binary file not shown.

8
usemap/.idea/.gitignore

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/../../../../:\java project\celuemoshijingjian\.idea/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

14
usemap/.idea/compiler.xml

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="celuemoshijingjian" />
</profile>
</annotationProcessing>
</component>
</project>

20
usemap/.idea/jarRepositories.xml

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

14
usemap/.idea/misc.xml

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

2
usemap/celuemoshijingjian.iml

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />

21
usemap/pom.xml

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.woniu</groupId>
<artifactId>celuemoshijingjian</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
</project>

15
usemap/src/main/java/com/woniu/celuejj/celue/ConcreteStrategyA.java

@ -0,0 +1,15 @@
package com.woniu.celuejj.celue;
public class ConcreteStrategyA implements Strategy {
@Override
public String strategy() {
return StrategySelector.strategyA.getStrategy();
}
@Override
public void algorithm() {
System.out.println("process with strategyA...");
}
}

15
usemap/src/main/java/com/woniu/celuejj/celue/ConcreteStrategyB.java

@ -0,0 +1,15 @@
package com.woniu.celuejj.celue;
public class ConcreteStrategyB implements Strategy {
@Override
public String strategy() {
return StrategySelector.strategyB.getStrategy();
}
@Override
public void algorithm() {
System.out.println("process with strategyB...");
}
}

16
usemap/src/main/java/com/woniu/celuejj/celue/ConcreteStrategyC.java

@ -0,0 +1,16 @@
package com.woniu.celuejj.celue;
public class ConcreteStrategyC implements Strategy {
@Override
public String strategy() {
return StrategySelector.strategyC.getStrategy();
}
@Override
public void algorithm() {
System.out.println("process with strategyC...");
}
}

14
usemap/src/main/java/com/woniu/celuejj/celue/Strategy.java

@ -0,0 +1,14 @@
package com.woniu.celuejj.celue;
public interface Strategy {
/**
* 采用策略
*/
String strategy();
/**
* 计算方法逻辑
*/
void algorithm();
}

5
usemap/src/main/java/com/woniu/celuejj/celue/StrategyRunner.java

@ -0,0 +1,5 @@
package com.woniu.celuejj.celue;
public interface StrategyRunner {
void execute(String strategy);
}

23
usemap/src/main/java/com/woniu/celuejj/celue/StrategyRunnerImpl.java

@ -0,0 +1,23 @@
package com.woniu.celuejj.celue;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class StrategyRunnerImpl implements StrategyRunner {
private static final List<Strategy> STRATEGIES = Arrays.asList(new ConcreteStrategyA(), new ConcreteStrategyB(), new ConcreteStrategyC());
private static Map<String, Strategy> STRATEGY_MAP = new HashMap<>();
static {
STRATEGY_MAP = STRATEGIES.stream().collect(Collectors.toMap(Strategy::strategy, s -> s));
}
@Override
public void execute(String strategy) {
STRATEGY_MAP.get(strategy).algorithm();
}
}

26
usemap/src/main/java/com/woniu/celuejj/celue/StrategySelector.java

@ -0,0 +1,26 @@
package com.woniu.celuejj.celue;
public enum StrategySelector {
strategyA(1, "strategyA"),
strategyB(2, "strategyB"),
strategyC(3, "strategyC");
private Integer code;
private String strategy;
public String getStrategy() {
return strategy;
}
public Integer getCode() {
return code;
}
StrategySelector(Integer code, String strategy) {
this.code = code;
this.strategy = strategy;
}
}

19
usemap/src/main/java/com/woniu/celuejj/celue/TeT.java

@ -0,0 +1,19 @@
package com.woniu.celuejj.celue;
/**
* 商场搞活动
* 根据客户购买商品的金额
* 收费时给与不同的打折
* 比如
* 购买 金额>=2000 的打八折(0.8)
* 金额 500 ~ 1000 打九折(0.9)
* 购买金额 0 ~ 500 的九五折(0.95)
* 根据不同的金额走不同计算策略逻辑
*/
public class TeT {
public static void main(String[] args) {
}
}

19
usemap/src/main/java/com/woniu/celuejj/factory/DefaultHashMap.java

@ -0,0 +1,19 @@
package com.woniu.celuejj.factory;
import java.util.HashMap;
import java.util.function.Function;
import java.util.function.Supplier;
public class DefaultHashMap<K, V> extends HashMap<K, V> {
Function<K, V> function;
public DefaultHashMap(Supplier<V> supplier) {
this.function = k -> supplier.get();
}
@Override
@SuppressWarnings("unchecked")
public V get(Object key) {
return super.computeIfAbsent((K) key, this.function);
}
}

74
usemap/src/main/java/com/woniu/celuejj/factory/Test.java

@ -0,0 +1,74 @@
package com.woniu.celuejj.factory;
import java.util.*;
import java.util.stream.Collectors;
//Java8中方便又实用的Map函数
public class Test {
static List<User> users = Arrays.asList(
new User(1, 23),
new User(2, 25),
new User(3, 45),
new User(1, 49));
public static void main(String[] args) {
Map<Integer, List<User>> userMap = new HashMap<>();
for (User user : users) {
if (!userMap.containsKey(user.getPayType())) {
userMap.put(user.getPayType(), new ArrayList<>());
}
userMap.get(user.getPayType()).add(user);
}
System.out.println(userMap);
Map<Integer, List<User>> collectMap = users.
stream().
collect(Collectors.groupingBy(item -> item.getPayType()));
System.out.println(collectMap);
Map<Integer, List<User>> userMapNew = new HashMap<>();
for (User user : users) {
userMapNew.computeIfAbsent(user.getPayType(), k -> new ArrayList<>()).
add(user);
}
List<User> userNews = users;
Map<Integer, List<User>> userTypeMap = new DefaultHashMap<>(ArrayList::new);
for (User userNew : userNews) {
userTypeMap.get(userNew.getPayType())
.add(userNew);
}
}
}

11
usemap/src/main/java/com/woniu/celuejj/factory/User.java

@ -0,0 +1,11 @@
package com.woniu.celuejj.factory;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class User {
private Integer payType;
private Integer age;
}

BIN
usemap/target/classes/com/woniu/celuejj/celue/ConcreteStrategyA.class

Binary file not shown.

BIN
usemap/target/classes/com/woniu/celuejj/celue/ConcreteStrategyB.class

Binary file not shown.

BIN
usemap/target/classes/com/woniu/celuejj/celue/ConcreteStrategyC.class

Binary file not shown.

BIN
usemap/target/classes/com/woniu/celuejj/celue/Strategy.class

Binary file not shown.

BIN
usemap/target/classes/com/woniu/celuejj/celue/StrategyRunner.class

Binary file not shown.

BIN
usemap/target/classes/com/woniu/celuejj/celue/StrategyRunnerImpl.class

Binary file not shown.

BIN
usemap/target/classes/com/woniu/celuejj/celue/StrategySelector.class

Binary file not shown.

BIN
usemap/target/classes/com/woniu/celuejj/celue/TeT.class

Binary file not shown.

BIN
usemap/target/classes/com/woniu/celuejj/factory/Test.class

Binary file not shown.

BIN
usemap/target/classes/com/woniu/celuejj/factory/User.class

Binary file not shown.
Loading…
Cancel
Save