java8 system lambda 实际代码中的应用

实体类对象

package org.springblade.shubh.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springblade.core.mp.base.BaseEntity;


/**
 *  实体类
 *
 * @author Chill
 */
@Data
@TableName("shubh_map_cluster")
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "MapCluster对象", description = "")
public class MapClusterEntity extends BaseEntity {

	private static final long serialVersionUID = 1L;

	/**
	 * 产业集群类型
	 */
	@ApiModelProperty(value = "产业集群类型")
	private String clusterType;

	/**
	 * 字典码
	 */
	@ApiModelProperty(value = "字典码")
	private String dictCode;
}

一 list 转 map

场景一 : 有一个对象集合, 以对象的某个属性作为key, 另一个属性作为value 转换为 map

List<MapClusterEntity> list = new ArrayList<>();
//  假装已经在集合中添加内容
Map<String, String> collect = list.stream().collect(Collectors.toMap(
MapClusterEntity::getClusterType, MapClusterEntity::getDictCode,(v1,v2)->v2
));

list.stream() : 获取集合list 的stream操作流数据
.collect() : 进行类型转换
Collectors.toMap(): 转换为map类型
MapClusterEntity::getClusterType : 以类 MapClusterEntity 的clusterType 属性作为key
MapClusterEntity::getDictCode : 以类 MapClusterEntity 的 dictCode 属性作为value

场景二: 有一个对象集合, 以对象的某个属性作为key, 对象整体作为value 转换为 map

List<MapClusterEntity> list = new ArrayList<>();
//  假装已经在集合中添加内容
Map<String, MapClusterEntity> collect = list.stream().collect(Collectors.toMap(MapClusterEntity::getClusterType, o -> o,(v1,v2)->v1));

list.stream() : 获取集合list 的stream操作流数据
list.stream().collect() : 进行类型转换
list.stream().collect(Collectors.toMap()): 转换为 map
MapClusterEntity::getClusterType : 以类 MapClusterEntity 的clusterType 属性作为key
o -> o: 以当前遍历的集合中的元素作为value

场景三: 有一个对象集合, 以对象的某几个属性拼接作为key(或者value) 转换为 map

List<MapClusterEntity> list = new ArrayList<>();
//  假装已经在集合中添加内容
Map<String, MapClusterEntity> collect = list.stream().collect(Collectors.toMap(
			o->o.getClusterType() + "," + o.getDictCode(), o -> o, (v1,v2)->v1));

list.stream() : 获取集合list 的stream操作流数据
list.stream().collect() : 进行类型转换
list.stream().collect(Collectors.toMap()): 转换为 map
o->o.getClusterType() + “,” + o.getDictCode() : 取集合list中当前遍历的元素(此处为MapClusterEntity对象), 获取对象的 clusterType 属性和dictCode 属性 进行拼接 后作为key
o -> o : 以集合中当前遍历的元素作为value
v1: 表示 map 旧value
v2: 表示 map 新value
(v1,v2)->v1 : 转换过程中,map的key冲突, 则保留旧value (也就是解决冲突, 如果不加该方法,则key冲突时报错)

二 list 转 list

一 取对象的一个值进行list封装

List<MapClusterEntity> list = new ArrayList<>();
//  再次已经在集合中添加内容

List<String> collect = list.stream().map(o -> o.getClusterType()).collect(Collectors.toList());

list.stream() : 获取集合list 的stream操作流数据
list.stream().map() : enmmm… 不想解释
map的作用很容易理解就是对集合之中的元素进行逐一进行函数操作映射为另外一个内容。
就是会对每一条输入进行指定的操作,然后为每一条输入返回一个对象。
嘿嘿 没听懂吧, 问题不大,继续看
list.stream().map(o -> o.getClusterType()) : 将当前遍历元素 o 替换(映射) 为 o.的 clusterType 属性值
list.stream().map(o -> o.getClusterType()).collect(Collectors.toList()) : 将映射(替换)后的新元素 转换为新集合 (返回替换(映射)后的新集合)
举一反三
stream().map()
list.stream().map(o -> “2”): 将当前遍历元素 o 替换(映射) 为 2
list.stream().map(o -> new Date()): 将当前遍历元素 o 替换(映射) 为 另一个新对象
list.stream().map(o -> example(o)) : 将当前遍历元素 o 传递到另一个方法中进行 操作, 并接收返回值
举一反三结束

