Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Efficient way of doing this

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a simple look up code here to that has to return a location based on day of the month represented in the 3 string arrays. It works but i think it can be made more simple and efficient. Can you guys think of some thing efficient and concise instead of using String arrays that map to dates. Thanks in advance
 
Marshal
Posts: 68115
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't Days2Locations, please.

Why are you using Strings? Why have some got leading 0s and some not, and why have you got [] around the 7?
What are you trying to do? You havent's explained that.
 
seshu Palamanti
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell for your reply you were right i had those as for another test i cleaned it up. Do you see any improvement that can be done ?
 
Marshal
Posts: 25215
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just a Map which maps "2" to "LocationOne" and so on?
 
seshu Palamanti
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds possible but the map will have duplicate locations as values in a map thinking much more efficient.
 
Paul Clapham
Marshal
Posts: 25215
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

seshu Palamanti wrote:the map will have duplicate locations as values in a map thinking much more efficient.



I don't see why that's a problem. It's the keys which are supposed to be unique.
 
seshu Palamanti
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was thinking of more in these lines using Java 8 Streams

 
Campbell Ritchie
Marshal
Posts: 68115
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is something rare: a case where Stream code is less legible than using a Map would be. Remember that the Map only requires the hash code be used, whereas your Streams solution will have to iterate the three arrays every time you want a location.That sort of code belongs in the constructor or an initialiser. You may want to make the map static if you are using the same values for every instance. I am still not convinced you should use Strings as keys, not if you can use Integers instead. Don't start ints with 0, however.

If there is some mathematical relationship between the day numbers and the locations, you might find a different technique works better.Or something like that.
 
seshu Palamanti
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply and comments. I will defiantly consider that.
 
Campbell Ritchie
Marshal
Posts: 68115
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

seshu Palamanti wrote:Thanks for . . . .

That's a pleasure
 
Paul Clapham
Marshal
Posts: 25215
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my Map solution:



You did use the word "efficient" in your post title and you can't get more efficient than line 45 in my code.

However there's a couple of things to note my solution:

(1) It would be very easy to mis-type the Map entries when using copy-and-paste to create them. Copy and paste and fail-to-edit is a very common problem.
(2) My code works in Java 9 or later, if you're on Java 8 you would have to write a static initializer to build the Map. Like Campbell said in his earlier comments.

 
Campbell Ritchie
Marshal
Posts: 68115
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are those pairs correct, Paul? If so, what about this? There does seem to ba a mathematical relationship between day and location, as I suspected earlier.orThe second code block only has lines 1, 2, and 11 changed. It has the advantage that we now have a final reference to an immutable List. It can't cope with any of those location references pointing to null. The get() method on a small List is probably faster than hashing in a Map, and plain simple low level array dereferences might be faster still. But whatever we do, this is a small collection and all sorts of access will run fast.
 
Paul Clapham
Marshal
Posts: 25215
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Are those pairs correct, Paul?



Like I said, there's plenty of scope for bad typing there. One should have them checked by an editor to make sure they are correct.

And yeah, there's scope for other solutions. But in real life we're going to find that the rules change every month and we need a less brittle solution. It's nice to have something "efficient" but if it stops working on the 28th of the month (two days from now) that's not very helpful.
 
Campbell Ritchie
Marshal
Posts: 68115
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . the rules change every month . . . .

Thank you. If the rules do change, then how can you program a solution at all? Woiuld you want some sort of properties bundle or properties file which you change every month?
 
Paul Clapham
Marshal
Posts: 25215
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I would just have a table in a database or something where people could attach a location to a future day in the calendar.
 
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're really bent on using streams somehow and you're afraid you might make a typing mistake (or if you're just lazy like me):
 
Rancher
Posts: 3439
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if we want to use streams an minimize the chance of typing mistakes even more:
 
Mike Simmons
Rancher
Posts: 3439
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But at that point, why bother with the stream and map anyway?
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!