File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Quickest way to convert a map to a list

 
Luke Murphy
Ranch Hand
Posts: 300
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone know a quickway to convert a map to a list that is quicker than this...

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and is that too slow for some reason?
 
Luke Murphy
Ranch Hand
Posts: 300
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:and is that too slow for some reason?


Well this is the performance forum :-)
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is indeed the performance forum. And the first rule of performance is "don't worry about it until it is a problem". or as Knuth put it, "Premature optimization is the root of all evil".
 
Luke Murphy
Ranch Hand
Posts: 300
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:it is indeed the performance forum. And the first rule of performance is "don't worry about it until it is a problem". or as Knuth put it, "Premature optimization is the root of all evil".


Yes it is a problem. I need to be able to convert a map to a list as fast as possible. I am wondering is this the fastest way.

Cheers.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"As fast as possible" is not really a spec - but if that is your requirement, my answer would be "buy a faster machine".
 
Luke Murphy
Ranch Hand
Posts: 300
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:"As fast as possible" is not really a spec - but if that is your requirement, my answer would be "buy a faster machine".

I have a use case which takes one hour approx, I have JProbed it and noticed a huge bottleneck on list.contains() calls. I have sped it up by replacing some of these with maps.

But in some cases I need to convert these maps back to lists.

There are several ways to convert maps to lists.

For example:



Which is abot 50% slower than the first way I pointed out.

I was wondering if someone knows of a faster way than I pointed out.

Pretty simple question.





 
Paul Clapham
Sheriff
Pie
Posts: 20164
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you identified the contains() methods as a performance bottleneck, and did something to deal with it. That's a good thing. But as far as I know you haven't identified converting maps to lists as the new performance bottleneck; at least, you haven't said that. And you have a reasonable-looking way to do it, too. So I'm with Fred, I don't see a reason to be looking at that for performance improvements. Go with the reasonable-looking way and see if it's worth improving.
 
Luke Murphy
Ranch Hand
Posts: 300
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:So you identified the contains() methods as a performance bottleneck, and did something to deal with it. That's a good thing. But as far as I know you haven't identified converting maps to lists as the new performance bottleneck; at least, you haven't said that. And you have a reasonable-looking way to do it, too. So I'm with Fred, I don't see a reason to be looking at that for performance improvements. Go with the reasonable-looking way and see if it's worth improving.


It is a bottleneck. But it is not as bad as having everything as lists.

Look no worries if no-one can answer the question. It probably is the fastest way to do it.


 
Paul Clapham
Sheriff
Pie
Posts: 20164
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would assume that something from the standard API would normally be at least as something that I threw together, if only because it has direct access to internal structures which I don't. But that's just an assumption, of course.

It's also possible there's something as simple as the standard API preallocating the full array for the ArrayList instead of having to recopy it several times as the list grows. (You must be creating a large list since you say this is a bottleneck.)

It might also make a difference if you created some other kind of list, say a LinkedList. Or there might be a difference depending on the kind of Map you're using.

Or you could design some new data structure which had the features of a Map and of a List at the same time, so you didn't have to do the copying at all.

Lots of possibilities, then. Just because it's a simple question doesn't mean it has a simple answer.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic