Java Learning (Collection Framework)

Set framework

Overview: Array length is fixed, when the added elements exceed the length of the array, we need to redefine the array. It's too troublesome. Java provides us with collection classes, which can store arbitrary objects. The length can be changed. It increases with the increase of elements and decreases with the decrease of elements.

Differences between arrays and collections:

Arrays can store both basic data types and reference data types. (The basic data type stores values, and the reference data type stores address values)

Collections can only store reference data types (objects)

* Basic data types can also be stored in collections, but they are automatically boxed into objects when stored.

Use of arrays and collections:

If the number of elements is a fixed recommended array.

If the number of elements is not a fixed recommended set.

 

        Collection c = new ArrayList();
        c.add(new Student("Zhang San", 23));
        c.add(new Student("Li Si", 23));
        c.add(new Student("Wang Wu", 23));
        Object[] arr = c.toArray();                                 // Converting a collection into an array
          for (int i = 0; i < arr.length; i++) {
                Student s=(Student)arr[i];                          //Downward transformation
                System.out.println(s.getName()+"...."+s.getAge());  //Get the value (which can be assigned or stored in this way)....)
            //  System.out.println(arr[i]);                         //This is tightly for print display. 
            }
Collection traversal array

 

        Collection c1 = new ArrayList();
          c1.add("a"); // Add to
          c1.add("b");
          c1.add("c");
          c1.add("d");

        Collection c2 = new ArrayList();
          c2.add("a"); // Add to
          c2.add("b");
          c2.add("c");
          c2.add("d");
        
//        c1.addAll(c2);                  //addAll()take c2 Each element in the c1 In the collection.
//        c1.add(c2);                     //add()take c2 Add as an object c1 Medium.
//        c1.removeAll(c2);               //removeAll()Delete the intersection.
//        Boolean b = c1.containsAll(c2); //containsAll() Determines whether the collection invoked contains the incoming collection.
          Boolean b = c1.retainAll(c2);     // retainAll()Take intersection. If the collection of calls changes, it returns true,Return without change false. 
          System.out.println(b);
Collection Set with All Function

 

Set framework (iterator traversal of set traversal)

Summary: Collections are used to store data, which needs to be viewed and iterated (traversal).

Principle: Iterators traverse collections. The internal storage structure of each collection is different, so each collection storage and fetch is different, so we need to define hasNext() and Next() methods in each class. This is possible, but it will make the whole collection system too bloated. The iterator is to extract the interface from the method upward, and then define its own iteration method within each class.

Benefits: 1. Provides that the entire set system is traversed by hasNext() and Next() methods.

2. Code has internal implementation at the bottom. Users don't need to care about what implementation they can use.

        Collection c1 = new ArrayList();
        c1.add(new Student("Zhang San", 23));
        c1.add(new Student("Li Si", 23));
        
         Iterator it=c1.iterator();               //Obtaining iterators
         while (it.hasNext()) {                   //Iterative Methods in Sets(ergodic)
            Student s=(Student) it.next();        //Downward transformation
            System.out.println(s.getName()+"..."+s.getAge());
            }
Iterative method

 

Collection Framework (List Collection Specific Functions)

    

   List list =new ArrayList();
        list.add(111);
        list.add(222);
        list.add(333);
        list.add(444);
        
        list.add(1,555);             //add(index, element)Adding elements by index
        list.remove(2);              //remove(index)Delete elements by index, which will not be automatically boxed when deleted
        Object s=list.get(1);        //get(index)Getting elements by index
        list.set(1, 222);            //get(index)Modify elements by index
        Iterator it=list.iterator(); // Obtaining iterators    
        // Iteration of elements in a set(ergodic)
        while (it.hasNext()) {    //Judge whether there are elements in the set, and return if there are. true
            System.out.println(it.next()); 
            
        }
List function

 

     // Demand: Judgment List Does a collection contain"world"This element, if any, is added"javaee"element
        List list = new ArrayList();
        list.add("a");
        list.add("b");
        list.add("world");
        list.add("c");

        ListIterator it = list.listIterator(); // Obtaining iterators(List Set-specific)
        while (it.hasNext()) {
            String s = (String) it.next(); // Downward transformation
            if ("world".equals(s)) {
                it.add("javaee");
            }

        }
        System.out.println(list);
Iterative Set Add Elements

 

Collection Framework (Vector's Special Function)

    

Vector v = new Vector();
        v.addElement("a");
        v.addElement("b");
        v.addElement("c");

        Enumeration e = v.elements();
        while (e.hasMoreElements()) {
            System.out.println(e.nextElement());

        }
Iteration of Vector

 

Collection framework (arrays and linked lists of data structures)

Array:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Collection c1 = new ArrayList();c1.add(new Student("Zhangsan", 23); c1. add (new Student ("Li Si", 23); Iterator it = c1. iterator (); / / / Get iterator while (it. hasNext (){iteration methods in the set () Student = (traversal) it. next (); / / / Downward transformation system. println out (s. println s. geme ()+ + getA). Ge ());}

Tags: PHP JavaEE Java

Posted on Sat, 25 May 2019 15:57:26 -0700 by maskme