Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider using Guavas Mapmaker class #3

Closed
Dexteroo7 opened this issue Jul 25, 2015 · 8 comments
Closed

Consider using Guavas Mapmaker class #3

Dexteroo7 opened this issue Jul 25, 2015 · 8 comments

Comments

@Dexteroo7
Copy link

Instead of synchronizing over the whole map, which is quite unnecessary in most cases use a ConcurrentMap instead. Guava has a great mapMaker utility.
eg. new MapMaker().weakKeys().makeMap()
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/MapMaker.html

@Swati4star
Copy link
Member

Using this in place of google maps?
how is it better than that?

@Dexteroo7
Copy link
Author

I meant instead of this :
https://github.com/Swati4star/NSIT-App-v2/blob/master/app/src/main/java/functions/ImageLoader.java
Collections.synchronizedMap(new WeakHashMap<ImageView, String>());

@piyushghai
Copy link

Avoid using Third party library for a simple map representation. Java has sufficient variants in the Collections Interface.

@piyushghai
Copy link

And as for the synchronized part, that should only be needed if the same map will be accessed by more than one thread concurrently.
If that is the case use a lighter lock mechanism like : http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantLock.html

@Dexteroo7
Copy link
Author

@piyushghai highly disagree. If one knows whats going on under the hood, using an external library is better. I could have suggested use of http://square.github.io/picasso/, but I understand the goal of the project is to learn by experimentation. But at the same time I feel https://github.com/google/guava is a pretty standard library one should be aware of and use.

P.S +1 for suggesting use of ReentrantLock lock, but it is lighter compared to what ? Or did you mean a custom synchronization scheme could be made, 'which' is lighter (eg. lock on writes, don't lock on reads etc.) Collections.synchronizedMap, locks the 'whole' map which is quite wasteful.

@piyushghai
Copy link

@Dexteroo7 If one knows what's going under the hood, then one can code it as well. Libraries are useful if they can abstract what's going under the hood and hence make life easier. Third party libraries should be used only if there is no native solution available. They also have their own memory footprint on the app.

Reentrant lock is a lightweight lock, which is faster than synchronizing every access to the map, which is what Collections.synchronizedMap returns. The use case depends upon what one's requirements are.

@Dexteroo7
Copy link
Author

this https://code.google.com/p/guava-libraries/wiki/PhilosophyExplained
and this, http://developer.android.com/tools/help/proguard.html

Lets agree to disagree over the use of Guava. I use it amply in any Java related project I undertake :)

@piyushghai
Copy link

Everyone has a different view point :) . I prefer using what Java provides off the blocks. If it lacks something, one should try to implement it first before using a custom library. Makes for a good learning as well shows you the limitations of the language

naman1901 added a commit to naman1901/NSIT-App-v2 that referenced this issue Feb 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants