Day 17 of learning java - Java collection framework

Java collection framework

Java collection overview
Front storage objects, arrays, disadvantages
A Java collection is like a container in which you can put references to multiple objects.
Java collection classes can be used to store multiple objects with different numbers, and can also be used to store associative arrays with mapping relationships

Java sets can be divided into three systems: Set, List and Map
Set: an unordered and unrepeatable set, which is implemented with map as the underlying layer
List: an ordered and repeatable collection. The list interface is implemented with arrays as the underlying layer, which is ordered
Map: set with mapping relationship, key value (key value pair), the bottom layer of map interface is hash function, non continuous (not random)
Before Java 5, Java collection will lose the data types of all objects in the container and treat all objects as Object types. Since Java 5 added generics, Java collection can remember the data types of objects in the container

Collection interface
The Collection interface is the parent interface of the List, Set, and Queue interfaces. The methods defined in this interface can be used to operate both the Set Collection and the List Collection

Traversing collection elements using the Iterator interface
The Iterator interface is mainly used to traverse the elements in the Collection, and the Iterator object is also called an Iterator.
The Iterator interface hides the underlying details of various Collection implementation classes and provides a unified programming interface for applications to traverse Collection collection elements.
Iterator is only used to traverse a collection, and it does not provide the ability to store objects. If you need to create an iterator object, you must have an iterated collection.

Traversing set elements with foreach loop
Starting from Java 5, JDK provides foreach loop iterative access to Collection

List represents an ordered and repeatable collection of elements, and each element in the collection has its corresponding order index
List allows duplicate elements, which can be indexed to access collection elements at a specified location.
List sets the index of elements by default in the order in which they are added.
The List collection adds some methods to operate the collection elements according to the index
void add(int index, Object ele)
boolean addAll(int index, Collection eles)
Object get(int index)
int indexOf(Object obj)
int lastIndexOf(Object obj)
Object remove(int index)
Object set(int index, Object ele)
List subList(int fromIndex, int toIndex), toIndex cannot be retrieved
One of the List implementation classes: ArrayList
ArrayList is a typical implementation class of List interface
In essence, ArrayList is a variable length array of object references

One of the List implementation classes: LinkedList
The LinkedList class uses the linked list storage method. The efficiency of inserting and deleting elements is relatively high, as shown in the following figure

When to use ArrayList and LinkedList respectively

Common methods of ArrayList

Set set
The Set interface stores a Set of unique, unordered objects.
HashSet is a common implementation class of Set interface.
HashSet allows collection element values to be null
The method to manipulate data is similar to List. There is no get() method in Set interface.
The Iterator interface represents the Iterator that iterates over a collection, specifically to traverse the collection.
hasNext(): determine whether there is another accessible element
next(): returns the next element to be accessed
Case analysis

