Home >Java >javaTutorial >How to use HashMap to access key-value mapping relationship in java

How to use HashMap to access key-value mapping relationship in java

WBOY
WBOYforward
2023-05-10 11:02:141271browse

Use HashMap to access key-value mapping relationships

Simply put, HashMap is composed of an array and a linked list. The array is the main body of HashMap, and the linked list mainly exists to resolve hash conflicts. If the located array position does not contain a linked list, then search, add and other operations are very fast and only require one addressing. The time complexity is O(1); if the located array contains a linked list, for the adding operation, its time complexity is O(1). The time complexity is O(n) - first traverse the linked list, overwrite it if it exists, add it if it does not exist; for the search operation, you still need to traverse the linked list, and then compare and search one by one through the equals method of the key object. From a performance perspective, the fewer linked lists in HashMap, that is, the fewer hash conflicts, the better the performance. Therefore, in daily coding, you can use HashMap to access key-value mapping relationships.

Case: Given a list of menu records, each menu record contains the parent menu identifier (the parent menu identifier of the root menu is null), and the entire menu tree is constructed.

/** 菜单DO类 */@Setter@Getter@ToStringpublic static class MenuDO {    /** 菜单标识 */
    private Long id;    /** 菜单父标识 */
    private Long parentId;    /** 菜单名称 */
    private String name;    /** 菜单链接 */
    private String url;
}/** 菜单VO类 */@Setter@Getter@ToStringpublic static class MenuVO {    /** 菜单标识 */
    private Long id;    /** 菜单名称 */
    private String name;    /** 菜单链接 */
    private String url;    /** 子菜单列表 */
    private List<MenuVO> childList;
}/** 构建菜单树函数 */public static List<MenuVO> buildMenuTree(List<MenuDO> menuList) {    // 检查列表为空
    if (CollectionUtils.isEmpty(menuList)) {        return Collections.emptyList();
    }    // 依次处理菜单
    int menuSize = menuList.size();
    List<MenuVO> rootList = new ArrayList<>(menuSize);
    Map<Long, MenuVO> menuMap = new HashMap<>(menuSize);    for (MenuDO menuDO : menuList) {        // 赋值菜单对象
        Long menuId = menuDO.getId();
        MenuVO menu = menuMap.get(menuId);        if (Objects.isNull(menu)) {
            menu = new MenuVO();
            menu.setChildList(new ArrayList<>());
            menuMap.put(menuId, menu);
        }
        menu.setId(menuDO.getId());
        menu.setName(menuDO.getName());
        menu.setUrl(menuDO.getUrl());        // 根据父标识处理
        Long parentId = menuDO.getParentId();        if (Objects.nonNull(parentId)) {            // 构建父菜单对象
            MenuVO parentMenu = menuMap.get(parentId);            if (Objects.isNull(parentMenu)) {
                parentMenu = new MenuVO();
                parentMenu.setId(parentId);
                parentMenu.setChildList(new ArrayList<>());
                menuMap.put(parentId, parentMenu);
            }            
            // 添加子菜单对象
            parentMenu.getChildList().add(menu);
        } else {            // 添加根菜单对象
            rootList.add(menu);
        }
    }    // 返回根菜单列表
    return rootList;
}

The above is the detailed content of How to use HashMap to access key-value mapping relationship in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete