Java Notes Collation (ix), collections in Java

Source: Internet
Author: User

In our work, we often need to put multiple objects in the set, we can use the array, but once fixed the length of the array is immutable, in order to save the quantity of the determined data, we can use the collection in Java.

In my opinion, the collection in Java can be regarded as a special database, can be used for the database to delete and modify data, you can see the size of the database, to see whether the database is empty and so on.

The collection in Java is derived from the collection and map two interfaces, and because it is a two interface, it can only be instantiated by its subclasses as a common collection:

  

  

Collection is the parent interface of the list, queue, and set interfaces, and map is the parent interface of HashMap.

Because it is an interface, the actual use is to instantiate it through its subclasses.

Collection can be regarded as a database in the traditional sense, providing a series of decision-and-change operation for database.

List  
 Public classListDemo01 { Public Static voidMain (string[] args) {List List=NewArrayList (); //add an element to the list using Add (), which shows that the list is repeatableList.add ("A"); List.add (B); List.add (A); //Iterate through the contents of a collection by looping         for(inti = 0; I < list.size (); i++) {//Use the size () method to get the list lengthSystem.out.println ("s" + (i + 1) + "elements are:" + list.get (i));//use Get (x) to get the element in the list x position        }        //use Remove (x) to remove the X-bit element from the listList.remove (1); //Iterate through the contents of a collection by looping         for(inti = 0; I < list.size (); i++) {//Use the size () method to get the list lengthSystem.out.println ("After removal, the first" + (i + 1) + "elements are:" + list.get (i));//use Get (x) to get the element in the list x position        }        //use IsEmpty () to see if the collection is empty and if True is NULL, false is not empty        if(List.isEmpty ()) {System.out.println ("This collection is empty."); } Else{System.out.println ("This collection is not empty."); }        //to add another collection to the collection using AddAllList List2 =NewArrayList (); List2.add (123); List2.add (D); List2.add (123);        List.addall (LIST2);  for(inti = 0; I < list.size (); i++) {//Use the size () method to get the list lengthSystem.out. println (After the removal, added, the "+ (i + 1) +" element is: "+ list.get (i));//use Get (x) to get the element in the list x position        }        //Use the Contains (X) method to determine whether an X element exists in the collection        if(List.contains ("Z") {System.out.println ("This element exists in the collection"); } Else{System.out.println ("This element does not exist in the collection"); }        //Use the Containsall (x) method to determine whether a collection is contained in the collectionList List3 =NewArrayList (); List3.add (A); List3.add (B); if(List.containsall (LIST3)) {System.out.println ("List contains List3"); } Else{System.out.println ("List does not contain LIST3"); }        //use indexof (x) to get the position of the X element in the collection, and if the element exists, returns the position where the element first appeared, or 1 if it does not exist .System.out.println ("123 appears in List2:" + list2.indexof (123)); System.out.println ("123 appears in List2:" + list2.indexof (456)); //use indexof (x) to get the position of the last X element in the collection, and if the element exists, returns the last occurrence of the element, or 1 if it does not exist .System.out.println ("123 Last occurrence in List2:" + list2.lastindexof (123)); System.out.println ("123 The last occurrence in List2 is:" + list2.lastindexof (456)); //use Set (x, y) to replace the X element in the list with YList2.set (2, 456);  for(inti = 0; I < list2.size (); i++) {System.out.println (After the substitution, the list's "+ (i+1) +" elements are: "+List2.get (i)); }
    
System.out.println (LIST2);
}}

All collection implementation classes rewrite the ToString () method, and ArrayList stores the data in the form of a "dynamic array", and when the System.out.println () is used to output the list directly, it outputs [xx,yy,zz ] in the form

Set

HashSet is a set implementation class, it is like a box, we can put a number of objects in this box, but we do not get into the box of objects in the order, and can not put the same object

The methods provided by HashSet and ArrayList are basically the same, except that duplicate objects are not allowed in HashSet, and false is returned if you add duplicate objects using Add (), and the newly added elements are not added.

When an element is added to a hashset, HashSet calls the pair's Hashcode () method to get the Hashcode value of the object, and then the object with the same hashcode exists in the collection, and the addition is unsuccessful.

 Public classSetDemo02 { Public Static voidMain (string[] args) {Set Set=NewHashSet (); //The X element is added to the set by the Add (x) methodSet.add ("A"); Set.add (1234); Set.add (1234); //The set's iterator () method is used to obtain the iteration period of the set, and then the value in the set is obtained by the next () method of the iteration period.//by the output you can see that the 1234 added above actually adds only the successful oneIterator Iterator =Set.iterator ();  while(Iterator.hasnext ()) {System.out.println (Iterator.next ()); } Set Set2=NewHashSet (); Set2.add (B); Set2.add (C); Set2.add (D); //Add a set collection to another by using the AddAll () methodSet.addall (Set2); Iterator=Set.iterator (); System.out.println ("After adding a collection, the elements in the collection have");  while(Iterator.hasnext ()) {System.out.println (Iterator.next ()); }                //use the Contains () method to determine whether an element is contained in the collection        if(Set.contains ("A") {System.out.println ("This collection contains a element"); }Else{System.out.println ("This collection does not contain a element"); }                //use Remove (x) to remove the X element from the collection, return True if present and remove, or false        if(Set.remove ("A") {System.out.println ("A element is present in the collection and has been removed"); }Else{System.out.println ("A element does not exist in the collection"); }    }}

Map

Java provides a collection---Map that is stored in <, value >, and you can find values by "keys". A map cannot contain the same key, and each key can only map one value.

Map is an interface, he provides HashMap, Hashtable, Linkedhashmap and other sub-classes, the actual use of HashMap.

The map interface provides three collection views that allow you to view the contents of a map in the form of a keyset, value set, or key-value mapping relationship set.

 Public classMapDemo01 { Public Static voidMain (string[] args) {Map<string, string> map =NewHashmap<string, string>(); //use put (x, y) to add a key-value pairMap.put ("Key1", "value1"); Map.put ("Key2", "value2"); Map.put ("Key3", "Value3"); Map.put ("Key4", "Value4"); Map.put ("Key5", "Value5"); Map.put ("Key6", "Value6"); //get the value of key x by Get (x)String str = map.get ("Key1");        System.out.println (str); //use ContainsKey (x) to determine if there is a key value pair of key X        if(Map.containskey ("Key3") {System.out.println ("The key value is Key3 's collection exists"); } Else{System.out.println ("A collection with a key value of Key3 does not exist"); }        //use Containsvalue (x) to determine if there is a key value pair of value x        if(Map.containsvalue ("Key3") {System.out.println ("Value value is Key3 of a collection exists"); } Else{System.out.println ("A collection with a value of Key3 does not exist"); }        //use keyset () to get all the keys in the map and return a set setSet<string> set =Map.keyset (); Iterator<String> iterator =Set.iterator ();  while(Iterator.hasnext ()) {String s=Iterator.next (); System.out.println ("Key:" + S + "----value:" +Map.get (s)); }                //use VALUES () to get all the values in the map and return a collectioncollection<string> C =map.values (); Iterator Iterator2=C.iterator ();  while(Iterator2.hasnext ()) {System.out.println (Iterator2.next ()); }                //use Remove (x) to remove key-value pairs of key xMap.Remove ("Key3"); System.out.println ("After removal, the remaining key-value pairs in the map); Collection<String> C2 =map.values (); Iterator Iterator3=C2.iterator ();  while(Iterator3.hasnext ()) {System.out.println (Iterator3.next ()); }            }}

  

  

Java Notes Collation (ix), collections in Java

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.