HashSet and TreeSet sets
-
Set Single Column Set Interface Features:
1. Disorder: The order of storage and retrieval is not necessarily the same.
2 Can't store duplicate elements (focus on why duplicate elements can't be stored)
3 There is no index, fori traversal cannot be used, only enhanced for loop traversal can be used.The effect of hash value: By default, it is used to represent the address of an object in memory. Generally, different objects have different hash values, so we often say that the address value is different. Unless the hashCode() method hash value is rewritten, it is possible that one.
-
HashSet collection (emphasis)
Features; the underlying use of hash table structure (arrays store linked lists).
Unique conditions for elements in Set sets: (important)
CONCLUSION: In order to guarantee the uniqueness of elements in Set sets, the hashCode() method and equals() method must be rewritten. -
LinkedHasSet: Ordered and not able to store duplicate elements, exceptions
Characteristics: The underlying structure is linked list + hash table; linked list guarantees orderliness. -
TreeSet collection
Sorting Conditions: Conditions Two Choose One
1. Elements in TreeSet set implement the interface of Comparable natural sorting and define sorting rules.
2. Elements do not need to implement the Comparable interface, but pass a Comparator comparator object when creating TreeSet collection objects
What's the difference between Comparable and Comparator?
Comparable naturally ordered interfaces require element objects to implement this interface.
Comparator comparator interface for TreeSet objects. -
Summary: Set sets
Common characteristics:
Unordered, unable to store duplicates, no index
HashSet: (emphasis)
The bottom layer is the hash table structure
The only condition for an element is that the element overrides the hashCode() method and equals() method.
LinkedHashSet:
The bottom layer is linked list + hash table structure
SPECIAL: ORDER OF ELEMENTS: THE ORDER OF STORAGE AND ACQUISITION IS UNIFORM
The only condition for an element is that the element overrides the hashCode() method and equals() method.
TreeSet: (Mastery)
Special: default natural ordering of elements
Sorting Conditions: Conditions Two Choose One
1. Elements in TreeSet set implement the interface of Comparable natural sorting and define sorting rules.
2. Elements do not need to implement the Comparable interface, but pass a Comparator comparator object when creating TreeSet collection objects
Define comparison rules:
Ascending order: comparing oneself with others is ascending order, and the parameters passed in are others
Descending order: comparing others with themselves is descending order
generic paradigm
Concept: Parameterize fixed data types, also known as parameterized types.
Benefits:
1. Advance the runtime exception to the compiler to ensure the correctness of the stored data.
2. Avoid mandatory type conversion.
- Classification of generics:
Generic classes:
Step 1: Define generics after the class name, so generics can be used anywhere in the class.
public class GenericClass<T> { //Data can only hold data of String type //private String data; /* Requirement: Require data to hold any type of data */ private T data; public T getData() { return data; } public void setData(T data) { this.data = data; } }
Step 2: Determine generics when creating objects
public static void main(String[] args) { //Create GenericClass objects and determine generics GenericClass<String> gc1 = new GenericClass<>(); gc1.setData("java"); System.out.println(gc1.getData()); //Storage Student Type GenericClass<Student> gc2 = new GenericClass<>(); gc2.setData(new Student("Leather sofa", 22)); System.out.println(gc2.getData()); }
- Generic interfaces:
Step 1: Define generics after the interface name, which can be used anywhere in the interface
public interface GenericInterface<T>{ //Using generics in abstract methods public T method(T t); }
Step 2: Define implementation classes for interfaces, just like generic classes
public class GenericInterfaceImpl<T> implements GenericInterface<T>{ public T method(T t){ } }
Step 3: Determine generics when creating implementation class objects
GenericInterface<String> gif=new GenericInterfaceImpl<String>();
- Generic methods:
Step 1: Define generics after the modifier of the method, and use generics anywhere in the method (return value, parameter list, method body).
public class GenericMethod { //Define a generic method public static <E> E show(E e){ //Determine whether e is a String type if(e instanceof String){ String s= (String) e; s="hello "+s; return (E) s; } return e; } }
Step 2: Determine the generic type by passing parameter values when calling a method
public static void main(String[] args) { //create object GenericMethod gm=new GenericMethod(); //Call show method String s = gm.show("java"); System.out.println(s); Student stu = gm.show(new Student("zhangsan", 20)); System.out.println(stu); }
- Wildcard generics: <?> denotes the parent class of generics and is generally used in the list of method parameters
public class GenericTest { public static void main(String[] args) { //Creating Collection Objects List<Number> list1=new ArrayList<Number>(); //Call method method method method(list1); //Creating Collection Objects /*List<Integer> list2=new ArrayList<Integer>(); //Call method method method method(list2);*/ //Creating Collection Objects List<Object> list3=new ArrayList<Object>(); //Call method method method method(list3); } /* Number Integer is a child-parent relationship, but a list set with Number and a list set with Integer are not child-parent relationships. List<? extends Number>:Define wildcards? Upper bound, the maximum type can only be Number type, so pass List < Object > error reporting List<? super Number>: Define wildcards? The lower limit, the smallest type can only be Number type, so pass List < Integer > error reporting */ /*public static void method(List<? extends Number> list){ System.out.println(list); }*/ public static void method(List<? super Number> list){ System.out.println(list); } }