Between List, Map, Set contact difference:

 

First, the difference between arrays and collections:

 1. The array size is fixed, and the same array can be the same data type

 2. The size of the collection is not fixed, you can use the set without knowing how much data there is a case.

Second, a set of three types: list (list), SET (set), Map (mapping)

Set List interface and the interface belongs to the Collection interface, Map Collection interfaces and interfaces exist side by side (same level).

 List (elements repeatability, ordering):

static void main public (String [] args) { 
     ArrayList = new new List the ArrayList <Integer> <> (); 
     arraylist.add (. 3); 
     arraylist.add (22 is); 
     arraylist.add (31 is); 
     arraylist.add (345 ); 
     arraylist.add (63 is); 
     arraylist.add (. 3); 
     // underlying the ArrayList is an array, foreach traversal needs to output, query fast, slow additions, the safety line layer, high efficiency 
     System.out.print ( "arraylist : "); 
     for (Integer Test: ArrayList) { 
        of System.out.print (Test +" "); 
    } 
     
     System.out.println (); // wrap 
     
     // Vector is an underlying array of objects to realize automatic increase, the elements will automatically added to the array, the query fast, slow additions, the safety line layer, inefficient 
     the Vector <Integer> = new new Vector the Vector <> (); 
     vector.add (. 3); 
     vector.add (. 5);
     vector.add (25 );
     vector.add (51 is); 
     vector.add (. 5); 
     System.out.println ( "Vector:" Vector +); 
     
     // bottom LinkedList is a linked list, query slow, additions fast line layer unsafe, high efficiency 
     LinkedList <Integer> = new new linkedList the LinkedList <> (); 
     linkedList.add (30); 
     linkedList.add (30); 
     linkedList.add (34 is); 
     linkedList.add (55); 
     System.out.println ( "LinkedList" + linkedList);

 operation result:

 

 Set (unique, disorder):

public static void main (String [] args) { 
          HashSet <String> hashSet = new new HashSet <> (); 
          a LinkedHashSet <String> LinkedHashSet = new new a LinkedHashSet <> (); 
          TreeSet <String> TreeSet = new new TreeSet <> (); 

          for (String Data: Arrays.asList ( "afaE", "afaE", "asfweD", "hfasfae", "aefaeA")) { 
              hashSet.add (Data); 
              linkedHashSet.add (Data); 
              treeSet.add (Data ); 
          } 

          // HashSet: disordered, random output. Underlying data structure is a hash table, two methods rely hashCode () and equals () to ensure uniqueness. 
          System.out.println ( "Ordering in HashSet:" + hashSet); 
 
          // LinkedHashSet: 
          System.out.println ( "
 
          // TreeSet: an orderly, unique. A red-black tree substructure, using natural order sorting and sorting containers comparison, based on the comparison whether the return value is 0 is determined only element. 
          System.out.println ( "Objects in the Order of TreeSet:" + TreeSet); 
    }

 operation result:

 

    Map (using key-value pairs <key, value> storage element, key unique key):

hashmap: + underlying structure is an array list, random, thread-safe, efficient, allowing a null (key value and are allowed), the parent class is AbstractMap

treemap: substructure is red-black tree, ordered, the data is sorted according to the key, the default is ascending order.

hashtable: the underlying structure is a hash table, disorderly, thread-safe, low efficiency, does not allow null values, is the parent class Dictionary

static void main public (String [] args) { 

        // the HashMap using key data stored using put () method to store data 
        the Map <Integer, String> = new new Map the HashMap <Integer, String> (); 
        map.put ( 1, "China"); 
        map.put (2, "little Japan"); 
        map.put (3, "Yankees"); 

        // foreach traversal method, commonly used by the unique features of the key, be key to traverse 
        for (Integer Key: map.keySet ()) { 
            System.out.println (Key + "->" + as map.get (Key)); 
        } 

        System.out.println ( "-------- ---------------------------- "); 

        // key unique because, when the cover is automatically duplicate key previously stored data value 
        Map. put (3, "Englishman"); 
        for (Integer key1: map.keySet ()) { 
            System.out.println (+ key1 "-->" + map.get(key1));
        }

        System.out.println ( "------------------------------------"); 

        // the Map. entry traversal key and a value, foreach same method, when a large capacity usage recommended 
        for (of Map.Entry <Integer, String> entry: EnumMap.entrySet ()) { 
            System.out.println (entry.getKey () + "- -> "entry.getValue + ()); 

        } 
    }

operation result: 

 

 

 

 

 

First, the difference between arrays and collections:

 1. The array size is fixed, and the same array can be the same data type

 2. The size of the collection is not fixed, you can use the set without knowing how much data there is a case.

Second, a set of three types: list (list), SET (set), Map (mapping)

Set List interface and the interface belongs to the Collection interface, Map Collection interfaces and interfaces exist side by side (same level).

 List (elements repeatability, ordering):

static void main public (String [] args) { 
     ArrayList = new new List the ArrayList <Integer> <> (); 
     arraylist.add (. 3); 
     arraylist.add (22 is); 
     arraylist.add (31 is); 
     arraylist.add (345 ); 
     arraylist.add (63 is); 
     arraylist.add (. 3); 
     // underlying the ArrayList is an array, foreach traversal needs to output, query fast, slow additions, the safety line layer, high efficiency 
     System.out.print ( "arraylist : "); 
     for (Integer Test: ArrayList) { 
        of System.out.print (Test +" "); 
    } 
     
     System.out.println (); // wrap 
     
     // Vector is an underlying array of objects to realize automatic increase, the elements will automatically added to the array, the query fast, slow additions, the safety line layer, inefficient 
     the Vector <Integer> = new new Vector the Vector <> (); 
     vector.add (. 3); 
     vector.add (. 5); 
     vector.add (25 );
     vector.add (51 is);  
     vector.add (51 is );
     vector.add (. 5); 
     System.out.println ( "Vector:" Vector +); 
     
     // underlying the LinkedList is a linked list, query slow, additions fast line layer unsafe, high efficiency 
     LinkedList <Integer> linkedList = new LinkedList <> (); 
     linkedList.add (30); 
     linkedList.add (30); 
     linkedList.add (34 is); 
     linkedList.add (55); 
     System.out.println ( "LinkedList" + linkedList);

 operation result:

 

 Set (unique, disorder):

public static void main (String [] args) { 
          HashSet <String> hashSet = new new HashSet <> (); 
          a LinkedHashSet <String> LinkedHashSet = new new a LinkedHashSet <> (); 
          TreeSet <String> TreeSet = new new TreeSet <> (); 

          for (String Data: Arrays.asList ( "afaE", "afaE", "asfweD", "hfasfae", "aefaeA")) { 
              hashSet.add (Data); 
              linkedHashSet.add (Data); 
              treeSet.add (Data ); 
          } 

          // HashSet: disordered, random output. Underlying data structure is a hash table, two methods rely hashCode () and equals () to ensure uniqueness. 
          System.out.println ( "Ordering in HashSet:" + hashSet); 
 
          // LinkedHashSet: 
          System.out.println ( "

          // TreeSet: an orderly, unique. A red-black tree substructure, using natural order sorting and sorting containers comparison, based on the comparison whether the return value is 0 is determined only element. 
          System.out.println ( "Objects in the Order of TreeSet:" + TreeSet); 
    }

 operation result:

 

    Map (using key-value pairs <key, value> storage element, key unique key):

hashmap: + underlying structure is an array list, random, thread-safe, efficient, allowing a null (key value and are allowed), the parent class is AbstractMap

treemap: substructure is red-black tree, ordered, the data is sorted according to the key, the default is ascending order.

hashtable: the underlying structure is a hash table, disorderly, thread-safe, low efficiency, does not allow null values, is the parent class Dictionary

static void main public (String [] args) { 

        // the HashMap using key data stored using put () method to store data 
        the Map <Integer, String> = new new Map the HashMap <Integer, String> (); 
        map.put ( 1, "China"); 
        map.put (2, "little Japan"); 
        map.put (3, "Yankees"); 

        // foreach traversal method, commonly used by the unique features of the key, be key to traverse 
        for (Integer Key: map.keySet ()) { 
            System.out.println (Key + "->" + as map.get (Key)); 
        } 

        System.out.println ( "-------- ---------------------------- "); 

        // key unique because, when the cover is automatically duplicate key previously stored data value 
        Map. put (3, "Englishman"); 
        for (Integer key1: map.keySet ()) { 
            System.out.println (+ key1 "-->" + map.get(key1));
        }

        System.out.println ( "------------------------------------"); 

        // the Map. entry traversal key and a value, foreach same method, when a large capacity usage recommended 
        for (of Map.Entry <Integer, String> entry: EnumMap.entrySet ()) { 
            System.out.println (entry.getKey () + "- -> "entry.getValue + ()); 

        } 
    }

operation result: 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/williamjie/p/11458721.html