Java self-study notes (22): [collection] collection overview, Collection, Map

Collections Overview

Single and double row Map Collection

There are two sub-interfaces interface Collection: List interfaces (re-ordered) and Set interfaces (disorder can not be repeated)

Map interface Map <K, V> TreeMap main subclasses and HashMap


Recognize separate collection Collection

Interface method:

// Add Method: 
the Add (Object O) // add the specified element 
the addAll (Collection C) // add the specified collection
 @ deletion method: 
Remove (Object O) // delete the specified element 
removeAll (Collection C) // output two intersection of the sets 
retainAll, (C collection) // reserved intersection of two sets 
Clear () // empty set
 // query method: 
size () // active element number set 
toArray () // the elements in the collection is converted into an array of type Object
 @ Analyzing method: 
isEmpty () // determines whether an empty 
the equals (Object O) // determines whether the specified elements of the same 
the contains (Object O) //Determining whether the specified element comprising 
containsAll (Collection C) // determines whether the specified set comprising
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class AddSelfDemo {

    public static void main(String[] args) {
        // Collection中AddAll方法演示
        List<String> list = new ArrayList<String>();//实例化
        //添加元素
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        
        list.addAll(list);
        //输出
        System.out.println(list);
        
        
        
        Set<String> set = new HashSet<String>();
        set.add("1");
        set.add("2");
        set.add("3");
        set.add("4");
        set.add("5");
        set.add("6");
        
        set.addAll(set);
        System.out.println(set);

List can add their own, Set not add itself
//
}
    }// List can be repeated, Set unrepeatable
import java.util.ArrayList;
import java.util.List;

public class ToArrayDemo {

    public static void main(String[] args) {
        // ToArrayDemo方法演示
        List<String> list = new ArrayList<String>();//实例化
        //添加元素
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        
        //无参ToArray
        Object[] str = list.toArray();
        printArr(str);                //1 2 3 4 5 6 
        
        //有参ToArray
        String[] str01 = new String[list.size()-1];
        String[] str02 = list.toArray(str01);
        printArr(str01);  //null null null null null 
        printArr(str02);  //1 2 3 4 5 6 
    }
    public static void printArr(Object[] str) {
        for(int i=0;i<str.length;i++ ) {
            System.out.print(str[i]+" ");
        }
        System.out.println();
    }

}

Double set of columns Map

Find their own method, too hard to write a

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class MapMethodDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Student[] stu = new Student[100000];
        for(int i=0;i<stu.length;i++) {
            stu[i]= new Student("2019"+(i+1),"张"+(i+1),18,"软件工程");
        }
        //加入Map
        Map<String,Student> map = new HashMap<String,Student>();
        for(int i=0;i<stu.length;i++) {
            map.put(stu[i].no,stu[i]);
        }
        
        //遍历
        //映射关系
        Set<Entry<String,Student>> set = map.entrySet();
        //iterator()
        Iterator<Entry<String,Student>> iterator = set.iterator();
        while(iterator.hasNext()) {
            Entry<String,Student> entry = iterator.next();
            System.out.println(entry.getKey());
        }
        
        
        //toArray()
        Object[] obj = set.toArray();
        for(int i=0;i<obj.length;i++) {
            Entry<String,Student> entry = (Entry<String,Student>)obj[i];
            Student stu01 =entry.getValue();
            stu01.print();
        }
        
        //查找前一千是否存在
        //key
        for(int i=0;i<1000;i++) {
            boolean isExist = map.containsKey(stu[i].no);
            System.out.println(isExist);
        }
        //value
        for(int i=0;i<1000;i++) {
            boolean isExist = map.containsValue(stu[i]);
            System.out.println(isExist);
        }
    }

}

 

Guess you like

Origin www.cnblogs.com/tkj521Ya/p/11297380.html