利用HashMap辅助调整对象结构

来源:互联网 发布:金属探测软件 编辑:程序博客网 时间:2024/06/16 01:55

将Person类firstName相同的人合并到一个PersonResponse对象,List<Person> --> List<PersonResponse>。这个需求可能没有太大实际意义,但是重要的是理解为什么要用到HashMap,不用HashMap可以怎么做,区别在哪里。

Person类:

/** * person类 * @author xiaowei 2017年11月14日 下午2:11:14 */public class Person {    private String  firstName;    private String  lastName;    private Integer age;    public String getFirstName() {        return firstName;    }    public void setFirstName(String firstName) {        this.firstName = firstName;    }    public String getLastName() {        return lastName;    }    public void setLastName(String lastName) {        this.lastName = lastName;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    @Override    public String toString() {        return "Person [firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + "]";    }}

PersonResponse类:

import java.util.ArrayList;import java.util.List;/** * person返回对象 * @author xiaowei 2017年11月14日 下午2:12:30 */public class PersonResponse {    private String       firstName;    /** personList */    private List<Person> list;    public String getFirstName() {        return firstName;    }    public void setFirstName(String firstName) {        this.firstName = firstName;    }    public List<Person> getList() {        if (list == null) {            list = new ArrayList<>();        }        return list;    }    public void setList(List<Person> list) {        this.list = list;    }    @Override    public String toString() {        return "PersonResponse [firstName=" + firstName + ", list=" + list + "]";    }}

下面为了方便直接在main方法中演示:

import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * HashMap用途之一:利用key不重复特性 * @author xiaowei 2017年11月14日 下午2:08:31 */public class HashMapUsage {    /**     * 将firstName相同的人合并到一个PersonResponse对象     */    public static void main(String[] args) {        // Person list 模拟查询数据库的结果集,并已根据firstName排序        List<Person> personList = new ArrayList<>();        Person p = new Person();        p.setAge(20);        p.setFirstName("李");        p.setLastName("明");        personList.add(p);        p = new Person();        p.setAge(29);        p.setFirstName("李");        p.setLastName("白");        personList.add(p);        p = new Person();        p.setAge(22);        p.setFirstName("小");        p.setLastName("明");        personList.add(p);        p = new Person();        p.setAge(30);        p.setFirstName("小");        p.setLastName("帅");        personList.add(p);        // 结果list        List<PersonResponse> resList = new ArrayList<PersonResponse>();        // personList不可能为null,可能是空集合        if (personList.isEmpty()) {            System.out.println("查询结果为空!");            return;        }        // List<Person> --> List<PersonResponse>        PersonResponse res = new PersonResponse();        Map<String, String> resMap = new HashMap<String, String>();        for (Person person : personList) {            if (resMap.get(person.getFirstName()) == null) {                res = new PersonResponse();                res.setFirstName(person.getFirstName());                resMap.put(person.getFirstName(), "");                resList.add(res);            }            res.getList().add(person);        }        System.out.println("查询结果为:" + resList);    }}

输出结果为:

查询结果为:[PersonResponse [firstName=李, list=[Person [firstName=李, lastName=明, age=20], Person [firstName=李, lastName=白, age=29]]], PersonResponse [firstName=小, list=[Person [firstName=小, lastName=明, age=22], Person [firstName=小, lastName=帅, age=30]]]]

原创粉丝点击