"Java basics of" Java collections (Collection)

As a Developer, Java collection classes that we use most at work, the most frequent category. Compared to the array (the Array), a variable-length collection class, it is more suitable for the needs of modern development;

Like a Java collection container, you can store any type of data, may be stored in conjunction with the specific type of generic objects. The program runs, Java can dynamically expand the collection, with the increase of the elements and expand. In Java, the class is typically present in the collection java.util package.

Java set mainly by two large system configuration, and are Collection System Map system, wherein the top layer and Map Collection are two major system interface.

Today the main stresses: Collection There are three sub-interfaces, respectively, List (list), Set (set), Queue (queue). Wherein, List, Queue ordering may be repeated in the elements, and the elements Set disorder not repeat;

List of main ArrayList, LinkedList implement two classes; Set HashSet is in the implementation class; and Queue after the emergence of the new collection is in JDK1.5, mainly in order to arrays and linked lists two forms.

Inheritance diagram:

Common collection: ArrayList (array)

Feature

  • Capacity is not fixed, as capacity increases dynamically expansion (substantially not reach the threshold value)
  • Ordered set (the order of insertion sequence == output)
  • Inserted elements may be null
  • More efficient (relative to LinkedList is)
  • Thread safe

list common method

import java.util.ArrayList;
import java.util.List;

public class var {
    public static void main(String[] args){
        List<String> list = new ArrayList();
        List<String> list1 = new ArrayList();

        //list添加数据
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.add("赵六");
        System.out.println("add:"+list);

        //list的长度
        System.out.println ( "size:" + list.size ()); 

        // set switch array 
        Object [] = ARR List.toArray (); 
        System.out.println ( "toArray:" + ARR); 

        // determining whether the set is empty 
        iF (! {list.isEmpty ()) 
            System.out.println ( "set list is not empty." ); 
        } 

        // determines whether the collection contains an element 
        iF (list.contains ( "Zhang three " )) { 
            System.out.println ( " Joe Smith comprising List. " ); 
        } 
        IF (list.contains (" Lee III " )) {
             //
         } the else {
            System.out.println ( if"list does not contain Li three." ); 
        } 

        // Copy List 
        list1.addAll (List); 
        System.out.println ( "addAll:" + List1); 

        // List delete data 
        list.remove (0); // According to the subscript deleted 
        ; list.remove ( "King of five") // delete according to content. 
        System.out.println ( "Remove:" + List); 

        // get the first two data 
        System.out.println ( "GET:" List.get + (. 1 )); 

        // Clear element 
        list.clear () ; 
        System.out.println ( "Clear:" + List); 

        // determines whether the set is empty
        (list.isEmpty ()) { 
            System.out.println ("Set list is empty." ); 
        } 
    } 
}

operation result:

list, there are other methods, see the JDK API.

 

Common collections: LinkedList (list)

Features: insert, delete fast.

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class var {
    public static void main(String[] agrs){
        List<String> linkedList = new LinkedList<String>();
        System.out.println("LinkedList初始容量:"+linkedList.size());

        //添加功能:
        linkedList.add("my");
        linkedList.add("name");
        linkedList.add("is");
        linkedList.add("jiaboyan");
        System.out.println ( "current capacity of the LinkedList:" + linkedList.size ()); 

        // modify the function: 
        linkedList.set (0, "Hello" ); 
        linkedList.set ( . 1, "World" ); 
        the System.out .println ( "current contents of the LinkedList:" + linkedList.toString ()); 

        // Get functions: 
        String = linkedList.get Element (0 ); 
        System.out.println (Element); 

        // iterate set: (LinkedList actual fall is subject ListItr) 
        the Iterator <String> Iterator =   linkedList.iterator ();
         the while (iterator.hasNext ()) { 
            String Next = Iterator.next ();
            System.out.println (Next); 
        } 
        // for loop iteration set: 
        for (String STR: linkedList) { 
            System.out.println (STR); 
        } 

        // determining function: 
        Boolean isEmpty = linkedList.isEmpty ();
         Boolean = linkedList.contains isContains ( "jiaboyan" ); 

        // length function: 
        int size = linkedList.size (); 

        // delete function: 
        linkedList.remove (0 ); 
        linkedList.remove ( "jiaboyan" ); 
        linkedList.clear ( ); 
        System.out.println ( "current capacity of the LinkedList:" + linkedList.size ()); 
    } 
}

operation result:

 

ArrayList and  LinkedList contrast

1. Insert Data: LinkedList high efficiency.

2. Query Data: ArrayList efficiency is very high.

3. Space utilization: LinkedList high.

 

Unusual set: Vector (queue)

Features: Thread safety

Import java.util.Arrays;
 Import java.util.Vector; 

