编程语言
首页 > 编程语言> > java8的stream操作

java8的stream操作

作者:互联网

 这是今天的最后一篇了,一口气发了七八篇吧,从网上copy都没有这么快的速度,这些零零碎碎的总结,都是平时工作常用的一些知识点,我一般都喜欢用印象笔记记下来或者用个txt记下来,后来发现知识还是分享出来,交流多了你会有额外的收获。java8也出来好几年了,切切实实感受到了她带来的便利,就来一个java排序来说,以前吭叽吭叽十几二十行代码才能实现,现在一行代码搞定。以前操作文件,总是担心资源,上个项目通过解析、清洗3到4G的csv文件,来获取我们自己想要的宝贵数据,java8的读写,真的是让你用了就爱不释手,hahah,闲话不多说,上代码

import com.google.common.collect.Maps;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;

/**
* java8stream
*
* @author nanfengxiangbei
* @date 2020/9/27 10:20
*/
public class StreamDemo002 {

public static void main(String[] args) {
List<Dish> menu = Arrays.asList(
new Dish("pork", false, 800, DishType.MEAT),
new Dish("beef", false, 700, DishType.MEAT),
new Dish("chicken", false, 400, DishType.MEAT),
new Dish("french fries", true, 530, DishType.OTHER),
new Dish("rice", true, 350, DishType.OTHER),
new Dish("season fruit", true, 120, DishType.OTHER),
new Dish("pizza", true, 550, DishType.OTHER),
new Dish("prawns", false, 300, DishType.FISH),
new Dish("salmon", false, 450, DishType.FISH),
new Dish("salmon", false, 420, DishType.FISH)
);

List<Dish> gt300Calories = menu.stream().filter(dish -> dish.getCalories() > 300)
.filter(Dish::getVegetarian)
.collect(Collectors.toList());

// gt300Calories.forEach(System.out::println);
List<String> length = menu.stream().filter(dish -> dish.getCalories() > 300)
.map(Dish::getName)
.map(s -> s.split(""))
.flatMap(Arrays::stream)
.distinct()
.collect(Collectors.toList());
// length.forEach(System.out::println);

List<Integer> arrsA = Arrays.asList(1, 2, 3);
List<Integer> arrsB = Arrays.asList(4, 5);

List<int[]> lists = arrsA.stream()
.flatMap(i -> arrsB.stream()
.filter(j -> (i + j) % 3 == 0)
.map(j -> new int[]{i, j}))
.collect(Collectors.toList());

Optional<Dish> dish = menu.stream()
.filter(Dish::getVegetarian)
.findAny();

int sum = menu.stream().filter(calories -> calories.getCalories() > 300)
.map(Dish::getCalories)
.reduce(0, Integer::sum);
// System.out.println(sum);

Map<String, List<Dish>> maps = menu.stream()
.collect(groupingBy(Dish::getName));
// System.out.println(maps);

Map<DishType, Integer> maps1 = menu.stream()
.collect(
groupingBy(Dish::getType,
Collectors.reducing(0, Dish::getCalories, Integer::sum))
);
// maps1.forEach((k,v) -> System.out.println(k + " : " + v));

Map<DishType, Double> maps2 = menu.stream()
.collect(
groupingBy(Dish::getType,
Collectors.averagingInt(Dish::getCalories))
);
// maps2.forEach((k,v) -> System.out.println(k + " : " + v));

 

}
}

 

 

--------------------------java8文件操作(这一个,应该就很详细了吧,haha,业务代码不能给太多)

/**
* 生成非融合包与观看数据的整合文件
*
* @param rhbRsplMap 融合包关联关系map
* @param ptbRsplMap 普通产品关联关系map
*/
public void dealWatchDataRlFusionData(Map<String, Object> rhbRsplMap,
Map<String, Object> ptbRsplMap,
String inputFilePath,
String inputFileName,
String outFile) {
try {
Path filePath = Paths.get(inputFilePath, inputFileName);
// 读
Stream<String> lineStream = Files.lines(filePath);
Stream<String> s1 = lineStream.filter(s -> {
String[] items = s.split("\\|");
String contentId = items[1].trim();
return ptbRsplMap.get(contentId) != null;
});

Stream<String> s2 = s1.map(s -> {
String[] items = s.split("\\|");
String account = items[0].trim();
String contentId = items[1].trim();
String start = items[2].trim();
String end = items[3].trim();
StringBuilder sb = new StringBuilder(account).append("|").append(contentId).append("|").append(start).append("|").append(end).append("|");

String ptProdCode;
if (ptbRsplMap.get(contentId) != null) {
ptProdCode = (String) ptbRsplMap.get(contentId);
} else {
ptProdCode = (String) rhbRsplMap.get(contentId);
}

sb.append(StringUtils.isEmpty(ptProdCode) ? "" : ptProdCode);
return sb.toString();
});

// 生成非融合包与观看数据的整合文件
File newFile = new File(outFile);

List<String> lineList = new ArrayList<>();
// 批量写
s2.forEach(s -> {
try {
lineList.add(s);
if (lineList.size() == 200000) {
FileUtils.writeLines(newFile, lineList, true);
lineList.clear();
}
} catch (Exception ex) {
logger.error("BaseFieldHandle.dealWatchDataRLFusionData异常ex", ex);
}
});
if (lineList.size() > 0) {
FileUtils.writeLines(newFile, lineList, true);
}
} catch (Exception e) {
logger.error("BaseFieldHandle.dealWatchDataRLFusionData异常", e);
}
}

标签:String,stream,menu,new,Dish,操作,DishType,java8
来源: https://www.cnblogs.com/nanfengxiangbei/p/14190187.html