In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
How to implement a disorder compatible with Set and a repeatable data structure of List? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Many common data structures are provided in Java. For example, Map, List, Set, etc. However, in many cases, we may need to count the number of occurrences of a certain data, and our implementation code may look like this:
If there are multiple methods in multiple projects, or in one project, multiple functions require this statistical capability. So can we extract this implementation into a new data structure that we can use anywhere? This is what this article is about, an unordered, List-compatible, repeatable data structure compatible with Set: Multiset.
It is possible that you will also be asked this question in the interview. Because the data structure seems to be the most boring, but it has the most long-term vitality.
The Guava class library provides a Set-compatible unordered, List repeatable data structure Multiset. The underlying layer of Multiset is still the data structure, which is equivalent to encapsulating the code above us. At the same time, it is an extension of the data structure in Java.
When using Multiset, our code becomes very simple. We only need to store it, and the final number of occurrences and repetitions can be obtained only through the count method, which is very convenient.
At the same time, Multiset also provides a setCount (E, int) method, which can modify the number of element E in Multiset, but cannot change the number of element occurrence to a negative number and a value greater than Integer.MAX_VALUE, otherwise an exception will be thrown.
Several features of Multiset can be summarized as follows:
The number of occurrences of elements in Multiset can only be positive, and that's why.
If the number of occurrences of E is 0, then E will not appear in multiset, but not in the views of elementSet () and entrySet ().
Multiset.size () returns the size of this collection, which is equal to the total number of elements that appear in multiset.
If you want to get the total number of different elements in the multiset, you can use elementSet (). Size () to achieve this.
Multiset.iterator () can traverse all the elements in Multiset, so the number of iteratior iterations is equal to multiset.size ().
Multiset supports adding and deleting elements, and sets the number of times elements appear.
SetCount (elem, 0) is equivalent to removing all elements of elem.
If the elem in the multiset.count (elem) method does not appear in the Multiset, then its return value is always 0.
This is the answer to the question about how to implement a disorder compatible with Set and the repeatable data structure of List. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.