Generics and Set Sets Instead of Java Initial Teaching
I. generics
Generics, or "parameterized types", identify the data types of the collection storage elements.
Writing: <Data Type (Generic)>
Generic advantages:
1. Ensure the security of the data (prompting you about the type of parameters passed in the method).
2. Avoid downward transition (type transition).
3. Convert runtime errors to compile-time errors.
generic interface
interface InterA<G>{
public abstract void fun(G g);
}
//Generic type determination can be determined on the implementation class of the interface
class InterAImpl implements InterA<String>{
@Override
public void fun(String g) {
// TODO Auto-generated method stub
}
}
? extends E
Subclasses inherit the parent class.
The parameter equivalent to the incoming must be a subclass or this class of the parent class (E).
You can limit the type of parameter (down).
ArrayList<Person> list1 = new ArrayList<>();
list1.add(new Person("wanghong", 18));
list1.add(new Person("dongdong", 20));
ArrayList<Student> list2 = new ArrayList<>();
list2.add(new Student("liuliu", 24));
list2.add(new Student("shengsheng", 21));
//addAll (Collection ? extends E)
//GENERALIZATION OF E list1
//The generic form of list2
list1.addAll(list2);
System.out.println(list1);
Array-to-set method asList
//Put an int array into a collection as an element
//The system does not carry out automatic packing.
int[] array1 = {1, 2, 3, 4, 5};
List<int[]> list = Arrays.asList(array1);
System.out.println(list);
Integer[] array2 = {1, 2, 3, 4, 5};
List<Integer> list2 = Arrays.asList(array2);
System.out.println(list2);
Unsupported OperationException exception
String[] strings = {"dongdong", "liuliu","shengsheng"};
List<String> list3 = Arrays.asList(strings);
//UnsupportedOperationException
//Operational exceptions are not supported
//Note: This method can't modify the length of a set after it has passed through it.
// list3.add("wanghong");
// System.out.println(list3);
//The significance of the asList method is that you can use methods in collection classes
boolean b = list3.contains("shengsheng");
System.out.println(b);
Multi-parameter method int... num
Int... num type can accept multiple int type values equivalent to an array of parameters
Calling mode
1. Input values are separated by commas
2. It's OK to pass in an array directly.
Note: Multiple parameters should be placed at the end of the method parameter list
private static void SimpleExample() {
//Calling multiparameter methods
print(1,2,3,4,5,6);
int[] array = {11, 33, 66};
print(array);
}
public static void print(int ... num) {
//ergodic
for (int i = 0; i < num.length; i++) {
System.out.println(num[i]);
}
}
Two ways to delete a collection
for loop deletion
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals("b")) {
list.remove(i--);
}
}
System.out.println(list);
Iterator deletion
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String str = iterator.next();
if (str.equals("b")) {
//delete
iterator.remove();
}
}
System.out.println(list);
swap, the exchange method provided by the collection tool class
ArrayList<Student> list = new ArrayList<>();
list.add(new Student("Wang Mou", 18));
list.add(new Student("Zhou Mou", 20));
list.add(new Student("Wu Mou", 21));
list.add(new Student("Zhao Mou", 16));
list.add(new Student("Li Mou", 14));
for (int i = 0; i < list.size() - 1; i++) {
for (int j = 0; j < list.size() - 1 - i; j++) {
//Take out the two students to compare.
Student student = list.get(j);
Student student2 = list.get(j+1);
//Comparative age
if (student.getAge() < student2.getAge()) {
//Exchange location set (index, obj)
// Student temp = student;
// list.set(j, student2);
// list.set(j+1, temp);
//Exchange methods provided by collection tool classes
Collections.swap(list, j+1, j);
}
}
}
System.out.println(list);
Set Set Set Set (Unordered, Unsubscripted, Unduplicated)
HashSet
Unordered collection, different order of storage and extraction, no index, no storage of duplicate elements.
Create Set Collections
//Orderliness refers to how the order of storage is stored and how to get it.
HashSet<String> set = new HashSet<>();
set.add("f");
set.add("f");
set.add("a");
set.add("a");
set.add("b");
set.add("b");
set.add("c");
set.add("c");
//Iterator traversal
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String string = iterator.next();
System.out.println(string
The strongest thing about HashSet is weight removal.
Hash value of object
If the parent class is not overridden, the result of each run is a different integer.
If the subclass overrides the method of the parent class, the hash value is customized;
hashCode() and equals methods need to be rewritten, usually generated by the system itself.
//System generation
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) //The same address (same object)
return true;
if (obj == null) //Judge whether the incoming is empty
return false;
if (getClass() != obj.getClass()) //Determine whether two objects come from a class
return false;
//Downward transition
Man other = (Man) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}