二 取对象的两个值拼接进行list封装

List<MapClusterEntity> list = new ArrayList<>();
//  真的已经在集合中添加内容
		MapClusterEntity me = new MapClusterEntity();
		me.setClusterType("one");
		me.setDictCode("1");
		MapClusterEntity me2 = new MapClusterEntity();
		me2.setClusterType("two");
		me2.setDictCode("2");
		MapClusterEntity me3 = new MapClusterEntity();
		me3.setClusterType("three");
		me3.setDictCode("3");
		MapClusterEntity me4 = new MapClusterEntity();
		me4.setClusterType("four");
		me4.setDictCode("4");
		list.add(me);
		list.add(me2);
		list.add(me3);
		list.add(me4);
		List<String> collect = list.stream().map(o -> o.getClusterType() + "_" + o.getDictCode()).collect(Collectors.toList());

		System.out.println(collect);

打印结果:

[one_1, two_2, three_3, four_4]

代码含义请结合上述举一反三自行分析;

三 取对象的两个值进行list封装 (不拼接)

List<MapClusterEntity> list = new ArrayList<>();
//  这次也真的已经在集合中添加内容
		MapClusterEntity me = new MapClusterEntity();
		me.setClusterType("one");
		me.setDictCode("1");
		MapClusterEntity me2 = new MapClusterEntity();
		me2.setClusterType("two");
		me2.setDictCode("2");
		MapClusterEntity me3 = new MapClusterEntity();
		me3.setClusterType("three");
		me3.setDictCode("3");
		MapClusterEntity me4 = new MapClusterEntity();
		me4.setClusterType("four");
		me4.setDictCode("4");
		list.add(me);
		list.add(me2);
		list.add(me3);
		list.add(me4);
List<String> collect = list.stream().flatMap(o -> Stream.of(o.getClusterType(),o.getDictCode())).collect(Collectors.toList());
System.out.println(collect);

打印结果:

[one, 1, two, 2, three, 3, four, 4]

list.stream() : 获取集合list 的stream操作流数据
list.stream().flatMap() : enmmm… 鄙人无法用大白话来解释, 希望各位大牛能在评论区指点一二
Stream.of() : 获取包含给定元素的stream操作流
list.stream().flatMap(o -> Stream.of(o.getClusterType(),o.getDictCode())): 获取当前遍历对象的 clusterType 属性值和 o的dictCode属性值的流, 并映射替换当前元素 o;
( 也就是用 Stream.of(o.getClusterType(),o.getDictCode()) 替换了 o . 替换效果等同于map
但是要使用 Stream.of(), 就得用flatMap,不能用map)
.collect(Collectors.toList()) : 不解释
举一再反三:
Stream.of()

		Stream<Integer> integerStream = Stream.of(1, 2, 3, 4);
		System.out.println(integerStream);
		integerStream.forEach(e-> System.out.print(e));
/** 输出结果:
java.util.stream.ReferencePipeline$Head@880ec60
1,2,3,4, **/
		MapClusterEntity me = new MapClusterEntity();
		me.setClusterType("one");
		me.setDictCode("1");
		MapClusterEntity me2 = new MapClusterEntity();
		me2.setClusterType("two");
		me2.setDictCode("2");
		MapClusterEntity me3 = new MapClusterEntity();
		me3.setClusterType("three");
		me3.setDictCode("3");
		MapClusterEntity me4 = new MapClusterEntity();
		me4.setClusterType("four");
		me4.setDictCode("4");
		Stream<MapClusterEntity> integerStream = Stream.of(me, me2, me3, me4);
		integerStream.forEach(e-> System.out.println(e));
/** 输出结果
	MapClusterEntity(clusterType=one, dictCode=1)
	MapClusterEntity(clusterType=two, dictCode=2)
	MapClusterEntity(clusterType=three, dictCode=3)
	MapClusterEntity(clusterType=four, dictCode=4)
	**/
		MapClusterEntity me = new MapClusterEntity();
		me.setClusterType("one");
		me.setDictCode("1");
		MapClusterEntity me2 = new MapClusterEntity();
		me2.setClusterType("two");
		me2.setDictCode("2");
		MapClusterEntity me3 = new MapClusterEntity();
		me3.setClusterType("three");
		me3.setDictCode("3");
		MapClusterEntity me4 = new MapClusterEntity();
		me4.setClusterType("four");
		me4.setDictCode("4");
		Stream<MapClusterEntity> integerStream = Stream.of(me, me2, me3, me4);
		integerStream.forEach(e-> System.out.println(e.getClusterType()));
		/** 输出结果
		one
		two
		three
		four
		**/