public  class var {
     public  static  void main (String [] as AGRS) { 
        the Vector Vector = new new the Vector (); 

        // add to the set of elements 
        vector.add ( " a " ); 
        vector.add ( " B " ); 
        vector.add ( " C " ); 
        System.out.println (Vector); 

        // add to the specified position of an element 
        vector.add (. 1," F. " ); 
        vector.add ( 3, "H" ); 
        System.out.println (the Vector); 

        // remove elements
        vector.remove ( "H" ); 
        vector.remove ( . 1 ); 
        System.out.println (Vector); 

        // modify elements 
        vector.set (. 1, "G" ); 
        System.out.println (Vector); 

        / / query element 
        System.out.println ( "GET:" vector.get + (. 1 )); 

        // determine whether the current collection is empty 
        Boolean B = vector.isEmpty (); 
        System.out.println (B); 

        // returns the number of elements in the set of vector 
        int I = vector.size (); 
        System.out.println ( "size:" + I); 

        // to set the objects into an array Object 
        Object [] array = vector.toArray ( );
        System.out.println(Arrays.toString(array));
    }
}

operation result:

Because vector thread-safe, leading to low efficiency, so use less.

 

Unusual collection: Stack (Stack)

Features: last out

Import the java.util.Stack; 

public  class var {
     public  static  void main (String [] as AGRS) { 
        Stack Stack = new new Stack (); 

        // Providing push, the write element 
        stack.push ( "A" ); 
        Stack. Push ( "B" ); 
        stack.push ( "C" ); 

        // providing push, a writing element 
        stack.push ( "D" ); 
        System.out.println (Stack); 

        // POP pulling a element, and removing 
        System.out.println (stack.pop ()); 
        System.out.println (Stack); 

        // pull element is not removed
        System.out.println(stack.peek());
        System.out.println(stack);

        // 查找search
        System.out.println(stack.search("B"));
    }
}

operation result:

 

Common collections: HashSet (hash)

 Features: elements will not be repeated.

import java.util.HashSet;
import java.util.Set;

public class var {
    public static void main(String[] agrs){
        Set set= new HashSet();
        set.add("1");
        set.add("1");
        set.add("2");
        set.add("3");
        set.add("4");
        set.add("5");
        set.add("5");
        System.out.println(set);
    }
}

operation result:

 

 

 

Common collections: TreeSet (binary)

Features: TreeSet is used to sort, you can specify a sequence is sorted in the order specified after the object is stored

public class Person {

    public String name;    //姓名
    public Integer age;    //年龄

    public Person(String name,Integer age) {
        this.name=name;
        this.age=age;
    }
}
import demo.design.strategy.imp.Person;
import java.util.TreeSet;

public class var {
    public static void main(String[] agrs){
        TreeSet<Person> ts = new TreeSet<>();
        ts.add(new Person("张三", 23));
        ts.add(new Person("李四", 13));
        ts.add(new Person("周七", 13));
        ts.add(new Person("王五", 43));
        ts.add(new Person("赵六", 33));
        System.out.println(ts);
    }
}

operation result:

 

 

Because TreeSet need to sort, it is necessary to specify class sorting logic

public  class the Person the implements the Comparable <the Person> { 

    public String name;     // name 
    public Integer Age;     // Age 

    public the Person (name String, Integer Age) {
         the this .name = name;
         the this .age = Age; 
    } 

    / ** 
     * plus the comparative method, so that this sort can TreeSet 
     * @param O 
     * @return 
     * / 
    public  int the compareTo (the Person O) {
         IF (o.age> the this .age) {
             return 1;
        }else if(o.age > this.age){
            return -1;
        }else {
            return 0;
        }
    }
}
import java.util.Iterator;
import java.util.TreeSet;

public class var {
    public static void main(String[] agrs){
        TreeSet<Person> ts = new TreeSet<>();
        ts.add(new Person("张三", 23));
        ts.add(new Person("李四", 13));
        ts.add(new Person("周七", 13));
        ts.add(new Person("王五", 43));
        ts.add(new Person("赵六", 33));

        using an iterative manner to obtain elements//
        Iterator<Person> it = ts.iterator();
        while (it.hasNext()){
            Person person= it.next();
            System.out.println("姓名:"+person.name+"年龄:"+person.age);
        }
    }
}

operation result:

 

Can grasp more than a collection of clinker, has had enough work in the future, the need to continue to understand, you can read the JDK API documentation.

Reference: https://www.jianshu.com/p/63b01b6379fb

           https://www.jianshu.com/p/a2236f562ead

           https://www.cnblogs.com/yzssoft/p/7127894.html

/ ** 
* plus the comparative method, so this TreeSet can sort
* @param O
* @return
* /

Guess you like

Origin www.cnblogs.com/jssj/p/11456257.html