`

List -> Map<String, Map<String, Striing>> demo

阅读更多
package com.ctrip.framework.apollo.demo;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

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

/**
 * @author maxiaolong
 * @create 2018-07-17 10:12
 **/
public class StreamDemo {

  public static void main (String[] args) {
    convert();
  }

  private static void convert () {
    List<Bean> beanList = Lists.newArrayList(new Bean("k1", "f1", "v1"),
            new Bean("k1", "f2", "v2"), new Bean("k2", "f2", "v3"));
    Map<String, Map<String, String>> resultMap = beanList.stream().collect(Collectors.toMap(bean -> bean.getKey(),
            bean -> {
              Map map = Maps.newHashMap();
              map.put(bean.getField(), bean.getValue());
              return map;
            }, (oldValue, newValue) -> {
                oldValue.putAll(newValue);
                return oldValue;
            }));
    resultMap.forEach((key, value) -> System.out.println("key: " + key + "; value: " + value));
  }

  static class Bean {

    private String key;

    private String field;

    private String value;

    public Bean (String key, String field, String value) {
      this.key = key;
      this.value = value;
      this.field = field;
    }

    public String getKey() {
      return key;
    }

    public void setKey(String key) {
      this.key = key;
    }

    public String getField() {
      return field;
    }

    public void setField(String field) {
      this.field = field;
    }

    public String getValue() {
      return value;
    }

    public void setValue(String value) {
      this.value = value;
    }
  }

}

 

分享到:
评论

相关推荐

    Mybatish和Ajax笔记

    public Map&lt;String, Object&gt; handleAjaxRequest(@RequestParam Map&lt;String, String&gt; params) { // 处理业务逻辑 List&lt;Movie&gt; movies = movieDao.getMovies(params); return Collections.singletonMap("movies", ...

    结合SpringMVC和Hibernate-validator,自动生成前台的js验证代码.pdf

    List&lt;Map&lt;String, String&gt;&gt; rules = new ArrayList&lt;&gt;(); // 遍历模型类的字段并处理验证注解 for (Field field : clazz.getDeclaredFields()) { // 处理验证注解,生成验证规则 // ... } // 生成并输出...

    基于注解的springMVC的demo

    List&lt;Student&gt; stuList = new ArrayList&lt;&gt;(); stuList.add(student); stuList.add(student); return stuList; //返回list对象,将会被自动转换为JSON格式 } ``` - **@ResponseBody**:将方法的返回值直接写入...

    jsf实现基于ajax的上传例子

    List&lt;FileItem&gt; fileItems = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(httpRequest); for (FileItem fileItem : fileItems) { if (!fileItem.isFormField()) { String fileName = ...

    Spring Boot高级教程之Spring Boot连接MySql数据库

    List&lt;Map&lt;String, Object&gt;&gt; list = jdbcTemplate.queryForList(sql); for (Map&lt;String, Object&gt; map : list) { Set&lt;Entry&lt;String, Object&gt;&gt; entries = map.entrySet(); if(entries != null) { Iterator&lt;Entry...

    Freemarker小demo

    &lt;#list data.array as item&gt; &lt;li&gt;${item.property}&lt;/li&gt; &lt;/#list&gt; &lt;/ul&gt; &lt;!-- 示例HTML截取 --&gt; &lt;div&gt;${truncateHtml(originalHtml, 100)}&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; ``` 这个模板展示了如何展示JSON数据中的...

    谷歌地图 delphi 封装库 2013 0.1.9 全面支持google maps api

    - Bug fixed: TGMMap -&gt; bug fixed on RemoveLinkedComponent when trying delete an object without being the list created. - Bug fixed: JavaScript =&gt; when it had figures of different types together, ...

    cms后台管理

    protected List&lt;Content&gt; getList(Map&lt;String, TemplateModel&gt; params, Environment env) throws TemplateException { Integer[] ids = DirectiveUtils.getIntArray(PARAM_IDS, params); if (ids != null) { //...

    springboot mybatis mapper.xml 配置

    public List&lt;User&gt; getUserListByPage(Map&lt;String, Object&gt; params) { return userMapper.getUserListByPage(params); } // 其他服务方法 } ``` 最后,通过创建一个控制器类,我们将这些服务暴露给HTTP请求:...

    config4j

    返回 List&lt;ConfigNode&gt; &lt;br&gt; * ROOT.NODE.CNODE:$value 末结点的TEXT值 返回 String &lt;br&gt; * ROOT.NODE.CNODE:$node 末结点的结点 返回 ConfigNode &lt;br&gt; * ROOT.NODE.CNODE:$properties 末结点的属性表(MAP) 返回 Map...

    springboot && JPA实现增删改查demo

    public List&lt;Girl&gt; getAllGirls() { return girlRepository.findAll(); } public Optional&lt;Girl&gt; getGirlById(Long id) { return girlRepository.findById(id); } public void deleteGirlById(Long id) { ...

    springMVC 多视图配置(Jsp Freemarket Json)实例

    Map&lt;String, String&gt; responseMap = new HashMap&lt;&gt;(); responseMap.put("message", "Hello from JSON"); return responseMap; } } ``` 这个例子展示了如何通过不同的视图解析器,根据请求的 URL 返回 JSP、...

    安卓最简单的aidl用两个程序模拟进程通信demo

    AIDL支持基本数据类型和自定义数据类型,以及List、Map等集合类型。 然后,Android Studio或Eclipse会根据`IService.aidl`生成对应的Java接口类`IService.java`,这个类在服务端和客户端都可以使用。服务端需要实现...

    spring定时器

    &lt;property name="jobClass" value="onlyfun.bb.Quartz.QuartzDemo"&gt;&lt;/property&gt; &lt;property name="jobDataAsMap"&gt; &lt;map&gt; &lt;entry key="jobData" value-ref="someData"&gt;&lt;/entry&gt; &lt;/map&gt; &lt;/property&gt; &lt;/bean&gt; ...

    Android 解析ios的plist 城市文件

    Map&lt;String, String&gt; result = new HashMap&lt;&gt;(); XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser parser = factory.newPullParser(); parser.setInput(is, "UTF-8"); int...

    springboot整合activiti完整demo

    Map&lt;String, Object&gt; variables = new HashMap&lt;&gt;(); variables.put("assignee", "username"); startProcess(businessKey, variables); ``` **5. 流程实例的查询与控制** - **查询流程实例**:使用`TaskService`...

    Springboot集成MyBatis自动生成代码工具源码

    List&lt;String&gt; warnings = new ArrayList&lt;&gt;(); boolean overwrite = true; InputStream in = MyBatisGeneratorMain.class.getResourceAsStream("/generatorConfig.xml"); ConfigurationParser cp = new ...

    httpclient发送get请求和post请求demo

    List&lt;BasicNameValuePair&gt; params = new ArrayList&lt;&gt;(); params.add(new BasicNameValuePair("key1", "value1")); params.add(new BasicNameValuePair("key2", "value2")); httpPost.setEntity(new ...

    jfinalpluginsjfinal-dreampie.zip

     public Map&lt;String, AuthzHandler&gt; getJdbcAuthz(); } 13.shiro的freemarker标签库 &lt;@shiro.hasPermission name="P_USER"&gt;  &lt;li&gt;&lt;a href="/admin/user"&gt;${i18n.getText("admin.user")}&lt;/a&gt;&lt;/li&gt;  ...

Global site tag (gtag.js) - Google Analytics