四 对象list, 经过一系列操作,转换为另一个对象list

非stream写法
只展示核心逻辑思路, 部分方法代码未展示, 若要实现demo 请自己手写代码(亲自实践,印象才深)


public List<MapClusterTreeNode> clusterRegionCoord() {
// 获取所有产业集群
List<MapClusterEntity> regionlist = this.list();
List<MapClusterTreeNode > reslist = new ArrayList()

for (MapClusterEntity mapClusterEntity : regionlist) {
			MapClusterTreeNode treeNode = getTreeNode(mapClusterEntity);
			reslist.add(treeNode);
			// 获取目标产业城市信息
			String clusterRegionCode = mapClusterEntity.getClusterRegionCode();
			Region clusterRegion = RegionCache.getByCode(clusterRegionCode);
			if (clusterRegion == null) {
				continue;
			}
			// 设置 坐标属性
			treeNode.setClusterRegionCoord(clusterRegion.getLat() + "," + clusterRegion.getLng());
			// 获取上游产业城市信息
			String upstreamRegionCode = mapClusterEntity.getUpstreamRegionCode();
			List<Region> upstreamRegionList = RegionCache.getByCodeList(Func.toStrList(upstreamRegionCode));
			if (Func.isEmpty(upstreamRegionList)) {
				continue;
			}
			// 拼接坐标属性
			List<String> upstreamRegionCoord = upstreamRegionList.stream().flatMap(o -> Stream.of(o.getLng() + ","+ o.getLat())).collect(Collectors.toList());
			treeNode.setUpstreamRegionCoord(upstreamRegionCoord);
			// 获取下游产业城市信息
			String downstreamRegionCode = mapClusterEntity.getDownstreamRegionCode();
			List<Region> downstreamRegionList = RegionCache.getByCodeList(Func.toStrList(downstreamRegionCode));
			if (Func.isEmpty(downstreamRegionList)) {
				continue;
			}
			// 拼接下游坐标属性
			List<String> downstreamRegionCoord = downstreamRegionList.stream().flatMap(o -> Stream.of(o.getLng() + "," + o.getLat())).collect(Collectors.toList());
			treeNode.setDownstreamRegionCoord(downstreamRegionCoord);
		}
}
	/**
	 * 转换树节点
	 * @param mapClusterEntity
	 * @return
	 */
	public MapClusterTreeNode getTreeNode(MapClusterEntity mapClusterEntity){
		MapClusterTreeNode treeNode = new MapClusterTreeNode();
		// 树节点id
		treeNode.setId(mapClusterEntity.getClusterId());
		// 树节点父id
		treeNode.setParentId(mapClusterEntity.getClusterType());
		// 展示图片地址
		treeNode.setBgimaegUrl(mapClusterEntity.getBgimaegUrl());
		// 产业集群名称
		treeNode.setClusterName(mapClusterEntity.getClusterName());
		
		return treeNode;
	}

MapClusterEntity :

/**
 *  实体类
 *
 * @author Chill
 */
@Data
public class MapClusterEntity {

	/**
	 * 产业集群类型
	 */
	@ApiModelProperty(value = "产业集群类型")
	private Long clusterType;

	/**
	 * 产业集群名称
	 */
	@ApiModelProperty(value = "产业集群名称")
	private String clusterName;
	/**
	 * 产业集群code
	 */
	@ApiModelProperty(value = "产业集群id")
	private Long clusterId;

	/**
	 * 图片地址
	 */
	@ApiModelProperty(value = "图片地址")
	private String bgimaegUrl;

	/**
	 * 产业集群所在地区code
	 */
	@ApiModelProperty(value = "产业集群所在地区编码")
	private String clusterRegionCode;

	/**
	 * 上游城市code
	 */
	@ApiModelProperty(value = "上游城市Code")
	private String upstreamRegionCode;

	/**
	 * 下游城市code
	 */
	@ApiModelProperty(value = "下游城市Code")
	private String downstreamRegionCode;

}

MapClusterTreeNode

/**
 *  模型VO
 *
 * @author Chill
 */