// Output the information of all elements in the collection in turn through the iterator
		System.out.println("Use Iterator Traversal, employee names are:");
		Iterator it = set.iterator();
		while (it.hasNext()) {
			Employee emp = (Employee);
			String name = emp.getName();
	**One of the Set implementation classes: HashSet**

HashSet is a typical implementation of Set interface, which is used most of the time.
HashSet stores the elements in the set according to the Hash algorithm, so it has good access and search performance.
HashSet has the following characteristics:
The order of elements is not guaranteed
HashSet is not thread safe
Collection element can be null
When an element is stored in the HashSet collection, the HashSet will call the hashCode() method of the object to get the hashCode value of the object, and then determine the storage location of the object in the HashSet according to the hashCode value.
The HashSet set sets the criteria for determining the equality of two elements: two objects are equal through the hashCode() method, and the return values of the equals() method of the two objects are equal.
Map exists side by side with Collection. Used to save data with mapping relationship: key value
key and value in Map can be data of any reference type
The key in a Map is stored in a Set. It is not allowed to be duplicate. That is, the class corresponding to the same Map object must override the hashCode() and equals() methods.
The String class is often used as the "key" of Map.
There is a one-way one-to-one relationship between key and value, that is, the unique and definite value can always be found through the specified key.
Map interface

Common methods of Map
Add / delete:

Object put(Object key,Object value)
Object remove(Object key)
void putAll(Map t)
void clear()

How to operate the meta view:

Set keySet()
Collection values()
Set entrySet()

Operation of element query:

Object get(Object key)
boolean containsKey(Object key)
boolean containsValue(Object value)
int size()
boolean isEmpty()
boolean equals(Object obj)

One of the Map implementation classes: HashMap
Common implementation classes of Map interface: HashMap, TreeMap and Properties.
HashMap is the most frequently used implementation class of Map interface.
Null key and null value are allowed. Like HashSet, the order of mapping is not guaranteed.
The criteria for determining the equality of two keys by HashMap is that the two keys return true through the equals() method, and the hashCode value is equal.
HashMap judges that two values are equal: two values return true through the equals() method.
Map implementation class 2: Hashtable
Hashtable is an old Map implementation class, thread safe.
Unlike HashMap, Hashtable does not allow null as key and value
Like HashMap, Hashtable cannot guarantee the order of key value pairs
The criteria for Hashtable to judge that two key s are equal and two value s are equal are consistent with hashMap.
Map implementation class 3: Properties
The Properties class is a subclass of Hashtable, which is used to process property files
Because the key and value in the property file are string types, the key and value in Properties are string types
When accessing data, it is recommended to use setProperty(String key,String value) method and getProperty(String key) method

Properties pros = new Properties();
pros.load(new FileInputStream(""));
String user = pros.getProperty("user");

Lesson cases


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

import org.junit.Test;

 * 1.Store multiple objects: array, collection
 * 2.The characteristics of array objects: Student[] stu=new Student[10];
 *                Disadvantages: once created, the length is immutable
 * 3.Collection is divided into two systems: collection and map
 *                 collection Interface
 *                        ---List Interface
 *                           --Orderly, repeatable
 *                           ArrayList(Main implementation class): fast query speed
 *                           LinkedList:Fast addition, deletion and modification
 *                        ---Set Interface
 *                           --Out of order, not repeatable
 *                           hashSet

public class TestCollection {
	public void test(){
		//A set is defined
		Collection coll=new ArrayList();
		//size(): get set length common
		//add(object o): commonly used for adding elements
		coll.add("Hum ha");
		coll.add(new Date());
		//Add all (collection c): add the elements in collection c to this collection
		Collection c=Arrays.asList(1,6,9,78);
		//isEmpty(): judge whether the current collection is empty. It is usually empty. It is true. It is not empty. It is false
		//clear(): commonly used to empty elements in a collection
//		coll.clear();
//		System.out.println(coll.size());
		//contains(object o): determines whether the current collection contains element o
		//remove(object o): remove the common elements o in the current collection
		//toArrary(): commonly used to convert a collection into an array
		Object[] o=coll.toArray();
		for(Object o1:o){
	public void test1(){
		//A set is defined
		Collection coll=new ArrayList();
		coll.add("Hum ha");
		coll.add(new Date());
		//The first way
		for(Object o:coll){
		//The second way:
		//iterator(): iterator: returns an iterator interface
	     Iterator i=coll.iterator();
	     while(i.hasNext()){//Determine whether there is the next element in the current collection, and return true if there is one
	    	 System.out.println(;//Point to next element




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

import org.junit.Test;

public class TestList {
	//arrayList is the main implementation class of list
//  Void add (object o): add element
//	void add(int index, Object ele): add the element to the location according to the specified index value
//	boolean addAll(int index, Collection eles): 
//	Object get(int index): query element value according to index value
//	int indexOf(Object obj): returns the first occurrence of the element obj in the current collection, if - 1 is not returned
//	int lastIndexOf(Object obj): returns the location of the last occurrence of the element obj in the current collection, if - 1 is not returned
//	Object remove(int index): delete the specified element according to the index value
//	Object set(int index, Object ele): set the element value of the specified index location to ele
	//; list common methods:
	//Add delete modify query size()

	public void test(){
		List list=new ArrayList();
		//Additive elements
		list.set(0, 345);
	public void test1(){
		List list=new ArrayList();
		//Additive elements
		//for cycle
		for(int i=0;i<list.size();i++){
		for(Object o:list){
	    Iterator i=list.iterator();


Published 26 original articles, won praise 1, visited 266
Private letter follow

Keywords: Java Junit Programming JDK

Added by thepip3r on Wed, 15 Jan 2020 15:01:36 +0200