AviatorScript 轻量级Java表达式解析工具
import java.util.ArrayList;
import java.util.Comparator;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author: Cai MinXing
* @create: 2020-03-25 18:15
**/
public class StreamTest {
// +--------------------+ +------+ +------+ +---+ +-------+
// | stream of elements +-----> |filter+-> |sorted+-> |map+-> |collect|
// +--------------------+ +------+ +------+ +---+ +-------+
public static void main(String args[]){
List<UserPo> list = new ArrayList<>();
list.add(new UserPo("小一", 10.d));
list.add(new UserPo("小五", 50.d));
list.add(new UserPo("小六", 60.d));
list.add(new UserPo("小6", 60.d));
list.add(new UserPo("小空", null));
list.add(new UserPo("小九", 90.d));
long count = 0;
List<UserPo> filterList = null;
// filter 过滤器的使用
// 筛选出成绩不为空的学生人数
count = list.stream().filter(p -> null != p.getScore()).count();
System.out.println("参加考试的学生人数:" + count);
// collect
// 筛选出成绩不为空的学生集合
filterList = list.stream().filter(p -> null != p.getScore()).collect(Collectors.toList());
System.out.println("参加考试的学生信息:");
filterList.stream().forEach(System.out::println);
// map 将集合映射为另外一个集合
// 取出所有学生的成绩
List<Double> scoreList = list.stream().map(p -> p.getScore()).collect(Collectors.toList());
System.out.println("所有学生的成绩集合:" + scoreList);
// 将学生姓名集合串成字符串,用逗号分隔
String nameString = list.stream().map(p -> p.getName()).collect(Collectors.joining(","));
System.out.println("所有学生的姓名字符串:" + nameString);
// sorted排序
// 按学生成绩逆序排序 正序则不需要加.reversed()
filterList = list.stream().filter(p -> null != p.getScore()).sorted(Comparator.comparing(UserPo::getScore).reversed()).collect(Collectors.toList());
System.out.println("所有学生的成绩集合,逆序排序:");
filterList.stream().forEach(System.out::println);
System.out.println("按学生成绩归集:");
Map<Double, List<UserPo>> groupByScoreMap = list.stream().filter(p -> null != p.getScore())
.collect(Collectors.groupingBy(UserPo::getScore));
for (Map.Entry<Double, List<UserPo>> entry : groupByScoreMap.entrySet()) {
System.out.println("成绩:" + entry.getKey() + " 人数:" + entry.getValue().size());
}
// forEach
filterList.stream().forEach(p -> p.setScore(p.getScore() + 10));
System.out.println("及格人数太少,给每个人加10分");
filterList.stream().forEach(System.out::println);
// count
count = filterList.stream().filter(p -> p.getScore() >= 60).count();
System.out.println("最后及格人数" + count);
DoubleSummaryStatistics statistics = filterList.stream().mapToDouble(p -> p.getScore()).summaryStatistics();
System.out.println("列表中最大的数 : " + statistics.getMax());
System.out.println("列表中最小的数 : " + statistics.getMin());
System.out.println("所有数之和 : " + statistics.getSum());
System.out.println("平均数 : " + statistics.getAverage());
// 并行流 使用
count = list.parallelStream().filter(p -> null != p.getScore()).count();
System.out.println("并行流处理参加考试的学生人数:" + count);
}
}
JDK下载地址:Java Platform, Standard Edition 11 ReferenceImplementations
list转map
Map<String,Entity> statMap = statList.stream().collect(Collectors.toMap(Entity::getId, Entity -> Entity));
List<String> collect = roleResultList.stream().map(AcAppRole::getName).collect(Collectors.toList());
Map<Integer, String> map = new HashMap<>();
map.put(10, "apple");
map.put(20, "orange");
map.put(30, "banana");
map.put(40, "watermelon");
map.put(50, "dragonfruit");
System.out.println("\n1. Export Map Key to List...");
List<Integer> result = map.keySet().stream().collect(Collectors.toList());
result.forEach(System.out::println);
System.out.println("\n2. Export Map Value to List...");
List<String> result2 = map.values().stream().collect(Collectors.toList());
result2.forEach(System.out::println);
System.out.println("\n3. Export Map Value to List..., say no to banana");
List<String> result3 = map.keySet().stream().filter(x -> !"banana".equalsIgnoreCase(x)).collect(Collectors.toList());
result3.forEach(System.out::println);
List<String> resultValues = map.entrySet().stream().sorted(Map.Entry.<Integer, String>comparingByKey().reversed())
.peek(e -> resultSortedKey.add(e.getKey()))
.map(x -> x.getValue())
.filter(x -> !"banana".equalsIgnoreCase(x))
.collect(Collectors.toList());
public static void main(String args[]) {
SqlServerReader tester = new SqlServerReader();
tester.testCaseFormat();
}
private void testCaseFormat() {
System.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, "test-data"));
System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "test_data"));
System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "test_data"));
System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "testdata"));
System.out.println();
System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, "testData"));
}
centeros jdk卸载
1.查看是否有jdk
rpm -qa|grep jdk
出现下面
java-1.7.0-openjdk-1.7.0.91-2.6.2.3.el7.x86_64
java-1.8.0-openjdk-headless-1.8.0.65-3.b17.el7.x86_64
java-1.8.0-openjdk-1.8.0.65-3.b17.el7.x86_64
java-1.7.0-openjdk-headless-1.7.0.91-2.6.2.3.el7.x86_64
rpm -qa|grep gcj
2.卸载
yum -y remove java java-1.7.0-openjdk-1.7.0.91-2.6.2.3.el7.x86_64
yum -y remove java java-1.8.0-openjdk-headless-1.8.0.65-3.b17.el7.x86_64
yum -y remove java java-1.8.0-openjdk-1.8.0.65-3.b17.el7.x86_64
yum -y remove java java-1.7.0-openjdk-headless-1.7.0.91-2.6.2.3.el7.x86_64
Spring框架的事务基础架构代码将默认地 只 在抛出运行时和unchecked exceptions时才标识事务回滚
也就是说,当抛出个RuntimeException 或其子类例的实例时。(Errors 也一样 - 默认地 - 标识事务回滚。)从事务方法中抛出的Checked exceptions将 ****不 被标识进行事务回滚。
1 让checked例外也回滚:在整个方法前加上 @Transactional(rollbackFor=Exception.class)
2 让unchecked例外不回滚: @Transactional(notRollbackFor=RunTimeException.class)
3 不需要事务管理的(只查询的)方法:@Transactional(propagation=Propagation.NOT_SUPPORTED)
读取classpath下jar中的文件
String content = ResourceUtil.readUtf8Str("cc/admin/modules/data/service/impl/config.json");
非jar中的可以这样写
String path = this.getClass().getResource("").getPath() + "/config.json";
File functionFile = new File(path);
String content = FileUtil.readString(functionFile, Charsets.UTF_8);