Get the App
SLTechnology News&Howtos  ›  Development  › 

How java uses HashMap to access key-value Mapping

Shulou Source: shulou.com Published: 2022-06-01 06:16:24 10月10日 Update

This article will explain in detail how java uses HashMap to access key-value mapping. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Access key-value mapping using HashMap

To put it simply, HashMap consists of arrays and linked lists, which are the main body of HashMap, and linked lists exist mainly to resolve hash conflicts. If the location of the located array does not contain a linked list, then the operations such as searching and adding are very fast and only need to be addressed once, and its time complexity is O (1). If the located array contains a linked list, the time complexity of the add operation is O (n)-first traversing the linked list, covering it if it exists, or adding it if it does not exist. For the lookup operation, you still need to traverse the linked list and then compare the lookups one by one through the equals method of the key object. In terms of performance, 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.

Example: given a list of menu records, each menu record contains the parent menu identity (the parent menu of the root menu is identified as null) to build the entire menu tree.

/ * * menu DO class * / @ Setter@Getter@ToStringpublic static class MenuDO {/ * * menu ID * / private Long id; / * * menu parent ID * / private Long parentId; / * * menu name * / private String name; / * * menu link * / private String url } / * * menu VO class * / @ Setter@Getter@ToStringpublic static class MenuVO {/ * * menu ID * / private Long id; / * * menu name * / private String name; / * * menu link * / private String url; / * * submenu list * / private List childList } / * * build menu tree function * / public static List buildMenuTree (List menuList) {/ / check list is empty if (CollectionUtils.isEmpty (menuList)) {return Collections.emptyList ();} / / process menu int menuSize = menuList.size (); List rootList = new ArrayList (menuSize); Map menuMap = new HashMap (menuSize) For (MenuDO menuDO: menuList) {/ / assignment menu object 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 ()); / / process Long parentId = menuDO.getParentId () based on parent identity; if (Objects.nonNull (parentId)) {/ / build parent menu object MenuVO parentMenu = menuMap.get (parentId) If (Objects.isNull (parentMenu)) {parentMenu = new MenuVO (); parentMenu.setId (parentId); parentMenu.setChildList (new ArrayList ()); menuMap.put (parentId, parentMenu);} / / add submenu object parentMenu.getChildList () .add (menu) } else {/ / add the root menu object rootList.add (menu);}} / / return the root menu list return rootList } this is the end of the article on "how java uses HashMap to access key-value mapping relationships". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Menus identities objects access arrays articles complexity name complexity performance time more links hashes conflicts processing positioning good practical subject Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Microsoft NVidia Huawei MySQL