The concept of generics:
Simply speaking, the same method (class) can accept different data types and run to get corresponding results without security problems
In the previous article, there was a code like this:
No collection type, iterator type defined
package demo; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class CollectionDemo { public static void main(String[] args) { //Collection can store any type of object //In a collection, you can store data without specifying the type of data to store Collection c1 = new ArrayList(); c1.add("abc"); c1.add("def"); Iterator it1 = c1.iterator(); while(it1.hasNext()){ //it.next Obtain Object Type, cast String s1 = (String)it1.next(); System.out.println(s1.length()); } } }
There is a hidden danger here: if add(1); is automatically boxed to Integer type, it cannot be converted to String, and the type conversion exception occurs
The concept of generics is proposed in java to solve this problem:
package demo; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class GenericDemo { public static void main(String[] args) { function(); } public static void function(){ Collection<String> c1 = new ArrayList<String>(); c1.add("abc"); c1.add("def"); Iterator<String> it1 = c1.iterator(); while(it1.hasNext()){ String s1 = it1.next(); System.out.println(s1); } } }
At this time, if add(1);, it will fail during compilation, which solves the security problem
In fact, the generics here are pseudo generics. It's just a compilation method. If it's not a String type, the compilation can't pass. Otherwise, the compilation succeeds
There is no generics in the compiled class file, but the final run is safe because the type problem is handled during compilation
Generic approach (i.e. understanding, no practical application value):
package demo; import java.util.ArrayList; public class GenericDemo { public static void main(String[] args) { ArrayList<Integer> array = new ArrayList<Integer>(); // ArrayList Method in set toArray generic method array.add(123); array.add(456); Integer[] i = new Integer[(array.size())]; Integer[] j = array.toArray(i); for (Integer k : j) { System.out.println(k); } } }
There are also generic interfaces, generic classes, etc., all of which are designed to solve security problems and facilitate users. They also bring enhanced for loops
Wildcards for generics? :
package demo; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; public class GenericDemo { public static void main(String[] args) { ArrayList<String> array = new ArrayList<String>(); array.add("abc"); array.add("def"); HashSet<Integer> set = new HashSet<Integer>(); set.add(123); set.add(456); iterator(array); iterator(set); } //It is required to define a method that can iterate two sets at the same time public static void iterator(Collection<?> c){ Iterator<?> it1 = c.iterator(); while(it1.hasNext()){ System.out.println(it1.next()); } } }