Download Java and java Official Website

Source: Internet
Author: User
Tags addall

Download Java and java Official Website
Java-Set * main points of this section 1. Java framework set 2. Collection interface API3, Iterator interface 4. Collection sub-interface: set interface> HashSet incluhashset TreeSet5 and Collection sub-interface 2: List interface> ArrayList inclulist Vector6, Map interface> HashMap TreeMap Hashtable7, Collections tool class

1. Java Collection Overview The set can store multiple objects of varying quantities, and can also be used to save associated arrays with image relationships, collection is also referred to as container 1. Java Collection is divided into two systems: Collection and Map> Collection interface> Set: unordered and non-repeated Collection of elements> List: an ordered and repeated set of elements> Map interface: a set with a ing relationship "key-value Pair". "key" is unique. Ii. Collection interface API 1. Collection interface inheritance tree 2. Methods in Collection:> size ()-> Number of elements in the returned set> add (Object obj) -> Add an element (any type of element) to the Collection> addAll (Collection coll)-> add all the elements contained in the coll to the Collection coll1 = Arrays. asList (1, 2, 3); coll. addAll (coll1);> isEmpty ()-> determines whether the set is empty. If it is null, return True> clear ()-> clear the element * System in the set. out. println (coll);-> View the elements in the Set> contains (Object obj)-> determine whether the set contains the specified obj element, if the returned True value is included, how to store the elements in the set as custom class objects requires: the custom class overwrites the equals () method> containsAll (Collection coll) -> determining whether the current Collection contains all the elements in the coll> retainAll (Collection coll)-> retaining the elements shared by the current Collection and the coll Collection is the intersection of the current Collection and the coll set, return the result to the current Collection> remove (Object obj)-> delete an element in the Collection. If the deletion is successful, True is returned. If the return value is False, no removeAll (Collection coll) exists) -> delete all elements in the current set that contain the coll set, and return a Boolean value> equals (Object obj)-> determine whether the elements in the two sets are the same> hashCode () -> calculate the hash value in the Set and return an integer, which is mainly used in the Set for multiple points. Then you can learn more about this method> toArray () -> convert the set to an array Object [] obj = coll. toArray ();> iterator ()-> is used to traverse a set and return the Iterator = coll object of the iterator interface implementation class. iterator (); Iterator. hasNext (); // determines whether the next element iterator exists. next (); // return the elements in the set. The number of calls is returned. Iv. Collection sub-interfaces: Set interface HashSet (main implementation class), LinkedHashSet, TreeSet
> Unordered storage. The common methods in Set are defined under Collection (unordered! = Random: the order of addition is different from the order of output, but the result obtained after running is ordered. The true disorder refers to the unordered position of elements in the underlying storage, underlying implementation, such as the linked list in C Language)> non-repeated elements. When the same elements are added to the Set, the following elements cannot be added> description: to add the element to the Set, you must override the equals () and hashCode () Methods of the Object to ensure that the elements in the Set are non-repetitive.> when adding an element to the Set, when an object is added using the Hash algorithm, the hashCode () method of the class where the object is located is called to calculate the Hash value of the object. The Hash value determines the storage location of the object in the Set. If there is no object storage before this location, the object is stored directly to this location. If there is an object storage at this location, use the equals () method to compare whether the two objects are the same. If they are the same, the next object cannot be added.> requirement: the attributes of hashCode () and equals () are the same. 1. HashSet () the common methods are Collection methods. 2. Using a linked list to maintain an order added to the set. As a result, when we traverse the elements of the LinkedHashSet set, the elements are traversed in the order they are added.> the storage location of the elements is determined based on the hashCode value of the elements. However, it also uses the linked list to maintain the order of elements, this makes the elements appear to be saved in the insert order> the insertion performance is slightly lower than that of HashSet, but when iteratively accessing all the elements in the Set, good performance> no repeated set elements. 3. TreeSet> only data of the same type or object can be added.> the same element cannot be inserted, the inserted data is traversed in ascending order. When the custom class does not implement the Comparable interface, when an object is added to the TreeSet, The ClassCastExcept is reported. Ion Exception error> when a custom class object is added to the TreeSet, there are two sorting methods: ① Natural sorting, ② custom sorting> natural sorting: java is required for the custom class. lang. comparable interface and override its compareTo (Object obj) abstract method. In this method, specify the attributes of the custom class for sorting> when adding elements to the TreeSet, first Follow compareTo () method. If 0 is returned, although only the attribute values of the two objects are the same, the program considers the two objects to be the same, the next object cannot be added.> the compareTo () & hashCode () & equals () values must be consistent.> Custom sorting: You must implement the custom sorting of the Comparator class/** TreeSet: see the following steps for consistency between compare (), hashCode (), and equals! * // @ Test public void testTreeSet2 () {// 1. create a Comparator Interface Class Object Comparator com = new Comparator () {// Add the Customer Class Object to the TreeSet, In this compare () method, specifies the attribute of the Customer. @ Override public int compare (Object o1, Object o2) {if (o1 instanceof Customer & o2 instanceof Customer) {Customer c1 = (Customer) o1; Customer c2 = (Customer) o2; int I = c1.getId (). compareTo (c2.getId (); if (I = 0) {return c1.getName (). compareTo (c2.getName () ;}return I ;}return 0 ;}; // 2. pass this object as a form parameter to TreeSet constructor TreeSet = new TreeSet (com); // 3. adding the compare method in the Comparator interface to the TreeSet involves Class Object. Set. add (new Customer ("AA", 1003); set. add (new Customer ("BB", 1002); set. add (new Customer ("GG", 1004); set. add (new Customer ("CC", 1001); set. add (new Customer ("DD", 1001); for (Object str: set) {System. out. println (str );}} 5. Collection sub-interface 2: List interface 1. List new methods; List Implementation class: ArrayList, rule List, Verctor ArrayList: List main implementation class, many new methods are added to the List: * The underlying layer is implemented by array> void add (int index, Object ele) to add elements to the specified index location. This insertion efficiency is very low, if an element is inserted before, all the elements following it must be moved> boolean addAll (int index, Collection eles) to add an eles set> Object get (int index) at the specified index location) get the element at the specified index position> int indexOf (Object obj) returns the first position of obj in the Set, and no-1> int lastIndexOf (Object obj) is returned) returns the last position of obj in the set.-1> Object remove (int index) is not returned to delete the element at the specified index. After deletion, move the following data forward in sequence> Object set (int index, Object ele) modify the element of the specified index location to ele> List subList (int fromIndex, int toIndex) the returned List is a subset of indexes from fromIndex to toIndex. The efficiency of frequent insert/delete operations is much higher than that of ArrayList. Verctor has been eliminated. 6. Map interface> Map and Collection coexist to save data with ing relationships: key-Value> keys and values in Map can be data of any reference type.> keys in Map are stored in Set. Duplicates are not allowed, that is, classes of objects of the same Map object, the hashCode () and equals () methods must be rewritten.> the String class is commonly used as the "Key" of Map.> one-to-one relationship exists between Key and Value, that is, the specified Key can always find the unique Value (the Value can be repeated, and the Key is unique ).> A Key-Value is called an Entry. All entries are stored in a Set, is non-repeated * HashSet class is a special implementation of the HashMap Class 1. Map interface inheritance tree> TreeMap sorts by the specified attribute of the Key of the elements added to the Map> HashMap (main implementation class) ), keys are stored in Set and cannot be repeated. Values are stored in Collection. They can be repeated.> when an element is added to a HashMap, The equals (0 method, judge two keys ***> Hashtable is almost no longer used.> Properties is commonly used to process attribute files, key-values are of the String type> LinkedHashMap uses the linked list to maintain the order added to the Map. Therefore, when traversing the Map, the Map method is followed by the added smooth traversal. 2. Map Method:> object put (Object key, Object value)-> Add an element to Map> Object remove (Object key) -> Delete this Key-Value according to the specified e Key Value> void putAll (Map t)> void clear ()-> clear> Object get (Object key)> boolean containsKey (Object key)> boolean containsValue (Object value)> int size ()-> returns the length of the Set> boolean isEmpty ()-> determines whether the set is empty
> Boolean equals (Object obj)
> KeySet ()-> traverse the Key Set set Set = map. keySet (); for (Object obj: set) {Ssytem. out. println (obj) ;}> values ()-> traverse the Value set Collection values = map. values (); Iterator I = values. iterator (); while (I. hasNext () {System. out. println (I. next () ;}> entrySet ()-> Traverse Key-Value pairs 3. How to Traverse Key-Value pairs: ① method 1 Set set1 = map. keySet (); for (Object obj: set1) {System. out. println ("Key:" + obj + "<-->" + "Value:" + map. get (obj);} ② method 2 Set set2 = map. entrySet (); for (Object obj: set2) {Map. entry entry = (Map. entry) obj; System. out. println (entry);} 4. Use Properties to process the attribute file @ Test public void test6 () throws FileNotFoundException, IOException {Properties pros = new Properties (); pros. load (new FileInputStream (new File ("jdbc. properties "); String user = pros. getProperty ("user"); System. out. println (user); String password = pros. getProperty ("password"); System. out. println (password );} 7. The tool class * Collection and Collections of the Collection operation set are different> Collections can operate Collection and Map 1. Common Methods:> reverse (List ), reverse the order of elements in the List> shuffle (List), random sorting of elements in the List set> sort (List ), sort the elements of a specified List set in ascending order according to the natural order of the elements> sort (List, Comparator), sort the elements of the List set in the order produced by the specified Comparator> swap (List, int I, int j), swap the elements at I and j in the specified List set> Object max (Collection), according to the natural order of elements, returns the largest element> Object max (Collection, Comparator) in a given set. returns the largest element> Object min (Collection) in a given set based on the order specified by Comparator, returns the smallest element in a given set.
> Object min (Collection, Comparator): returns the minimum element of a given set in the order specified by Comparator.
> Int frequency (Collection, Object), returns the number of occurrences of the specified Element in the specified set> void copy (List dest, List src ), copy the content in src to dest> boolean replaceAll (List list, Object oldVal, Object newVal), and replace the old value in the List with the new value.

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.