当前位置:网站首页>Guava new collection type
Guava new collection type
2022-06-25 05:38:00 【Which floor do you rate moto】
Guava Introduced a lot of JDK There is no the , but New collection types that are obviously useful , These new types are for and JDK Set framework coexists , Instead of going to JDK Other concepts are hard plugged in the set abstraction
Multiset
Set up set and list Characteristics of : Elements can be repeated , Element disorder ( Having no element order restrictions ArrayList<E>)
/**
* @author nieyapeng
* @create 2022-02-06-20:20
*
* Multiset
* Set up set and list Characteristics of : Elements can be repeated , Element disorder ( Having no element order restrictions ArrayList<E>)
* add(E) Add a single given element
* iterator() Returns an iterator , contain Multiset All elements of ( Including elements of repetition )
* size() Returns the total number of all elements ( Including elements of repetition )
*
*
* When put Multiset regard as Map<E, Integer> when , It also provides query operations that meet performance expectations :
* count(Object) Returns the count of the given element .HashMultiset.count The complexity of is O(1),TreeMultiset.count The complexity of is O(log n).
* entrySet() return Set<Multiset.Entry<E>>, and Map Of entrySet similar .
* elementSet() Returns all the Set<E>, and Map Of keySet() similar .
* all Multiset The memory consumption of the implementation increases linearly with the number of non repeating elements .
*
*
* Multiset No Map
* Please note that ,Multiset<E> No Map<E, Integer>, although Map Maybe some Multiset Part of the implementation . To be precise Multiset It's a kind of Collection type , And fulfilled Collection Interface related contracts . About Multiset and Map The other notable differences are :
* Multiset The element count in can only be positive . The count of any element cannot be negative , Nor is 0.elementSet() and entrySet() There will be no such element in the view .
* multiset.size() Returns the size of the collection , Equal to the sum of all element counts . For the number of non repeating elements , You should use elementSet().size() Method .( therefore ,add(E) hold multiset.size() increase 1)
* multiset.iterator() Will iterate over the repeating elements , So the iteration length is equal to multiset.size().
* Multiset Support direct addition 、 Reduce or set the count of elements .setCount(elem, 0) Equivalent to removing all elem.
* Yes multiset Elements not in ,multiset.count(elem) Always return 0.
*
*
* Map Corresponding Multiset Do you support null Elements
* HashMap HashMultiset yes
* TreeMap TreeMultiset yes ( If comparator Words of support )
* LinkedHashMap LinkedHashMultiset yes
* ConcurrentHashMap ConcurrentHashMultiset no
* ImmutableMap ImmutableMultiset no
*/
public class MultiSetDemo {
public static void main(String[] args) {
HashMultiset<String> hashMultiset = HashMultiset.create();
//add(E) Add a single given element
hashMultiset.add("nieyp");
hashMultiset.add("nieyp");
hashMultiset.add("nieyp1");
hashMultiset.add("nieyp1");
hashMultiset.add("nieyp11");
//count(Object) Returns the count of the given element .HashMultiset.count The complexity of is O(1),TreeMultiset.count The complexity of is O(log n).
int count = hashMultiset.count("nieyp");
System.out.println("nieyp Number of occurrences "+count);
/*iterator() Returns an iterator , contain Multiset All elements of ( Including elements of repetition )
nieyp11
nieyp1
nieyp1
nieyp
nieyp
*/
Iterator<String> iterable = hashMultiset.iterator();
while (iterable.hasNext()){
String result = iterable.next();
System.out.println(result);
}
//size() Returns the total number of all elements ( Including elements of repetition )
int size = hashMultiset.size();
System.out.println(size);
//entrySet() return Set<Multiset.Entry<E>>, and Map Of entrySet similar .[nieyp11, nieyp1 x 2, nieyp x 2]
Set<Multiset.Entry<String>> entrySet = hashMultiset.entrySet();
for (Multiset.Entry<String> stringEntry : entrySet) {
System.out.println(stringEntry);
}
System.out.println(entrySet);
// elementSet() Returns all the Set<E>, and Map Of keySet() similar .[nieyp11, nieyp1, nieyp]
Set<String> set = hashMultiset.elementSet();
System.out.println(set);
}
}边栏推荐
- UVA816 Abbott’s Revenge
- A summary of the experiment of continue and break in C language
- Drag modal box
- Even if you are not good at anything, you are growing a little bit [to your 2021 summary]
- Word quickly makes multiple single-sided table labels, number plates, etc
- Use serialize in egg to read and write split tables
- Native JS high risk reminder pop-up code snippet, "are you sure you want to do this?" and "it cannot be recovered after deletion. Do you want to continue“
- How to choose the years of investment in financial products?
- [relax's law of life lying on the square] those poisonous chicken soup that seem to be too light and too heavy, but think carefully and fear
- Large number operation (capable of square root, power, permutation and combination, logarithm and trigonometric value)
猜你喜欢

JSON Library Tutorial from scratch (III): parsing strings, learning and sorting notes

DOM document object model (I)

2022.1.25

2022.1.21 diary

05 virtual machine stack

Unsupervised domain adaptation in semantic segmentation:a review unsupervised domain adaptation in semantic segmentation: a review

Get the first letter of Chinese phonetic alphabet in Excel and capitalize it

2022.1.23 diary

A review of small sample learning

渗透测试-提权专题
随机推荐
H5 canvas drawing circle drawing fillet [detailed explanation]
Example of dynamic programming 3 leetcode 55
Database query optimization method
How to add an external header file in vs?
CopyPlugin Invalid Options options should be array ValidationError: CopyPlugin Invalid Options
Five simple data types of JS
2021-04-02
[pan Wai 1] Huawei computer test
3.2.3 use tcpdump to observe TCP header information (supplement common knowledge of TCP protocol)
[OSPF routing calculation (class I LSA router, class II LSA network, and class III LSA sum net)] -20211228-30
Mirror image of binary tree
Word quickly makes multiple single-sided table labels, number plates, etc
Vue uses keep alive to cache page optimization projects
Unsupervised domain adaptation in semantic segmentation:a review unsupervised domain adaptation in semantic segmentation: a review
C language - minesweeping
Click to jump out and drag the pop-up window
A brief talk on media inquiry
Pytorch- daily learning notes of some small functions involving training
Word of the Day
Small sample learning data set