Java - how to flatten a set of sets, and should I do this first?

Let's say I have a chart implemented with two maps (in and out) that display a map (source, set (edge)) and (target, set (edge)) respectively. Until now, I also had an allEdges set, which I decided to get rid of. the returned set of edges is now more complicated, since I have to smooth out the values ​​of one of the maps. what's the best (fastest) way to do this? or should I just leave set allEdges (I don't really care about memory, just thought it was a bit redundant).

thanks

+4
source share
2 answers

Time and space are the quintessence. You can save your set of edges explicitly, sacrificing the amount of space it takes to have it without calculating it, or you can return this space and calculate the set of all edges when you need it.

From your post, it seems that the first is the best solution. Don't worry about the β€œright path” - worry about what makes sense for your application.

+5
source

Please note that if you use SetMultimap from google-collections , you can trivially view the collection of all edges using multimap.values() . In your case, if you understand correctly, there will be no duplicates in this collection, but, unfortunately, it does not actually implement Set . To get the set, use ImmutableSet.copyOf(multimap.values()) .

And yes, if you need this as a set often, it makes sense to keep the backup Set around and keep updating it when you go.

Or you can scratch it all and use a full-featured graphics library such as JUNG .

0
source

Source: https://habr.com/ru/post/1300647/


All Articles