The problem of a java set

there is a location class Place, whose constructor is Place (String name, Position p, String description), and the constructor of coordinate Position class is Position (int x, int y).
now there is a collection of Place objects HashSet < Place > places=new HashSet < > ();. Because a name name may correspond to multiple different Place objects, in order to facilitate the query by place name, we want to arrange the places with the same name, that is, to organize the location objects into a map: Map < String, Set < Place > > map=new HashMap < > (). How to get map? from the existing collection places? Where the key of map is a string, which is the name of the place, and value is the Set collection of Place with the corresponding name. Thank you in advance!

Mar.07,2021

under what circumstances should I do this? Is it the data extracted from the database? The data is very large? Not enough memory? If you want to put the whole Set in Map, just loop upstairs. I assume that your data is so large that you don't want to waste memory, all the data is cache in memory, and you rarely use to fetch locations according to name. Use List, instead of Set, and then click name Collections.sort to use Collections.binarySearch if List, is fetched according to name.


do iterator traversal on set, you can get each place object, and then map.put (place.getName, place),) is fine.


this is a grouping. If you have learned Java8, it is easy to use stream to deal with thieves, set.stream (). Collect (Collectors.groupingBy (new Function () {}));
if you don't know Java8, the following article can help you. https://www.oschina.net/quest.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb1a03-2372.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb1a03-2372.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?