Java Collection Framework (JCF) collention

Source: Internet
Author: User
Tags date now save file set set

  I. Concept: A series of encapsulated, inherited, or implemented classes and interfaces that are provided in advance to achieve a purpose or function.

 Two. Features:

1. Elements can be of different types 2. Set length variable 3. Space is not fixed

  Three. Comparison of collection and collections

1.collection is the core interface for encapsulating things,

2.collections is an algorithm class for an operation set.

  Four. There are three kinds of collections (list, set, map) under the collection Interface :

   1.list collection (contains ArrayList, LinkedList, vectors) —————— > list

      ① features : Linear, that is ordered, elements placed in the order and elements stored in the order of consistency, the appearance of the list is the biggest feature is the subscript.

Selection of ②:arraylist and LinkedList and the difference between ArrayList and vector

Selection of ②-①:arraylist and LinkedList

Arraylist: The bottom of the package that appears as an array is an array (used when multiple traversal is required and a large number of queries are required).

LinkedList: The underlying package is a doubly linked list (used when there is a need to do a lot of additions, deletions, especially in the middle).

The difference between ②-②:arraylist and vector

Vector: Use it when considering thread safety, using the same usage as ArrayList.

③: Use of ArrayList to increase, delete, change, check the use of grammar:

Arraylist lst = new Arraylist ();

Add: Lst.add ("Hello"); Lst.add (New Date ());//can put any type of data

Change: Lst.set (0, "World");//Modify the data inside, you need to set the value of the mark and modified

Delete: Lst.remove (0);//directly given the corresponding subscript we want to delete the value

Check: Date now = (date) lst.get (0);//By adding, changing, and deleting the above, we delete the subscript zero data, then Lst.add (New Date ()), and from subscript 1 to the value of subscript 0

④:linkedlist usage and ArrayList of the increase, deletion, change, check is the same, just need to replace the above code in the ArrayList LinkedList can be.

⑤: generics-used when controlling a collection to manipulate only one data type:

Cases:

LinkedList <String> lst = new LinkedList <String> ();//In this case, the LST only has a String type of data.

⑥: Traversal--takes each element of the set out in turn, doing the same:

⑥-1. Traversing with normal for loop

for (int i = 0; i < lst.size (); i + +) {

System.out.println (Lst.get (i). GetDate ());//date in ③

}

⑥-2. Using iterators--iterator Complete traversal (only list, set collection use)

Cases:

Iterator it = Lst.interato (); Returns the date in object;//③

while (It.hasnext ()) {

Date d = it.next ();

System.out.println (D.getdate ());

}

Features: no subscript, go through the whole

⑥-3.for-each Loop-The underlying package is an iterator but the syntax is simpler, and you can manipulate the array

Cases:

for (Date d:lst) {

System.out.println (D.getdate ());//date in ③

}

    2.set (including HashSet, Treeset)--Set

① features: Can not place duplicate elements, no order, there is no subscript set on the representation.

②: Common subclass HashSet (how can I tell if two elements are duplicated?) )

②-1: Call the Equals method to get two objects compared to true.

②-2: The hashcode value of two elements remains the same

Note: When both conditions are met, Java considers this to be the same pair, all overriding the Equals method, and generally overriding the Hashcode method so that equals returns true to the object, and Hashcode can return the same integer value.

③:hashset from the use of the grammar of increasing, deleting, changing and checking:

Add: The method of adding elements is the same as ArrayList

Change: No Modification method

Delete: can only be deleted according to the object, or the Equals and hashcode to determine exactly which object to delete.

Check: Unable to get a specified element

④: Traversal-Removes each element of the collection once, doing the same

④-1: Normal for loop is not supported because there is no subscript

④-2: Supports iterators

④-3: Supports For-each loops

Note: The traversal method is the same as ArrayList.

    3.Map (HashMap, Properis)--mapping

① Features: Key-value pairs, key requirements unique, values can be repeated. (the order in which elements are placed is independent of the Order of storage)

② Common subclass: HashMap (mainly used for collection operations); Properis (dedicated to manipulating properties files)

Use of ③:hashmap

ImportCom.lovo.bean.StudentBean; Public classTestMap {     Public Static voidMain (string[] args) {HashMap<string, studentbean> map =NewHashmap<string, studentbean>(); //add ElementMap.put ("j34001",NewStudentbean ("Zhang3", 18,80)); Map.put ("J34002",NewStudentbean ("Li4", 28,85)); Map.put ("J34003",NewStudentbean ("Wang5", 18,87)); //Get the lengthSystem.out.println (Map.size ()); //modifying elementsMap.put ("j34003",NewStudentbean ("Zhao6", 24,75)); //Remove elements---by key to remove elementsMap.Remove ("j34003"); //gets the specified element objectStudentbean stu = Map.get ("j34003"); //Traverse Map//cannot traverse keys and values at the same time, only separate traversal//traverse all the keysset<string> KeySet = Map.keyset ();//gets all the keys, loads a set set, and returns to the caller         for(String key:keyset) {System.out.println (key); }        //iterate through all the valuescollection<studentbean> allstus = Map.values ();//get all the values and think about why not return a list or other collection type?         for(Studentbean tmpstu:allstus) {System.out.println (Tmpstu.getname ()); }       }}

Use of ④:properis

 PackageCom.lovo.props;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;Importjava.util.Properties;ImportJava.util.Set; Public classtestproperties {     Public Static voidMain (string[] args) {Properties props =NewProperties ();//Create Properties collection container FirstProps.setproperty ("J134001", "Chenguo");//call the SetProperty method into the element, each of which is a pair of key-value pairsProps.setproperty ("J134002", "John Doe");//The first parameter is a key and requires no repetitionProps.setproperty ("J134003", "Zhang San");//The second parameter is a valueProps.setproperty ("J134003", "Zhao Liu");//If a duplicate value is present, it is equivalent to a modification instead of a newString name= Props.getproperty ("J134001");//returns NULL if the key does not exist by keying in the value in the collectionSystem.out.println (name); Props.remove ("J134002");//deletes the entire key-value pair according to the keySystem.out.println (Props.getproperty ("J134002")); intSize = Props.size ();//get the number of elements in a container
//the properties action file must be a fixed-format text file---Property file---will be the second profile in work usage ranking//Save File//try {//Props.store (New FileOutputStream ("File/student.properties"), "");//} catch (FileNotFoundException e)//e.printstacktrace ();//} catch (IOException e) {//e.printstacktrace ();// } //Read the fileProperties Newprops =NewProperties (); Try{newprops.load (NewFileInputStream ("File/student.properties")); } Catch(FileNotFoundException e) {E.printstacktrace (); } Catch(IOException e) {E.printstacktrace (); } System.out.println (Newprops.getproperty ("J134001")); System.out.println (Newprops.size ()); }}

Java Collection Framework (JCF) collention

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.