Java Collection-Collection-list-set-map-iterator learning Notes

Source: Internet
Author: User

1. Collection Interface

A collection can be understood as a dynamic array of objects, two meanings:

1), Dynamic: Objects in the set can be arbitrarily expanded and truncated;

2), the collection can hold arbitrary objects.

the collection has a higher performance than an array, and it is easier to extend and modify the data.

Common Subinterfaces for collection: List, Set, Queue.


2. List interface

The list collection can hold arbitrary objects, and objects in the collection can be duplicated.

List of common implementation classes: ArrayList, Vector.

Comparison of ArrayList with vectors:

Performance: ArrayList adopts asynchronous processing mode, high performance, vector synchronous processing, low performance;

Linear security aspect: ArrayList is non-thread safe, vector is thread safe.

"ArrayList instance"

<span style= "FONT-SIZE:14PX;" >import Java.util.arraylist;import Java.util.list;public class ListDemo1 {public static void main (string[] args) { list<string> lists = Null;lists = new arraylist<string> (); Lists.add ("Hello"); Lists.add ("World"); Lists.add ("Hello"); Allow objects in list to repeat for (int i=0; i< lists.size (); i++) {System.out.println (Lists.get (i));} System.out.println ("delete operation ...."); Lists.remove (2); for (int i = 0; I < lists.size (); i++) {System.out.println (Lists.get (i));} if (Lists.isempty ()) {System.out.println ("collection is empty ..."); else {System.out.println ("the collection is not empty ..."); if (Lists.contains ("World")) {System.out.println ("contains world" in the collection); System.out.println ("World where" is: "+ Lists.indexof (" World "));} else {System.out.println ("the collection does not contain world");} list<string> lists2 = new arraylist<string> () Lists2.add ("World"); Lists2.add ("Hello"); if (Lists.equals ( LISTS2) {//list is compared with the order in which elements are deposited System.out.println ("lists equals Lists2");} else {System.out.println ("lists and lists2 not Equal");} }}</span>

"Vector instance"

<span style= "FONT-SIZE:14PX;" >import Java.util.list;import Java.util.vector;public class VectorDemo1 {public static void main (string[] args) {List <String> lists = Null;lists = new Vector<string> (), Lists.add ("Hello"), Lists.add ("World"), for (int i = 0; I < ; Lists.size (); i++) {System.out.println (Lists.get (i))}}} </span>

3. Set interface

A repeating element cannot be placed in the set collection.

set sets are automatically sorted;

Set common implementation class: Hashset,treeset.

"HashSet instance"

Import Java.util.hashset;import Java.util.iterator;import Java.util.set;public class SetDemo1 {public static void main ( String[] args) {set<string> sets = Null;sets = new hashset<string> (); Sets.add ("D"); Sets.add ("A"); Sets.add ( "C"); Sets.add ("B"); Sets.add ("E");iterator<string> its = Sets.iterator (), while (Its.hasnext ()) {/* Iteration output */ System.out.println (Its.next ());} set<string> sets2 = null;sets2 = new hashset<string> (); Sets2.add ("E"); Sets2.add ("D"); Sets2.add ("A"); Sets2.add ("C"); Sets2.add ("B"); the If (Sets.equals (sets2)) {//Set comparison is independent of the order in which the elements are deposited System.out.println ("sets equals sets2!");} else {System.out.println ("Sets is not equal to sets2!");}}}

4. Iterator Interfaceiterator is the standard operation of the collection output.

Iterator is a collection of specialized iterative output interfaces that iterate over the elements to determine whether they have content or, if anything, take the content out.

For a concrete example, see the example above.


5. Map Interface

Map is saved in key/value form.

The common implementation class for map: Hashmap,hashtable.

Key duplication is not allowed.

"HashMap instance"

Import Java.util.collection;import java.util.hashmap;import Java.util.iterator;import Java.util.Map;import Java.util.set;public class MapDemo1 {public static void main (string[] args) {map<string, string> Map = new HASHMAP&L T String,string> (); Map.put ("Key1", "Hello"), Map.put ("Key2", "World"), Map.put ("Key3", "HI"); String s = map.get ("Key2"); Through key, get value; System.out.println (s); set<string> sets = Map.keyset (); Get all key;iterator<string> Iteskey = Sets.iterator (), while (Iteskey.hasnext ()) {System.out.println ( Iteskey.next ());} collection<string> cols = Map.values (); Get all the value; iterator<string> itesval = Cols.iterator (); while (Itesval.hasnext ()) {System.out.println (Itesval.next ());} if (Map.containskey ("Key1")) {System.out.println ("key exists");} if (Map.containsvalue ("World")) {System.out.println ("value exists");} Map.Remove ("Key1"); Remove the specified key}}



Java Collection-Collection-list-set-map-iterator learning Notes

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.