Enumset is a generic container for Java enumeration types, and since Java has SortedSet, TreeSet, hashset and other containers, why do you want to have one more enumset<t>? The answer must be enumset has certain characteristics, for example, the Enumset is very fast. Other features are not listed, after all, the content of this article is not to introduce the characteristics of enumset.
First, the fact that there is such a enumset, it has 50 enumeration value t0~t49, 50 values are inserted into the container (HashSet, Enumset), for an operation, the 50 enumerated values are moved out as the second operation. The total time of the first and second operation is set to a period, take hashset operation of a cycle and a cycle of enumset do not make much sense, so we use 50 cycles and as a comparison, HashSet consumes 9ms, Enumset consumes 4ms (this result only shows that the same operation Enumset faster than HashSet, not as a reference, because this time is not the thread exclusive time). Here's the code and the results:
Public classenumsettest{Private Staticenumtest[] Enumtestarr =enumtest.values (); Public Static voidMain (string[] args) {Set Set=NewHashset<enumtest>(); inti = 0; SimpleDateFormat DF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss. SSS "); System.out.println ("HashSet ... Begin "+ Df.format (NewDate ())); while(I <= 1000) {addenumerate (set); Removeenumerate (set); I++; } System.out.println ("HashSet ... End "+ Df.format (NewDate ())); Enumset<EnumTest> Enumset = enumset.noneof (enumtest.class); I= 0; System.out.println ("Enumset ... Begin "+ Df.format (NewDate ())); while(I <= 1000) {addenumerate (enumset); Removeenumerate (Enumset); I++; } System.out.println ("Enumset ... End "+ Df.format (NewDate ())); } /** HashSet ... Begin 2015-01-03 21:11:51.579 * HashSet ... End 2015-01-03 21:11:51.588 * Enumset ... Begin 2015-01-03 21:11:51.589 * Enumset ... End 2015-01-03 21:11:51.593 **/ Private Static voidaddenumerate (Set set) { for(Enumtest T:enumtestarr) {set.add (t); } } Private Static voidremoveenumerate (Set set) { for(Enumtest T:enumtestarr) {set.remove (t); } }}
Then why Enumset faster, Enumset is an abstract method, this test is to use the Enumset implementation Regularenumset,regularenumset Add method of the source code is as follows:
Public Boolean Add (e e) { typecheck (e); long oldelements = elements; | = (1L << ( (Enum) e). ordinal ()); return Elements! = oldelements ;
Source Code of Regularenumset
From the source of the Add method, you can see that the Add method actually does an operation on the long integer data element, which means that Enumset actually saves the enumeration value on a long integer data. None of the enumeration values occupy a bit. The primary action to be added each time is 1, type Check 2, add enumeration value 3, determine if the enumeration value has been added.
Now simulate a scene to illustrate how Enumset works. Create a new Enumset (SET1) and add Enumtest.t3 (Ordinal:3) to Set1 with the following code
enumset<enumtest> Set1 = enumset.noneof (enumtest. Class); Set1.add (ENUMTEST.T3);
The element has 64 bits, which is briefly drawn here. Here's a question, what if the number of enumerated values exceeds 64? More than 64 are using Enumset's another implementation of the Jumboenumset.
This is the end of this article, as to how Jumboenumset works and how Regularenumset other methods work, please explore it yourself. All source code can be found in http://grepcode.com/.
A preliminary glimpse of how Java Enumset works