Java Fundamentals 12 [collections and generics] (read head first Java Records)

Source: Internet
Author: User
Tags comparable

Collection Listknow the collection of index order, ArrayList, LinkedList, vector three sub-class implements the list interface ArrayListArrayList has no sorting method and can be sorted with Collections.sort (ArrayList object), but ArrayList in Collections.sort () must be of type string, otherwise it cannot be compiled , unless the type also inherits or implements the comparable LinkedListfor efficient collections involved in frequently inserting or deleting intermediate elements  Setgo heavy, unique, not allowed duplicate collections, TreeSet, Linkedhashset, hashset three classes implement the set interface TreeSetordered state to keep and prevent duplicationthe value of the TreeSet must be a type that implements the comparable, such as Stringtreeset<string> tree=new treeset<string> ();
tree.add ("Hello");
Tree.add ("World");System.out.println (tree); HashSetprevent duplicate collections to quickly find matching elementsby Hashcode () to get the object to join the position, to determine whether the object is repeated, hashcode () the same two objects are not necessarily equaluse A.equals (b) to determine whether a and B are equal, equivalent to A==b. objects are equal A.hashcode () and B.hashcode () must be equal  Mapdata consists of key and value, search for values by key, two keys can refer to the same object, but cannot have the same keyTreeMap, HashMap, Linkedhashmap, Hashtable implement the map interface HashMapcan be saved and removed (like a dictionary) in pairs name/valuehashmap<string,integer> scores=new hashmap<string,integer> ();scores.put ("Kathy");scores.put ("Bert", 343);System.out.println (Scores.get ("Bert"));using put (key,value) to add elements, use get (key) to return values Linkedhashmaplike HashMap, the Eggshell remembers the order in which elements are inserted, and can be set to sort by the last access of the element .   generic type generic typeUsing the <> this set of symbols to identify the use of generics, generics mean better type security, specifying that only data of the specified type can be added, and that the data comes out of that type. If you do not specify a generic, you can add any object, similar to arraylist<object> ArrayList Specifies an example of a generic:arraylist<string> thislist=new arraylist<string>at this point ArrayList can only add objects of type string and is still string type* Collections can use generics  generic type-checkingthe array Arry can be added to the subclass of the specified generic, and the set ArrayList can only be added to the specified generics, which specifies that the subclasses of generics cannot be addedFor example: Declare a method parameter of type arraylist<animal>, use only arraylist<animal> parameter,arraylist<dog> and Arraylist<cat > is not because, for example, both dog and cat inherit from Animal,dog, there is a possibility that cat does not exist, and if you call the dog's own method, it will be thrown in the wrong way. The same problem occurs when the array Arry run, except that the collection (ArrayList) is a type check at compile time, and the array (Arry) is checked at run time   three generic types1. Use only < type >such as "arraylist<animal> animals"indicates that only the type is a animal object, animal subclasses or whatever, otherwise the compilation will not passyou can now add elements to the ArrayList 2. Use <t extends type >Arraylist<t extends Animal>Public <t extends animal> void takething (arraylist<t> one,arraylist<t>)  The entends of a generic is defined here to represent inheritance or implementation, so only if the type inherits from animal or implements the animal interface is allowedso you can use animal or its sub-class cat, dog, etc. you can now add elements to the ArrayList  3. Use universal characters? To create a generic <? Extends type >arraylist<, extends animal>Public void takething (arraylist<? extends animal> one,arraylist<? extends animal>) the extends here is consistent with the 2nd meaning, which means inheritance or interface implementationwhen using the <?> declaration, the compiler does not allow anything to be added to the collection, we can call any method in the collection, but cannot add something to the collection Other Notes:generics can also be used with <? super T>, which indicates that the type used must be a parent of T or T 

Java Fundamentals 12 [collections and generics] (read head first Java Records)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.