Set of arrays

An array of advantages:

  1. Once initialized the array length is determined
  2. Array stores a single data type
  3. Call the array element array index by way of
  4. Stored in the plurality of elements in the array are ordered, repeatable, use memory space is continuous

An array of drawbacks:

  1. Once initialized, not variable length
  2. The array is relatively simple operation surface, not related to add, delete, change, search, and other methods of operation
  3. For the development of complex issues, it is difficult to fully describe the use of an array, such as: disorderly storage, data unrepeatable

set:

  1. Collection Interface: a storage is a data
    1. ----> List Interface: re-ordered data
      1. --->ArrayList,LinkedList,Vector
    2. ----> Set Interface: disorderly non-repeatable data
      1. --->HashSet,LinkedSet,TreeSet
  2. Map interface: data storage is one of the, key-value
    1. ----->HashMap,LinkedMap,TreeMap,Hashtable,Properties

Framework set advantage: solving the drawbacks of the plurality of array operation objects

Note: add objects to the Collection, the object must override equals () method

Collection common method;

package collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;

public class CollectionTest {
    
    @Test
    public void test1() {
        //添加元素
        Collection coll=new ArrayList();
        coll.add("aa");
        coll.add(new Object());
        coll.add(1);
        System.out.println(coll);
        
        //获取集合长度
        System.out.println(coll.size());
        
        // .addAll () 
        Collection Colli = new new   the ArrayList <> (); 
        coll1.add ( 2 ); 
        coll1.addAll (Coll); 
        System.out.println (Colli); 
        
        // Clear () Clear set 
        coll.clear ( ); 
        
        // determines whether the set is empty 
        System.out.println (coll.isEmpty ()); 
        
        coll1.add ( new new the Person ( "Tom", 12 is )); 
        System.out.println (Colli); 
        
        // contions ( Object obj) method: to determine whether the element contains elements obj
         // actually called Object in the equals () method
         // use this method must be overridden where the class equals () method 
        System.out.println (coll1.contains ( new newThe Person ( "tom", 12 ))); 
        
        // contionsAll (): determine whether coll1 all the elements are present in the collection coll 
        System.out.println (coll.containsAll (coll1)); 
        
        // the Remove (): Delete
         // this time to ensure the existence of yo delete elements present in the collection 
        coll.add ( "ABCD" ); 
        coll.remove ( "ABCD" ); 
        System.out.println (Coll); 
        
        // removAll () to delete from the current collection and a set of common elements coll 
        System.out.println (coll1.removeAll (coll)); 
        
        // retainAll (): get the current set of elements common to the collection coll 
        System.out.println (coll1.retainAll (coll)); 
        
        / / the equals () methods 
        
        // the hashCode () 
        
        // toArray (): set to an array 
        coll1.add ( "AAAA" );
        Object [] arr=coll1.toArray();
        System.out.println(Arrays.toString(arr));
        
        //数组转换为集合
        Collection c=Arrays.asList(new String[] {"aaa","ddd","ccc"});
        System.out.println(Arrays.asList("mm"));
        System.out.println(c);
    }
    
}

// use Iterator () iterator

    A set of interfaces to initialize Collection:    Collection C = new new the ArrayList ();

 

Call the iterator method to generate a set of iterator object iterator = the Iterator c.iterator ();

 

package collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.junit.Test;

public class IteratorTest {
    @Test
    public void test1() {
        Collection c=new ArrayList();
        c.add("mm");
        c.add(new Person("Tom", 12));
    //    System.out.println(c);
        
        //iterator():返回一个迭代器实例
        Iterator iterator=c.iterator();
        
    //    System.out.println (iterator.getClass ());
     //     System.out.println (Iterator.next ());
     //     System.out.println (Iterator.next ());
         // System.out.println (iterator.hasNext ()); 
    
        // for 
        for ( int I = 0; I <c.size (); I ++ ) { 
            System.out.println (Iterator.next ()); 
        } 
        // enhanced for loop 
        / * 
         * for (type of the element to traverse a set of temporary variables: to traverse the set of variables) { 
         * Sout (temporary variables); 
         *} 
         * / 
        for (Object Object: C) { 
            System.out.println (Object); 
        } 
        
        // hasNext () recommended
        while (iterator.hasNext()){
            System.out.println(iterator.next());
            
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/ylblikestudyJava/p/12390123.html
set
set