java遍历二级城市_java8用Lambda遍历省市区三级数据
1 创建省市区的数据库
CREATE TABLE `m_area_code` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '唯一标识',
`code` int(10) NOT NULL COMMENT '省份code',
`parent_code` int(10) NOT NULL DEFAULT 0 COMMENT '父级code',
`area_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `index_areacode_code`(`code`) USING BTREE,
INDEX `index_areacode_pcode`(`parent_code`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4069 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '行政区划' ROW_FORMAT = Dynamic;
2 插入北京的数据
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (12, 110000, 0, '北京市', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (425, 110100, 110000, '北京市', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (814, 110101, 110100, '东城区', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (815, 110102, 110100, '西城区', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (816, 110105, 110100, '朝阳区', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (817, 110106, 110100, '丰台区', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (818, 110107, 110100, '石景山区', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (819, 110108, 110100, '海淀区', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (820, 110109, 110100, '门头沟区', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (821, 110111, 110100, '房山区', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (822, 110112, 110100, '通州区', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (823, 110113, 110100, '顺义区', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (824, 110114, 110100, '昌平区', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (825, 110115, 110100, '大兴区', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (826, 110116, 110100, '怀柔区', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (827, 110117, 110100, '平谷区', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (828, 110118, 110100, '密云区', NULL);
INSERT INTO `medical`.`m_area_code`(`id`, `code`, `parent_code`, `area_name`, `pinyin`) VALUES (829, 110119, 110100, '延庆区', NULL);
3 遍历数据
(方法一:比较耗时)
ListmAreaCodeList = mAreaCodeService.selectList();
ListoneLevelRegion = mAreaCodeList.stream().filter(item -> item.getParentCode() == 0).collect(Collectors.toList());
//遍历一级地区
oneLevelRegion.stream().forEach( oneItem ->
{
//获得二级地区 地级市、地区、自治州、盟
ListtwoLevelRegion = mAreaCodeList.stream().filter(twoItem ->
StringUtils.equals(String.valueOf(twoItem.getParentCode()),String.valueOf(oneItem.getCode())))
.collect(Collectors.toList());
//遍历二级地区
twoLevelRegion.stream().forEach( twoItem ->
{
//获得三级地区 市辖区、县级市、县
ListthreeLevelRegion = mAreaCodeList.stream().filter(threeItem ->
StringUtils.equals(String.valueOf(threeItem.getParentCode()) , String.valueOf(twoItem.getCode())))
.collect(Collectors.toList());
twoItem.setMAreaCodeList(threeLevelRegion);
});
oneItem.setMAreaCodeList(twoLevelRegion);
}
);
result.setData(oneLevelRegion);
(方法二:耗时比较短)
ListmAreaCodeList2 = mAreaCodeService.selectList();
ListresultList = new ArrayList<>(32);
MapareaMap = new HashMap<>(mAreaCodeList2.size());
mAreaCodeList2.forEach(area ->{
MAreaCodeVo vo = areaMap.get(area.getId());
if(vo==null){
vo= new MAreaCodeVo();
vo.setMAreaCodeList(new ArrayList<>());
areaMap.put(area.getCode(),vo);
}
vo.setId(area.getId());
vo.setCode(area.getCode());
vo.setAreaName(area.getAreaName());
vo.setParentCode(area.getParentCode());
Integer parentId = area.getParentCode();
if(parentId >0){
MAreaCodeVo parentVo = areaMap.get(parentId);
if(Objects.isNull(parentVo)){
parentVo = new MAreaCodeVo();
parentVo.setId(parentId);
parentVo.setMAreaCodeList(new ArrayList<>());
areaMap.put(vo.getId(),parentVo);
}
parentVo.getMAreaCodeList().add(vo);
}else{
resultList.add(vo);
}
});
result.setData(resultList);
4 返回的数据
{
"id": 12,
"code": 110000,
"parentCode": 0,
"areaName": "北京市",
"pinyin": null,
"mareaCodeList": [
{
"id": 425,
"code": 110100,
"parentCode": 110000,
"areaName": "北京市",
"pinyin": null,
"mareaCodeList": [
{
"id": 814,
"code": 110101,
"parentCode": 110100,
"areaName": "东城区",
"pinyin": null,
"mareaCodeList": null
},
{
"id": 815,
"code": 110102,
"parentCode": 110100,
"areaName": "西城区",
"pinyin": null,
"mareaCodeList": null
},
{
"id": 816,
"code": 110105,
"parentCode": 110100,
"areaName": "朝阳区",
"pinyin": null,
"mareaCodeList": null
},
{
"id": 817,
"code": 110106,
"parentCode": 110100,
"areaName": "丰台区",
"pinyin": null,
"mareaCodeList": null
},
{
"id": 818,
"code": 110107,
"parentCode": 110100,
"areaName": "石景山区",
"pinyin": null,
"mareaCodeList": null
},
{
"id": 819,
"code": 110108,
"parentCode": 110100,
"areaName": "海淀区",
"pinyin": null,
"mareaCodeList": null
},
{
"id": 820,
"code": 110109,
"parentCode": 110100,
"areaName": "门头沟区",
"pinyin": null,
"mareaCodeList": null
},
{
"id": 821,
"code": 110111,
"parentCode": 110100,
"areaName": "房山区",
"pinyin": null,
"mareaCodeList": null
},
{
"id": 822,
"code": 110112,
"parentCode": 110100,
"areaName": "通州区",
"pinyin": null,
"mareaCodeList": null
},
{
"id": 823,
"code": 110113,
"parentCode": 110100,
"areaName": "顺义区",
"pinyin": null,
"mareaCodeList": null
},
{
"id": 824,
"code": 110114,
"parentCode": 110100,
"areaName": "昌平区",
"pinyin": null,
"mareaCodeList": null
},
{
"id": 825,
"code": 110115,
"parentCode": 110100,
"areaName": "大兴区",
"pinyin": null,
"mareaCodeList": null
},
{
"id": 826,
"code": 110116,
"parentCode": 110100,
"areaName": "怀柔区",
"pinyin": null,
"mareaCodeList": null
},
{
"id": 827,
"code": 110117,
"parentCode": 110100,
"areaName": "平谷区",
"pinyin": null,
"mareaCodeList": null
},
{
"id": 828,
"code": 110118,
"parentCode": 110100,
"areaName": "密云区",
"pinyin": null,
"mareaCodeList": null
},
{
"id": 829,
"code": 110119,
"parentCode": 110100,
"areaName": "延庆区",
"pinyin": null,
"mareaCodeList": null
}
]
}
]
}
java遍历二级城市_java8用Lambda遍历省市区三级数据相关推荐
- Java初始化省市区三级数据
使用Jsoup爬虫工具获取全国地区数据(省市县镇村) 最近新做一个项目,要在数据库初始化省市区三级数据,所以在网上找了个爬虫工具,从国家统计局区划代码网站爬取了相关数据.具体原理不解释了,只要能实现功 ...
- Java 9 揭秘(16. 虚拟机栈遍历)
Tips 做一个终身学习的人. 在本章中,主要介绍以下内容: 什么是虚拟机栈(JVM Stack)和栈帧(Stack Frame) 如何在JDK 9之前遍历一个线程的栈 在JDK 9中如何使用Stac ...
- Java中Map集合的三种遍历方式
文章目录 Map集合的遍历方式 Map集合的遍历方式一: 键找值 Map集合的遍历方式二: 键值对 Map集合的遍历方式三: Lambda Map集合的遍历方式 Map集合的遍历方式有3种: 方式一: ...
- java map集合遍历方法,Java的Map集合的三种遍历方法
集合的一个很重要的操作---遍历,学习了三种遍历方法,三种方法各有优缺点~~ 1. package com.myTest.MapText; import java.util.Collection; i ...
- [Leedcode][JAVA][第102题][二叉树的层序遍历][递归][迭代][BFS]
[问题描述][第102题][二叉树的层序遍历][中等] 给你一个二叉树,请你返回其按 层序遍历 得到的节点值. (即逐层地,从左到右访问所有节点).示例: 二叉树:[3,9,20,null,null, ...
- Java中 List、Set、Map遍历方式以及性能比较
目录 一.简介 二.遍历方式 1.ArrayList遍历方式 (1)for循环遍历 (2)foreach循环遍历 (3)Iterator迭代器遍历 2.LinkedList遍历方式 (1)for循环遍 ...
- java后台解析json并保存到数据库_[Java教程]ajax 发送json 后台接收 遍历保存进数据库...
[Java教程]ajax 发送json 后台接收 遍历保存进数据库 0 2017-09-25 15:00:23 前台怎么拿参数的我就不管了我也不会 反正用这个ajax没错 ajax 代码 一定要写 ...
- Java深入了解TreeSet,和迭代器遍历方法
Map集合:链接: Map集合的五种遍历方式及Treemap方法 Set集合:链接: Java中遍历Set集合的三种方法 TreeSet集合:链接: Java深入了解TreeSet,和迭代器遍历方法 ...
- Java中List集合的三种遍历方式(全网最详)
Map集合:链接: Map集合的五种遍历方式及Treemap方法 Set集合:链接: Java中遍历Set集合的三种方法 TreeSet集合:链接: Java深入了解TreeSet,和迭代器遍历方法 ...
- 【Java面试题】List如何一边遍历,一边删除?
这是最近面试时被问到的1道面试题,本篇博客对此问题进行总结分享. 1. 新手常犯的错误 可能很多新手(包括当年的我,哈哈)第一时间想到的写法是下面这样的: public static void mai ...
最新文章
- 微软网站打不开_强烈建议收藏,微软出了自家「协作白板」应用
- Graph Search图谱搜索
- swap的实现(没有中间变量)
- 求数组最小值及其下标
- 五分钟学会企业的OpenStack(T版)——简介及安装方式
- 郝斌--数据结构---汉诺塔实现(c语言实现)
- 在asp.net中实现回车替代Tab键
- 一些奇妙的线段树操作
- 665. Non-decreasing Array - LeetCode
- 论文解读丨LayoutLM: 面向文档理解的文本与版面预训练
- c++中的fork函数_fork函数的作用_fork函数创建进程
- c语言简单图形库,C语言图形库简单对比及EGE库的安装小手册
- LDN双模键盘常见问题(FAQ)
- element audio 标签 不显示_不闪屏,HDR,带鱼屏全都有,LG 29WK600宽屏显示器测评...
- 移动办公软件,VIP163邮箱手机版的登陆方式有哪些?
- 「数字电子技术基础」2.数制和码制
- CodeForces 3-B Lorry
- 算法-基于成交量的Adaboost股价涨跌预测模型
- 如何提高串口通信速度
- 五分钟带你玩转docker(三)全网最新最简单docker安装方式,楼主亲测
热门文章
- 发生在我们身边的灵异事件 - 发生在台湾奇萊山的一些灵异事件
- python 语言基本知识2:数据结构
- C语言2009选择题答案,全国2009年10月自学考试C加加程序设计试题
- 鸿蒙时代实力排名,混沌氏(浑沌)、鸿蒙氏,盘古开天辟地时两个最强大的部落首领?...
- DataWindow的数据更新技术及应用
- pandas报错:columns overlap but no suffix specified
- repo sync x509: certificate is valid for,外部过滤器失败,smudge过滤器lfs失败,cannot initialize work tree
- 经济学计算机会成本和贸易区直的题,管理经济学2017年4月真题(02628)
- 使用python的模拟退火算法估计heston期权定价模型的五个参数(新)
- 魅族手机无限网无法连接服务器,魅族手机wifi为何连接不了了