-
获取List<Map>中的Key值,返回List<String>
2017-07-31 17:52:31【需求】:获取List数据集中每项的key值,如List=[{测试1:测试一,测试2:测试二},{测试1:测试三,测试2:测试四},{测试1:测试五,测试2:测试六},{测试1:测试七,测试2:测试八}] 返回结果List=测试1,测试2 ...【需求】:获取List<Map>数据集中每项的key值,如List<map>=[{测试1:测试一,测试2:测试二},{测试1:测试三,测试2:测试四},{测试1:测试五,测试2:测试六},{测试1:测试七,测试2:测试八}]
返回结果List<String>=测试1,测试2
【解决】:
public List<String> GetMapKey(List<Map> listResult) { if ((listResult != null) && (!listResult.isEmpty())) { List listKey = new ArrayList(); Map mapResult = (Map)listResult.get(0); Set mapKeySet = mapResult.keySet(); String listHead = ""; Iterator iteratorKey = mapKeySet.iterator(); while (iteratorKey.hasNext()) { listHead = iteratorKey.next(); listKey.add(listHead); } return listKey; } return null; }
-
获取Map中Key和Value的值
2020-04-03 16:59:31在日常编码中,我们经常会用到Map和HashMap来处理数据,今天我遇到了一个关于获取Map中key的问题,在这给大家分享一下: 业务场景: //将Map放入list中 List<Map<String, String>> ListMap = new ...在日常编码中,我们经常会用到Map和HashMap来处理数据,今天我遇到了一个关于获取Map中key的问题,在这给大家分享一下:
业务场景:
//将Map放入list中 List<Map<String, String>> ListMap = new ArrayList<>(); for (Item item : items) { Map<String, String> map = new HashMap<>(); map.put(item.itemCode, item.code); ListMap.add(map); } //现在想从ListMap中,根据Map的key值进行判断,对符合条件的Map获取其相应的value值 for (int i=0;i<ListMap .size();i++){ Map<String, String> map= ListMap.get(i); for (Map.Entry<String,String> entry: map.entrySet()) { //获取key String mk=entry.getKey(); if ( mk.equals("001s")) { //获取value String m = entry.getValue(); ...... } } }
小记一下,希望能够给大家提供帮助,如果大家有更好的方法来处理这个问题,欢迎在下方留言。
-
java获取map中的key值
2019-11-23 08:53:27java 获取map中的key值 public static String getKeySet(Map map){ List<String> list= new ArrayList<>(); Set set = map.keySet(); Iterator it = set.iterator(); while(it.hasNext()){ list.... -
随机获取一个集合(List, Set)中的元素,随机获取一个Map中的key或value
2019-12-23 10:50:51从List或Set中随机取出一个元素,从Map中随机获取一个key或value。 因为Set没有提供get(int index)方法,仅仅能先获取一个随机数后。利用一个计数器,对Set进行循环,当计数器等于随机数时返回当前元素,对于Map的...利用Java提供的Random类。从List或Set中随机取出一个元素,从Map中随机获取一个key或value。
因为Set没有提供get(int index)方法,仅仅能先获取一个随机数后。利用一个计数器,对Set进行循环,当计数器等于随机数时返回当前元素,对于Map的处理也类似。
不知有没有更好的方法……
package com.xjj.util; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ThreadLocalRandom; /** * 随机数工具,单例模式 * @author XuJijun * */ public class RandomUtils { //private static Random random; //双重校验锁获取一个Random单例 public static ThreadLocalRandom getRandom() { return ThreadLocalRandom.current(); /*if(random==null){ synchronized (RandomUtils.class) { if(random==null){ random =new Random(); } } } return random;*/ } /** * 获得一个[0,max)之间的随机整数。 * @param max * @return */ public static int getRandomInt(int max) { return getRandom().nextInt(max); } /** * 获得一个[min, max]之间的随机整数 * @param min * @param max * @return */ public static int getRandomInt(int min, int max) { return getRandom().nextInt(max-min+1) + min; } /** * 获得一个[0,max)之间的长整数。 * @param max * @return */ public static long getRandomLong(long max) { return getRandom().nextLong(max); } /** * 从数组中随机获取一个元素 * @param array * @return */ public static <E> E getRandomElement(E[] array){ return array[getRandomInt(array.length)]; } /** * 从list中随机取得一个元素 * @param list * @return */ public static <E> E getRandomElement(List<E> list){ return list.get(getRandomInt(list.size())); } /** * 从set中随机取得一个元素 * @param set * @return */ public static <E> E getRandomElement(Set<E> set){ int rn = getRandomInt(set.size()); int i = 0; for (E e : set) { if(i==rn){ return e; } i++; } return null; } /** * 从map中随机取得一个key * @param map * @return */ public static <K, V> K getRandomKeyFromMap(Map<K, V> map) { int rn = getRandomInt(map.size()); int i = 0; for (K key : map.keySet()) { if(i==rn){ return key; } i++; } return null; } /** * 从map中随机取得一个value * @param map * @return */ public static <K, V> V getRandomValueFromMap(Map<K, V> map) { int rn = getRandomInt(map.size()); int i = 0; for (V value : map.values()) { if(i==rn){ return value; } i++; } return null; } /** * 生成一个n位的随机数,用于验证码等 * @param n * @return */ public static String getRandNumber(int n) { String rn = ""; if (n > 0 && n < 10) { //Random r = new Random(); StringBuffer str = new StringBuffer(); for (int i = 0; i < n; i++) { str.append('9'); } int num = Integer.parseInt(str.toString()); while (rn.length() < n) { rn = String.valueOf(ThreadLocalRandom.current().nextInt(num)); } } else { rn = "0"; } return rn; } public static void main(String[] args) { /*Set<String> set = new HashSet<>(); for (int i = 0; i < 12; i++) { set.add("I am: " + i); } for (int i = 0; i < 10; i++) { System.out.println(getRandomElement(set)); }*/ System.out.println(getRandom().nextInt(-100, 10)); } }
-
Mybaits中获取参数Map中的key和value
2020-11-28 15:48:051.先了解一下mybatis 中 foreach 的用法 mybatis的foreach标签经常用于遍历集合,构建in条件语句... 表示本次迭代获取的元素,若collection为List、Set或者数组,则表示其中的元素;若collection为map,则... -
scala中获取Map中key和value的方法
2017-07-14 16:23:00val scores=Map("Alice"->10,"Bob"->3,"Cindy"->...// 获取所有的key val nameList=scores.map(_._1) // map 函数返回List println(nameList.getClass) 遍历list中的元素 nameList.foreac... -
java 随机map_随机获取一个集合(List, Set)中的元素,随机获取一个Map中的key或value...
2021-02-12 19:36:03从List或Set中随机取出一个元素,从Map中随机获取一个key或value。因为Set没有提供get(int index)方法,仅仅能先获取一个随机数后。利用一个计数器,对Set进行循环,当计数器等于随机数时返回当前元素,对于Map的... -
java map 随机取值_随机获取一个集合(List, Set)中的元素,随机获取一个Map中的key或value...
2021-02-26 09:02:41从List或Set中随机取出一个元素,从Map中随机获取一个key或value。因为Set没有提供get(int index)方法,仅仅能先获取一个随机数后。利用一个计数器,对Set进行循环,当计数器等于随机数时返回当前元素,对于Map的... -
对list中map中的key值进行排序
2019-01-16 16:59:00//对时间戳进行排序 获取最新的mapList<Map<String, Object>> dateList = new ArrayList<Map<String, Object>>(); Map<String, Object> paramsValue = new Has... -
JSP页面中获取Map集合的所有Key值
2018-07-25 19:02:15集合来封装数据,通常在Jsp页面中获取Map集合的值都是已知的,但是这次不同,这次的Map集合中的Key值不单单是一个字符串,而是一个实体类集合,需要从List<Map>中获取到这个实体类,也就是key值。 ... -
Java中Map获取key和value
2018-06-26 17:33:19Java Map获取key和value的方法 if (null != list && !list .isEmpty()) { for (Map<String,Object> m : list ) { Iterator iterator=m.entrySet().iterator(); Entry entry=... -
获取map中根据key进行排序的value数据
2015-07-09 09:12:30今天做的项目中遇到了获取map中...看看之前同事写的map根据key排序的代码,发现他是先把key都取出来封装到一个List中,然后用Collections.sort方法对List排序,按照排完序的顺序去除value顺序加入到一个新的List中。... -
vue获取map集合中的key、value值
2020-09-01 15:00:00背景:前端接收后端返回的list集合,并将该集合展示在页面。 //例如: let _list = [{"name":"小一", "age":10, "id":1},{"name":"小二", "age":15, "id":2},{"name":"小三", "age":9, "id":3}] 方案一,直接在HTML... -
Map中获取key-value值的方法
2021-02-13 17:56:25当调用put(K key,V value)方法吧数据存到Map中后,那么如何把Map中的key值和value值取出来呢?都有哪几种取值的方法呢?下边就来一一介绍。 一、前置准备 以HashMap为例,先为map中存入几个数据,以便于后边对map的... -
获取Map中最大(小)的key
2018-08-15 14:04:43将Map中的key存放至set集合中,... * 获取map中最大的key * @param map * @return */ public static Integer getMaxKey(Map<Integer, List<User>> map) { if (map == null){... -
获取map中最小的value值的key
2019-04-28 15:21:36Map<String, Integer> map = new HashMap<>(); map.put("o", 4); map.put("t", 2); map.put("s", 1); List<Map.Entry<String, Integer>... list = new ArrayList(map.entrySet()); // list.s... -
如何给List中的map进行排序
2018-11-23 16:32:01解决办法,通过获取List中map的key值,然后进行排序 Collections.sort(list, new Comparator<Map<String, Object>>() { public int compare(Map<String, Object&... -
在JavaScript中获取Map集合中的key和value值(前提是:既不知道key为什么值,也不知道value有哪些值)
2016-09-12 10:29:36在JavaScript中获取Map集合中的key和value值(前提是:既不知道key为什么值,也不知道value有哪些值) Map> //遍历map集合 获取value值相关信息 for(var k in map){ //通过定义一个局部变量k遍历获取到了map中... -
嵌套Map或者List获取key、value值
2011-04-11 15:20:40一些特定情况下需要用map或者list嵌套 这里是一个例子 如何获取嵌套中的参数 -
Springboot项目获取配置文件中的Map和List值
2020-09-04 18:31:03形式的,那么也就是说,我们获取到配置文件中的key,那么对应的value值就会被拿到。 获取对应的Map值: 首先引入依赖: <!--增加读取配置文件中的map --> <dependency> <groupId>org.spring... -
前台中list结合怎么遍历_map、list、map(Object,map)的前台获取遍历
2021-02-05 06:34:58[code]一、map后台代码:[code]Map map2 = new HashMap();map2.put("a","hello world");map2.put("b","this is map");request.setAttribute("map2",map2);前台代码:[code]${item.key} > 或者${item['a']}${item.... -
el表达式遍历list中的list_EL表达式获取map和list集合中的值 | 学步园
2020-12-19 18:38:08Map的每个对象以key=value的形式给出当forEach tag的item属性中的表达式的值是java.util.Map时,在var中命名的变量被设置为类型是java.util.Map.Entity的item。这时,迭代变量被称为entity,因此,用表达式${entity.... -
java List里面放的很多map, map的key,value都不一样,如何遍历效率最高
2017-09-13 01:44:21java 有很多ArrayList,并且每个list里面放的很多map , 并且map的key,value都不一样,如何遍历获取每个list中想要的几个map,效率最高 -
js中Map的key为一个变量
2017-01-16 18:04:02js中,Map的key为一个变量,此时可以通过map[变量]来获取对应的值。 var mapList = [{key=111,value=abc},{key=222,value=def}] var otherMapList = [{111=abc111},{222=def222}] for(var i in otherMapList){ ... -
在JavaScript中获取Map集合中的key和value值(前提是:既不知道key为什么值,也不知道value有哪些值)...
2019-09-17 19:30:45在JavaScript中获取Map集合中的key和value值(前提是:既不知道key为什么值,也不知道value有哪些值) Map<String,List<Document>> //遍历map集合 获取value值相关信息 for(var k in map){...
收藏数
423
精华内容
169
-
牛牛量化策略交易
-
基于FPGA的verilog语言的四位全加器
-
天池语义分割task04
-
题目3:文本文件单词的检索与计数(实验准备)
-
20210301resume.zip
-
Flume--相关图例
-
(2021年2月18日整理)自媒体全套教程+全套工具(带操作教程)+原创实操教程+N个素材网站+赠全套副业实操课程
-
【Redis】Java操作Redis:RedisTemplate 关于五种数据类型的 API
-
560_linux内核学习_sched.c文件sched_init函数分析
-
MySQL 管理利器 mysql-utilities
-
第四次CCF计算机软件能力认证 节日 (模拟)
-
leetcode 内存访问问题本地复制方法 ddressSanitizer: SEGV on unknown address 0x000000000000
-
Tecplot 360 EX 2020 Release 2 User's Manual 用户手册
-
jdk-9.0.4_Wind-x64.zip
-
【shell脚本】
-
【Redis】缓存穿透,缓存击穿,缓存雪崩及解决方案
-
辅助控制器来料检验规范模版.docx
-
混合动力系列轿车出厂检验规范.docx
-
数据库面试题【十四、主键使用自增ID还是UUID】
-
信息安全风险评估与风险管理.ppt