@Data
public class MapClusterTreeNode implements INode<MapClusterTreeNode>, Serializable {

	private static final long serialVersionUID = 1L;

	public MapClusterTreeNode(){

	}

	public MapClusterTreeNode(Long id,Long parentId,String clusterName){
		this.id = id;
		this.parentId = parentId;
		this.clusterName = clusterName;
	}


	private Long id;
	// 父节点
	private Long parentId;
	// 子节点
	private List<MapClusterTreeNode> children;

	public List<MapClusterTreeNode> getChildren() {
		if (this.children == null) {
			this.children = new ArrayList();
		}

		return this.children;
	}


	/**
	 * 产业集群名称
	 */
	@ApiModelProperty(value = "产业集群名称")
	private String clusterName;

	/**
	 * 图片地址
	 */
	@ApiModelProperty(value = "图片地址")
	private String bgimaegUrl;

	@ApiModelProperty(value = "目标产业所在地坐标")
	private String clusterRegionCoord;

	@ApiModelProperty(value = "上游所在地区坐标")
	private List<String> upstreamRegionCoord;

	@ApiModelProperty(value = "下游产业所在地区坐标")
	private List<String> downstreamRegionCoord;
}

Stream 写法
请结合上述 一二三 种场景自行思考一下该如何 用Stream写法改造 (大佬请绕路)

	public List<MapClusterTreeNode> clusterRegionCoord() {
		// 获取所有产业集群
		List<MapClusterEntity> regionlist = this.list();
		List<MapClusterTreeNode> reslist = regionlist.stream().flatMap(mapClusterEntity -> {
			// 转换为树节点model , 并赋值产业链坐标
			MapClusterTreeNode treeNode = getTreeNode(mapClusterEntity);
			return Stream.of(treeNode);
		}).collect(Collectors.toList());
// 或者可以写成
		/**List<MapClusterTreeNode> reslist = regionlist.stream()
		.flatMap(mapClusterEntity -> Stream.of(getTreeNode(mapClusterEntity)))
		.collect(Collectors.toList());**/
		return reslist;
	}
	/**
	 * 转换树节点
	 * @param mapClusterEntity
	 * @return
	 */
	public MapClusterTreeNode getTreeNode(MapClusterEntity mapClusterEntity){
		MapClusterTreeNode treeNode = new MapClusterTreeNode();
		// 树节点id
		treeNode.setId(mapClusterEntity.getClusterId());
		// 树节点父id
		treeNode.setParentId(mapClusterEntity.getClusterType());
		// 展示图片地址
		treeNode.setBgimaegUrl(mapClusterEntity.getBgimaegUrl());
		// 产业集群名称
		treeNode.setClusterName(mapClusterEntity.getClusterName());
		// 获取目标产业城市信息
		String clusterRegionCode = mapClusterEntity.getClusterRegionCode();
		Region clusterRegion = RegionCache.getByCode(clusterRegionCode);
		// 获取上游产业城市信息
		String upstreamRegionCode = mapClusterEntity.getUpstreamRegionCode();
		List<Region> upstreamRegionList = RegionCache.getByCodeList(Func.toStrList(upstreamRegionCode));
		// 获取下游产业城市信息
		String downstreamRegionCode = mapClusterEntity.getDownstreamRegionCode();
		List<Region> downstreamRegionList = RegionCache.getByCodeList(Func.toStrList(downstreamRegionCode));
		if (clusterRegion != null && Func.isNotEmpty(upstreamRegionList) && Func.isNotEmpty(downstreamRegionList)) {
			// 设置目标坐标属性
			treeNode.setClusterRegionCoord(clusterRegion.getLat() + "," + clusterRegion.getLng());
			// 拼接上游坐标属性
			List<String> upstreamRegionCoord = upstreamRegionList.stream().flatMap(o -> Stream.of(o.getLng() + "," + o.getLat())).collect(Collectors.toList());
			treeNode.setUpstreamRegionCoord(upstreamRegionCoord);
			// 拼接下游坐标属性
			List<String> downstreamRegionCoord = downstreamRegionList.stream().flatMap(o -> Stream.of(o.getLng() + "," + o.getLat())).collect(Collectors.toList());
			treeNode.setDownstreamRegionCoord(downstreamRegionCoord);
		}
		return treeNode;
	}

代码含义: 不解释

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值