SET interface and its implementation in Java

xiaoxiao2021-03-06  54

Set interface and its implementation 1.SET interface set is an abstraction of mathematics, and the set does not contain a duplicate element. How to define whether it is a repetition element? Set up to one NULL element; for any non-NULL E1 and E2 E1.EQUALS (E2) == false. Object.hashcode () is satisfied: a. In the execution of the program, you must return to return from HashCode () whenever you repeat it on the same Java object. The same integer value is not as information to be modified like Object.equals (), but this integer value does not have to be consistent between multiple runs of the same application. B. If two objects are passed through Equals () The judgment is equal, then the two Object () will return to the same value .c. If the two objects are determined by equals (), then the two Object is not required. .hashcode () Returns different integer values. Collection operation: S1.Addall () Original S1.Retainal (S2) Original non-symmetric difference S1.Remove (S2) Symmetry SET SYMMETRICDIFF = New hashset (S1); SYMMERTRICDIFF. Add (S2); SET TMP = New HashSet (S1); Tmp.retainal (S2); SymmertricDiff.Removell (TMP); SymmeRtricDiff is the symmetrical difference of S1 and S2. Relative to SET RelativeCompl = New HashSet (S1) SET TMP = New hashset (S1); Tmp.retainal (S2); RelativeCompl.Removell (TMP); RELATIVECMOMPL, called S2 relative. 2.SET implementation hashset is using a hash table storage element, Sort, can be randomly accessed, the optimal performance of the SET .Treeset implements the sortedset interface, use a red black Tree to store elements, providing an orderly storage and access. 2. Hashset hashset Depending on the underlying implementation of HashMap. Hashset code segment: // Dummy Value to Associate With an Object in the backing map private static firm Object Present = New Object ();

Public hashset () {map = new hashmap ();

Public Boolean Add (E o) {Return map.put (o, present) == NULL;}

Public Boolean Remove (Object O) {Return Map.Remove (O) == Present;

Public Boolean Contains (Object O) {Return map.containskey (} From the code, the Hashset borrows HashMap at the bottom layer, using an Object type dummy value as a mapping value in the underlying HashMap store in the Hashset. It caught The key to the HashMap does not allow repeated features. For the ad (), the underlying map is called, and the elements you want to add will be placed in the underlying Map. If the underlying map returns NULL, the original collection There is no such button in the middle of the PUT () of the MAP interface, one is that the original map does not include the key; the other may be stored in the original MAP, but the The key is mapped to NULL. Only the return null in the hashset and the return null, that is, the original collection does not contain the element. This is because the storage of the bottom layer of the Hashset is one. Objects called Present Object types, it is impossible to be null. 2.2 TreeSet SET interface has a sub-interface: sORTEDSET, provides the order storage of the set elements, where the elements are ascended as sequential. In order to join an element to the sortedset implementation These elements are sorted, and the element type must be implemented as a COMARABLE interface, or use the Comparator when establishing SortedSet. Otherwise, the program will throw ClassCastException.3 when running. Precautions for use If the variable object is stored in the SET, repeated elements in these objects So fundamentally violates SET's conventions, and the behavior of SET has also become uncertain. So, when the variable object is stored in the SET, it must be very careful. Similarly, the key in the Map is also the same because of the MAP Key is also not allowed to repeat. //dupeleset.java class dupeleset {public static void main (string [] args) {Calendar CLD = new Calendar.GetInstance (); CLD.SET (2003, 0, 1); Date D1 = CLD.getTime ();

CLD.SET (2005, 10, 2); DATE D2 = CLD.getime ();

Set s = new hashset (); s.add (d1); s.Add (d2);

System.out.println ("Before Modify:" S);

D1.settime (D2.GetTime ());

System.out.println ("After Modified:" S);

}} The above program output: before modify: [Wed Jan 01 21:54:19 cst 2003, Thu Nov 02 21:54:19 cst 2000] after modifyied: [Wed Jan 01 21:54:19 cst 2000, Thu Nov 02 21:54:19 CST 2000]

转载请注明原文地址:https://www.9cbs.com/read-40710.html

New Post(0)