java collection framework

The Java Collection Framework, also known as container, is a set of interface interfaces and implementation classes defined under the java.util package. It mainly displays that multiple elements are placed in a unit for adding, deleting, modifying and querying these elements.
The implementation class of the interface is the knowledge of our data structure learning. Here are a brief introduction to these interfaces

Collection: used to store and manage a set of object objects, which are generally called element elements

  • Set: the element cannot be repeated, and the search / search semantics are hidden behind it
    SortedSet: a set of ordered elements that cannot be repeated
  • List: linear structure
  • Queue: queue
  • Dequeue: two terminal queue
    Map: key value to key value, mapping relationship
  • SortedMap: a set of ordered key value pairs

Collection

Method signature Explain
boolean add (E e) Put element e in the collection
void clead () Delete all elements in the collection
boolean isEmpty () Determine whether the set is an empty set
boolean remove (Object e) If element e appears in the collection, delete one of them
int size () Returns the number of elements in the collection
Object[] toArray () Returns an array containing all the elements in the collection

Example

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

public class CollectionDemo {
    public static void main(String[] args) {
        //Collection is an interface used to store and manage a group of objects. These objects are generally called elements, in which are the types of elements
        //Here, the type of element in ArrayList is String
        Collection<String> list = new ArrayList<>();
        //In the Collection interface, the default output [] indicates that the linear table exists, but it is empty
        System.out.println(list);//[]
        System.out.println(list.size());//0
        System.out.println(list.isEmpty());//true
        System.out.println("=====================");
        //list.add(); endplug
        list.add("Small");
        list.add("Small");
        list.add("year");
        list.add("I");
        list.add("love");
        list.add("you");
        System.out.println(list);
        System.out.println(list.size());//5
        System.out.println(list.isEmpty());//false
        System.out.println("====================");
        //Put a collection in an array, and the array type can only be Object
        Object[] arr = list.toArray();
        System.out.println(Arrays.toString(arr));
        for(String e : list){
            System.out.println(e);
        }
        System.out.println("================");
        //list.remove(); delete element
        //list.remove("other"); if there is no such element in the collection, no error will be reported
        list.remove("Small");
        for(String e : list){
            System.out.println(e);
        }
        System.out.println("=======================");
        //Clear linear table
        list.clear();
        System.out.println(list);
        System.out.println(list.size());
        System.out.println(list.isEmpty());
    }
}
//results of enforcement
[]
0
true
=====================
[Small, Small, year, I, love, you]
6
false
====================
[Small, Small, year, I, love, you]
//Small
//Small
//year
//I
//love
//you
================
//Small
//year
//I
//love
//you
=======================
[]
0
true

Map

Method signature Explain
V get (Object k) Find v according to the specified k
V getOrDefault (Object k,v defaultValue) Find value according to the specified k. if not, the default value will replace
V put (K key,V value) Put the specified k-v into the Map
boolean containsKey (Object key) Determine whether key is included
boolean containsValue (Object value) Judge whether value is included
Set<Map,Entry<K,V>> entrySet () Return all key value pairs
boolean isEmpty () Judge whether it is empty
int size () Returns the number of key value pairs

Example

import java.util.HashMap;
import java.util.Map;

public class MapDemo {
    public static void main(String[] args) {
        //The key value in the Map corresponds one by one, and the two element types are String
        Map<String,String> map = new HashMap<>();
        System.out.println(map);//{}
        System.out.println(map.size());
        System.out.println(map.isEmpty());
        System.out.println("========================");
        //map.get(k); find value according to the given key, key cannot be repeated, value can be repeated
        //key equals phone number value equals name mapping many to one
        System.out.println(map.get("author"));//null
        //map.getOrDefault(); find value according to the specified key. If there is no value, use the default value instead
        System.out.println(map.getOrDefault("author", "nameless"));
        //map.containsKey(); determine whether the key is included
        //Here, judge whether the book has an author, because the relevant information of the book is not included at this time,
        // So no author, no anonymous, just a default value
        System.out.println(map.containsKey("author"));
        System.out.println(map.containsKey("nameless"));
        System.out.println(map);
        System.out.println(map.size());
        System.out.println(map.isEmpty());
        System.out.println("==============");
        map.put("author","Lu Xun");
        map.put("Title","A Madman's Diary");
        map.put("Creative time","1918 year");
        System.out.println(map);
        System.out.println(map.size());
        System.out.println(map.isEmpty());
        System.out.println("================");
        System.out.println(map.get("author"));//Lu Xun
        System.out.println(map.getOrDefault("author","nameless"));//Lu Xun
        System.out.println(map.containsKey("author"));//true
        System.out.println(map.containsValue("nameless"));//false
        System.out.println(map.containsValue("Lu Xun"));//true
        System.out.println("=================");
        for(Map.Entry<String,String> entry : map.entrySet()){
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}
//results of enforcement
{}
0
true
========================
null
//nameless
false
false
{}
0
true
==============
{author=Lu Xun, Creative time=1918year, Title=A Madman's Diary}
3
false
================
//Lu Xun
//Lu Xun
true
false
true
=================
//author
//Lu Xun
//Creative time
1918year
//Title
//A Madman's Diary
38 original articles published, praised 6, 991 visitors
Private letter follow

Tags: Java

Posted on Mon, 09 Mar 2020 00:29:13 -0700 by TheNookie