java根据字段值合并两个list

  • Post author:
  • Post category:java


核心算法是,两个for循环,遍历list,然后用map的去重特性,赋值,拿到新的list

package com.example.demo2;

import com.example.demo2.entity.*;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


@SpringBootTest(classes = {Demo2Application.class}, properties = {"application.properties"})
public class Test {
    @org.junit.Test
    public void test() throws ParseException {
        /*说明:以下是两个list,唯一标志是unicast的值,每一个按钮,对应一个unicast的值,
        展示的是第一个列表,但后端没有返回按钮的打开和关闭状态,需要去第二个表里面查找,
        把true状态,覆盖到第一个表里面。
        这里涉及到表的合并,借助两个for循环,比对判断 ,然后更新ui状态
          {
                "code":200,
                "data":[
                    {
                        "id":325,
                        "gateway":"2903381",
                        "unicast":"72 02",
                        "pasterName":"单开随意贴(遥控器)",
                        "pid":"A7C131",
                        "powerItems":"[{\"unicast\":\"C172\",\"name\":\"按键1\",\"isOn\":0}]"
                    }
                ],
                "message":"查询成功"
            }
            {
                "code":200,
                "data":[
                    {
                        "select":true,
                        "pasterId":"2903381_C16D",
                        "name":"三开随意贴(遥控器)_按键3",
                        "pid":"A7C130"
                    },
                    {
                        "select":true,
                        "pasterId":"2877042_C158",
                        "name":"双开随意贴(遥控器)_按键1",
                        "pid":"A7C132"
                    }
                ],
                "message":"查询成功"  58  59  6d 77
            }
        * */

        List<PowerItemsBean> list = new ArrayList<>();
        list.add(new PowerItemsBean("C158", "按键1", 0, false));
        list.add(new PowerItemsBean("C159", "按键2", 0, false));
        list.add(new PowerItemsBean("C16D", "按键3", 1, false));
        List<SetSuiStatusBean.DataBean> list2 = new ArrayList<>();
        list2.add(new SetSuiStatusBean.DataBean(true, "2903381_C16D", "三开随意贴(遥控器)_按键3", "a7c130"));
        list2.add(new SetSuiStatusBean.DataBean(false, "2903381_C158", "单开随意贴(遥控器)_按键2", "a7c132"));
        list2.add(new SetSuiStatusBean.DataBean(true, "2903381_C177", "双开随意贴(遥控器)_按键1", "a7c131"));

        System.out.println(list + "\t" + list2);
        /*
         * 好像是说弄两个for循环  存入一个新表中
         * */
        HashMap<String, PowerItemsBean> map = new HashMap();
        for (int i = 0; i < list.size(); i++) {
            map.put(list.get(i).getUnicast(), new PowerItemsBean(list.get(i).getUnicast(), list.get(i).getName(), list.get(i).getIsOn(), list.get(i).isSelect()));
        }
        for (int j = 0; j < list2.size(); j++) {
            map.put(getPasterData(list2.get(j).getPasterId()), new PowerItemsBean(getPasterData(list2.get(j).getPasterId()), getPasterData(list2.get(j).getName()), 0, list2.get(j).isSelect()));

        }
        System.out.println(map);
    }

    private String getPasterData(String data) {
        String[] times = data.split("_");
        return times[1];
    }
}



版权声明:本文为cf8833原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。