`

listToMap

 
阅读更多
/**
* List实用类
*
* @author vernon.chen
*
*/
public class BeanUtils extends org.apache.commons.collections.ListUtils {

/**
* <p>
* 将List变为Map
* </p>
*
* @param list
* @param <V>
*            list的项目
* @param keyField
*            作为Map的项目
* @return 变换成的Map
* @throws Exception
*             keyField项目没有找到的话,抛出异常
*/
public static <V> Map<String, V> listToMap(
List<V> list,
String keyField)
{
Map<String, V> map = new HashMap<String, V>();
for (V obj : list) {
String key = null;
try {
key = BeanUtils.getProperty(obj, keyField);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
map.put(key, obj);
}
return map;
}
}
分享到:
评论

相关推荐

    js模拟list和map

    javascript 模拟 java中的 List,Map js文件为 js/utils.js IE6.0 测试通过 &lt;br&gt;List: add(var obj) //添加一个元素 remove(var index) //删除一个元素 get(var index) //获取一个元素 remove...

    Java中 List《map》把里面map中某个key相同记录的其他key 的 value 合并

    Java中 List《map》把里面map中某个key相同记录的其他key 的 value 合并 Java中 List《map》把里面map中某个key相同记录的其他key 的 value 合并

    Google转Map图层

    Google转Map图层

    在Java 8中将List转换为Map对象方法

    主要介绍了在Java 8中将List转换为Map对象方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

    StringtoList和StringtoMap和StringtoObject和StringtoArray

    Json格式的字符串转成List、Array、Map、Object、List等,就是各种字符串转其他格式的方法.有个封装好的转换方法和一个我自己做的示例

    json字符串转成 Map/List

    NULL 博文链接:https://wuniu2010.iteye.com/blog/1771934

    驼峰处理,map过滤

    查询返回map或list&lt;Map&gt;时,针对大小写以及下划线,横杠(USER_NAME,USER-NAME),转化为标准的java变量类型userName,支持互相转化。

    map 和 bean 之间的转换

    map 和 bean 之间的转换,可以利用反射的原理实现

    list转String

    String list 转化为 String, 使用Stringbuffer 。。。。。。。。。。。。。。。

    Map to List

    NULL 博文链接:https://cxwitjy.iteye.com/blog/1456617

    jackson json 转 map 数值科学计数法的问题解决.docx

    将json转换为map之后,如果数值类型太多会在转换之后出现科学计数法的问题,经研究jackson源码、网上百度资料得知可以通过设置一些参数解决该问题

    java面试宝典

    192、Can I use Hidden form field to track session? 45 194、In a architecture pattern like MVC, it is mandatory that Servlet should be the controller, why not JSP? 46 195、Why JSP is used as View part ...

    unordered-list-to-mind-map:使用kityminder-core将html无序列表转换为思维导图的示例

    unordered-list-to-mind-mapAn example that converts html unordered list to mind map using kityminder-core使用百度脑图的 kityminder-core,将 HTML 无序列表转换为思维导图的示例How to UseYou will find the ...

    Json工具类

    接口调用时,数据的交互传输,有toObject,toJson,toList,toMap

    python 基础教程之Map使用方法

    Map的规范为:map(function_to_apply, list_of_inputs) 大多数时候,我们需要将列表中的所有元素一个个传递给一个函数,并收集输出。例如: items = [1, 2, 3, 4, 5] squared = [] for i in items: squared....

    Java中快速把map转成json格式的方法

    主要介绍了Java中快速把map转成json格式的方法,本文使用json-lib.jar中的JSONSerializer.toJSON方法实现快速把map转换成json,需要的朋友可以参考下

    RoadMapiOS.pdf 苹果官方

    the last tutorial, you’ll have created a simple to-do list app. After you’ve built your first app in this guide and are considering your next endeavor, read the fourth module. It explores the ...

    3parinfo_host_map_lun

    • List of LUNs exposed to hosts • Information about a specific LUN that is exposed to the host.

    exceltojson.zip

    excel数据替代接口数据转json,目前还在完善,list集合下只能放一个map

    jdk1.8.0_202.zip

    java 开发者 配置jdk1.8 一下是jdk1.8的... List&lt;Integer&gt; c = names.stream.map(x-&gt;x.length()).collect(Collectors.toList()); List&lt;Integer&gt; c = names.stream.map(String::length).collect(Collectors.toList());

Global site tag (gtag.js) - Google Analytics