当前位置:网站首页>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);
}
}边栏推荐
- Excel splits a worksheet into multiple worksheets according to conditions, and how to split multiple worksheets into independent tables
- Characteristics of ES6 arrow function
- Small sample learning data set
- Go Context - Cancelation and Propagation
- Read the general components of antd source code
- February 19 CTF exercise
- A review of small sample learning
- SQL get current time
- Create dynamic array
- JSON Library Tutorial from scratch (II): parsing digital learning and sorting notes
猜你喜欢

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

Analysis of IM project framework
![The k-th node of the binary search tree [sword finger offer]](/img/9c/26b508100d8b49c114cf72346a8e7c.jpg)
The k-th node of the binary search tree [sword finger offer]

Read the general components of antd source code

Reading and writing of nodejs excel (.Xlsx) files

UVA816 Abbott’s Revenge

Professional things use professional people

Array and simple function encapsulation cases

Use serialize in egg to read and write split tables

2022.1.23 diary
随机推荐
Various pits encountered in the configuration of yolov3 on win10
The article is on the list. Welcome to learn
Five simple data types of JS
Electric store stores data
CopyPlugin Invalid Options options should be array ValidationError: CopyPlugin Invalid Options
Svg code snippet of loading animation
Drag modal box
Trouble of setting table property to null
Oracle SQL statement operand: rounding, rounding, differentiation and formatting
Code learning-cvpr2020 unsupervised domain adaptive semantic segmentation: intra advance
The k-th node of the binary search tree [sword finger offer]
Database low-end SQL query statement fragment
Using JS to realize the sidebar of life information network
Use js to simply implement the apply, call and bind methods
February 20ctf record
[day40 literature extensive reading] space and time in the child's mind: metallic or atomic
Go Concurrency
Excel splits a worksheet into multiple worksheets according to conditions, and how to split multiple worksheets into independent tables
JMeter stress testing and agent recording
2022.1.23 diary