Object storage
① Array (basic data type & reference data type) ② Collection (reference data type)Copy the code
The disadvantage of storing data in arrays is that the length, once initialized, is immutable. There is no way to actually assign a value to an array element
Set framework
Collection interface: (two subinterfaces List and Set)
List interface: ① Store ordered, repeatable elements (equivalent to dynamic array) ② add method: Delete remove(int index) modify set(int index, Object obj) get(int index) insert add(int index, Object Object (obj) ③ The class of the elements added to the List must override equals() ⅰ. Vector(ancient implementation class, thread-safe, but less efficient than ArrayList)Copy the code
Set interface: ① Disorderliness: not randomness, refers to the location of the element stored in the bottom layer is unordered ② non-repeatable: When adding the same element to a Set, the following element cannot be added to the Set (equals() and hashCode() must be overwritten to ensure that elements in the Set are not repeatable). When an object is added to a Set, the hashCode() method of the object's class is called to calculate the hash value of the object, which is already stored in the object, and equals() is used to compare whether the two objects are the same. If they are, the latter object cannot be added (the hashCode() method must be consistent with equals()). 1. Set(main implementation class) ⅱ. LinkedHashSet: < HashSet > is a subclass of HashSet. When traversing a set of elements, we do so in the order in which they are appended. TreeSet Elements added to a TreeSet must be of the same class. Elements added to a TreeSet can be traversed in the order specified by elements added to the collection (e.g., String, When adding elements to TreeSet, compareTo() is first compared. Once 0 is returned, the program considers the two objects to be the same, even though they only have the same property. The latter object cannot be added (compareTo() is consistent with hashCode() and equals())). When adding objects of a custom class to a TreeSet, there are two sorting methods: (1) The natural ordering requires that user-defined classes implement the java.lang.Comparable interface and rewrite the abstract method of compareTo(Object OBj), in which the attribute of the user-defined class is sorted. If this interface is not implemented, a runtime exception will be reported. ② Custom sort creates an implementation object that implements the Comparator interface. Overriding the Comparator's compare(Object o1, The compare() method specifies that the elements are sorted by their class attribute. Pass the Object of the implementation class that implements the Comparator interface as a parameter to the constructor of TreeSet. If this interface is not implemented, a runtime exception is reported requiring the overridden compareTo() or compare() methods to be consistent with equals() and hashCode() methodsCopy the code
Map interface: Stores key-value pairs of data
HashMap: (main implementation class) key: (Set) value: (Collection) key: (Set) value: (Collection) A key-value pair is an Entry. All entries are Set and non-repeatable. When adding elements to a HashMap, the equals() method of the class where the keys are added is called to determine whether the two keys are the same. TreeMap: LickedHashMap: LickedHashMap: LickedHashMap: LickedHashMap: LickedHashMap: LickedHashMap: LickedHashMap: LickedHashMap: LickedHashMap: LickedHashMap: You need to sort by the specified property of the class in which the key is located. Null values (not recommended) ⅴ. Properties: used for handling properties files. Both key and value are stringsCopy the code
Iii. Basic use of Collections
Reverse sort List Collections: collections.reverse (List); Collections.shuffle(List); Collections.sort(List); Specify sort List Collections: collections.sort (List, Comparator); Collections.swap(List, int I, int j); Get the maximum number of List elements: collections.max (List); Collections.max(list, Comparator); Get the minimum value of the List element: collections.min (List); Collections.min(list, Comparator); Get the number of occurrences of an element in List: collections. frequency(List, Object); List newList = array.asList (new Object[list.size()]); Collections.copy(newList, list); ReplaceAll (List, oldValue, newValue);Copy the code
Iv. Synchronization control for Collections
Collection newCollection = Collections.synchronizedCollection(list);
List<Object> newList = Collections.synchronizedList(list);
Map<Object, Object> newMap = Collections.synchronizedMap(map);
Set<Object, Object> newSet = Collections.synchronizedSet(set)
Copy the code
Five Enumeration.
The Enumeration interface is an “older version” of the Iterator.
Enumeration enumeration = new StringTokenizer("12-ab-#r-8h", "-");
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
}
Copy the code