Java After-Class Summary 14 (Collection Framework)

Collection frame

The Java Collections Framework provides a set of interfaces and classes with excellent performance and ease of use . (Note: all in the Java.util package)

Interface: Iterator (iteration), Collection , List , Set , Map   (Note: List and Set both inherit Collection )

Implementation classes: (1) The implementation classes of the List interface are: ArrayList , LinkedList ;

             (2) The implementation classes of the Set interface are: HashSet , TreeSet

             (3) The implementation classes of the Map interface are: HashMap , TreeMap

Utility classes: Collections, Arrays

Storage characteristics of each interface: Collection: store a set of non-unique, unordered objects 

                                  List: The interface stores a set of non-dimensional, ordered (insertion order) objects        

                                  Set : interface stores a set of one-dimensional, unordered objects   

                                 Map: The interface stores a set of key-value objects, providing a mapping from key to value

ArrayList collection class   ( not one-dimensional, ordered )

Array List newList=new ArrayList(); // Instantiate an ArrayList     (Note: the instance must be packaged)

newList.add(news1);      // Add an object to the collection

System.out.println(newList.size());       // Output the length of the collection

for (int i = 0; i < newList.size(); i++) { // traverse the collection News biaoTi=(News)newList.get(i); // because the traversal is an Object object, so the type conversion is required   System.out.println(biaoTi.getTitle());       }



LinkedList collection class    ( not one-dimensional, ordered )

LinkedList newList=new LinkedList();      // Instantiate a LinkedList      (Note: the instance needs to import the package)

newList.add(news1);      // Add an object to the collection

newList.addFirst(news3); // Put the given object in the first place of the collection

newList.addLast(news2); // Put the given object to the last digit of the collection

newList.remove (news1);   // Remove an object in the collection

(Note: Comparison of ArrayList and LinkeList: ArrayList collection is more convenient to query objects in the collection, and LinkeList collection has less impact on other objects in the collection when inserting and deleting objects in the collection;)

Set interface implementation class is HashSet   ( dimension one, unordered)

Set s=new HashSet();    //Instantiate a set collection object   (Note: the instance needs to import the package)

s.add(news1); // Add an object to the collection         

Note: Because the objects stored in the Set interface are unordered and cannot be traversed with subscripts, traversing the Set collection can only be used: "enhanced for" and " Iterator iteration".

Enhanced for :  for (Object obj : s) {         //Object : Object type traversed obj: Object name s: Set collection name News biaoTi=(News)obj;       //Force obj whose original type is Object to News object System.out.println(biaoTi.getTitle());


}


Iterator iterator traversal:

Iterator method: boolean hasNext();   //Determine whether there is another accessible element in the collection , return true or false 

                         Object next();   //returns the next element to be accessed

Iterator it=s.iterator();    // Get the iterator Iterator name: it (Note: Get the package to import Iterator ) 

while(it.hasNext()) {                     //Use the loop to judge whether the collection has the next element, return to true, continue the loop, return false to exit the loop
News biaoTi=(News)it.next();      //because it .next(); is of type object and needs to be cast.
System.out.println(biaoTi.getTitle());      // Traverse the properties of each object in the output Set collection

}

Map interface

The Map interface specializes in the storage of key-value mapping data, and can implement operations on values ​​based on keys.

HashMap of the most commonly used implementation class of the Map interface; (key; value)

 Map map=new HashMap();     //Instance a MAP set  (Note: the instance needs to import the package)

map.put("CN","China" );        //Add a key-value pair to the collection; "CN"=key(key), "China"=value(value) 

String cn1=( String) map.get("CN")   //Get the corresponding value through the key, you need to force conversion 

System.out.println(cn1);       //output value

map.clear();         //Clear the key-value pairs in the collection

Generics

Specify the type of the object as a parameter to other classes or methods to ensure the safety and stability of type conversion. (parameterized type)

Such as: Student stu1=new Student("Xiao Ming", "Male");

        Map<String,Student> map=new HashMap<String, Student>();

           map.put("ming", stu1);

            for (String s : map.keySet() ){
System.out.println(s+" The corresponding student's name is "+map.get(s).getName()+" The student's gender is: "+map.get(s ).getSex());
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324610580&siteId=291194637