The rest will not be said. Everything is in the code.
Package com. gc. generic;/*** usage of collection objects before JDK5: * 1. Add any type objects to the Collection * 2. data types are lost when an object is retrieved from the collection, use the type-related method, * security risks ********************************* generic in JDK 5: allow programmers to use generic technology to limit the processing type of a set * List
List = new ArrayList
(); * In this way, the specific data type elements are obtained when the elements in the collection are retrieved (no type force is required). ** Note: generics are provided to the javac compiler, it is used to limit the input type of the set, so that the compiler * blocks the insertion of illegal data into the set at the source code level. However, after the compiler compiles a java * program with generics, the generated class file will no longer contain generics, so that the program running efficiency will not be affected *, this process is called "erasure". ** the generic technology is only a technology in the compiler stage. It serves as a security check for javac commands and is generated. after the class file, the generic information will be erased ** List
---- Parameterized type ** generic technical objects List and set. Map type security constraints ** when using generic objects for type conversion, the objects at both ends of the equal sign must use the same generic type */import java. util. arrayList; import java. util. hashMap; import java. util. iterator; import java. util. using list; import java. util. list; import java. util. map; import java. util. set; import java. util. treeSet; import java. util. map. entry; import org. junit. test;/***** @ author Android General **/public class GenericTest {@ Testpublic void demo5 () {// use a type-safe Map --- because map is a key-Value Pair structure, specify two types of generic Map
Map = new HashMap
(); Map. put ("aaa", "111"); map. put ("bbb", "222"); // because generic is used, so the key and value types must be String // retrieve the map element ----- two // (1) traverse the Map through keySet (2) traverse the Map through EntrySet to retrieve each key-value Pair Map. entry defines a key-value pair to traverse through the getKey and getValue of the Entry // The first method is to traverse the Set through the Map keySet.
Keys = map. keySet (); for (String key: keys) {System. out. println (key + ":" + map. get (key);} System. out. println ("------------"); // The second method obtains each key-value pair Set through map's entrySet ---
> EntrySet = map. entrySet (); // each element is a key-value pair for (Entry
Entry: entrySet) {// obtain each key and value System through the getKey and getValue of the entry. out. println (entry. getKey () + ":" + entry. getValue () ;}@ Testpublic void demo4 () {// use a type-safe SetSet
Set = new TreeSet
(); Set. add ("asd"); set. add ("fdf"); set. add ("bxc"); // only String elements can be added to the generic type. // you can retrieve the Set elements. These two types are unordered, so there is one less Traversal method than List. // The first method inherits the Collection, so Iterator is used to traverse Iterator.
Iterator = set. iterator (); while (iterator. hasNext () {String s = iterator. next (); System. out. println (s);} System. out. println ("-----------------"); // The second JDK introduces the foreach loop structure and traverses the setfor (String s: set) {System. out. println (s) ;}@testpublic void demo3 () {// use type-safe ListList
List = new external list
(); List. add ("aaaa"); list. add ("bbbb"); list. add ("cccc"); // because generic is used, only elements of the String type can be added to the list. // three types of elements can be traversed. // the first type is ordered because the List is sequential (the order of storage is the same as the order of retrieval) traverse for (int I = 0; I
Iterator = list. iterator (); // iterator traversal through the iterator hasNext and next methods while (iterator. hasNext () {String s = iterator. next (); System. out. println (s);} System. out. println ("------------------------"); // The third JDK introduces the foreach loop structure and traverses the listfor (String s: list) {System. out. println (s);} System. out. println ("------------------------");} @ Testpublic void demo2 () {// application generic set // This list can only store String-type data lists
List = new ArrayList
(); List. add ("abc"); list. add ("def"); // the object in the action set, traversing the set to retrieve the data and traverse the set through the size () method and get (index) method for (int I = 0; I
Reprinted please indicate the source: http://blog.csdn.net/android_jiangjun/article/details/26622775