Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How java uses HashMap to access key-value Mapping

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report