I was using Multiset to have easy access to the freq of elements, but I realize there is Collections#frequency(Collection<?>, Object) that does the same for any collection. What is the point of using Multiset then? Is performance an issue here?
Using Guava multisets or Collections.frequency()?
1.3k Views Asked by seinecle At
1
There are 1 best solutions below
Related Questions in JAVA
- Add image to JCheckBoxMenuItem
- How to access invisible Unordered List element with Selenium WebDriver using Java
- Inheritance in Java, apparent type vs actual type
- Java catch the ball Game
- Access objects variable & method by name
- GridBagLayout is displaying JTextField and JTextArea as short, vertical lines
- Perform a task each interval
- Compound classes stored in an array are not accessible in selenium java
- How to avoid concurrent access to a resource?
- Why does processing goes slower on implementing try catch block in java?
- Redirect inside java interceptor
- Push toolbar content below statusbar
- Animation in Java on top of JPanel
- JPA - How to query with a LIKE operator in combination with an AttributeConverter
- Java Assign a Value to an array cell
Related Questions in COLLECTIONS
- how to keep 10 biggest integer while reading a list in java?
- Collecting inner List from outer List using Java 8
- Passing a lambda expression as parameter to a method?
- Performance of element-compare in java collections
- Where can I find the definitions of System.Collections.Concurrent classes in the mono source code?
- Any null safe alternative to ArrayList.addAll?
- Using Python how to get number of occurance of a string in 'n' number of columns from a .csv file
- Couchbase Bulk loading error with upsert() (.NET SDK 2.0)
- How to create a Dynamic IEnumerable
- How does Collections.binarySearch work?
- What collection is best to implement this program?
- Big form collections with Symfony2
- Update multiple values in an oracle table using values from an APEX collection
- Rails combine member & collection in routes.rb
- "Class.Property" is not accessible in this context because it is 'Private'
Related Questions in GUAVA
- Gradle: Override transitive dependency by version classifier
- How to wrap a method that returns an optional <T> with fromNullable?
- Good way to convert Optional<Integer> to Optional<Long>
- ORMLite and custom data persiter of Optional<Double>
- searching for keys when values are selected
- Best data structure in Java when using HashSet as a cache
- How to index a multimap based on several criteria?
- Joining a collection based on members of the type
- How doesn't add null value to Guava's LoadingCache?
- How to build a ConcurrentLinkedHashmap using Guava?
- Guava splitter for splitting on space,special character, digit
- Guava 18.0 refreshAfterWrite
- How long does an event live in the eventbus?
- Enable json serialization of Multimap in Spring Boot Project
- How is Guava Splitter.onPattern(..).split() different from String.split(..)?
Related Questions in MULTISET
- Data structure to efficiently merge up to n elements of multiset
- How to properly instantiate a MultiSet (created on my own) using Python
- Seg Error while erasing from multiset C++
- Pass a comparison function for Key Type without using decltype[c++]
- Move elements from std::multiset
- C++ Multiset count()
- Multiset of pair, find
- Oracle Cast and MULTISET avaliable in POSTGRES
- In multiset , the code does not enter into compare function and throws error
- unexpected output from C++ multiset lower_bound
- C++ : Running time of next() and prev() in a multiset iterator?
- Multinomial sets
- Initializing multiset with custom comparison function in C++
- How can I access and erase every 2nd last element from a multiset?
- Get first N elements in a C++ multiset
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Guava documentation for Multiset#count() has to say:
So, yes, I suspect that performance is the issue here.
I think
Multiset#countis more efficient becauseCollections#frequencyiterates through the entire collection. For an object o whose frequency you're checking, it goes through all elements e in the collection and checks(o == null ? e == null : o.equals(e)).For Multiset (which is an interface), the exact implementation of
countdepends on the class. If it is aHashMultiset, for example, then it is backed by aHashMap. For details about how that is more efficient than iterating through the whole collection, take a look at this answer: How does a Java HashMap handle different objects with the same hash code?.The Guava code is as follows
Similarly, for a
TreeMultiset, which maintains the ordering of its elements and is backed by an AVL tree,countcan be obtained in O(log(n)) steps instead of O(n), where n is the size of the collection. The Guava code is as follows: