Java SE Chapter 7 Generics and Collections-Collection Classes-List Interface, Set Interface, Map Interface

1. List interface
Description:
1) This interface inherits the Collection interface
2) The elements are allowed to be repeated
3) The elements are placed in the order in which the elements are added, and will not be rearranged
4)
The specific implementation class of the List interface that supports the position index random operation The commonly used classes are ArrayList And LinkList.
1. ArrayList supports dynamic arrays that can be adjusted as needed.
It encapsulates a dynamically redistributable Object[] array.
Each ArrayList object has a capacity, which represents the capacity of the array storing the elements in the list.
Common methods:
ArrayList()
Array List(Collection<? extends E>c)
ArrayList(int capacity)
void ensureCapacity(int minCapacity)
void trimToSize()
Example: Demonstrate common operations of generic ArrayList based on String type, including add, delete, Modify and traverse.

package ch07;

import java.util.*;

public class ArrayListDemo {
    
    
	public static ArrayList <String>arrayList;
	//初始化链表
	public static void init(){
    
    
		arrayList=new ArrayList<String>(4);
		System.out.println("初始化长度:"+arrayList.size());
		arrayList.add("First");
		arrayList.add("second");
		arrayList.add("third");
		arrayList.add("Forth");
	}
	//打印链表信息
	public static void printInfo(){
    
    
		System.out.println("增加元素后的长度:"+arrayList.size());
		ArrayList <String> arrayList2=new ArrayList<String>(arrayList);
		System.out.println("arrayList:"+arrayList);
		System.out.println("arrayList2"+arrayList2);
	}
	//对链表实现修改、删除操作
	public static void modify(){
    
    
		arrayList.add(1,"insert data");
		System.out.println("增加元素后的长度:"+arrayList.size());
		arrayList.remove("second");
		System.out.println("删除‘second’后的长度:"+arrayList.size());
		arrayList.remove(2);
		System.out.println("删除第3个元素后的长度:"+arrayList.size());
		arrayList.remove("nothing");
		System.out.println("删除‘nothing’后的长度:"+arrayList.size());
	}
	public static void toArray(){
    
    
		Object[]arr=arrayList.toArray();
		for(int i=0;i<arr.length;i++){
    
    
			String str=(String)arr[i];
			System.out.println((i+1)+":"+str);
		}
	}
	public static void main(String[] args) {
    
    
		init();
		printInfo();
		modify();
		toArray();
	}

}

Insert picture description here
2. List of commonly used methods of LinkList
LinkedList()
LinkedList(Collection<? extends E>c)
void addFirst(E obj)
E getFirst()
E removeFirst()
void addLast((E ob)
E getLast()
E removeLast()
3. List traversal
Example: Demonstrate the function of using Iterator to traverse a collection.

public static void travel(){
    
    
		System.out.println("遍历前的长度:"+arrayList.size());
		Iterator<String>iterator=arrayList.iterator();
		int i=0;
		while(iterator.hasNext()){
    
    
			String str=iterator.next();
			i++;
			System.out.println(str);
			if(i%3==0){
    
    
				iterator.remove();
			}
		}
		System.out.println("删除后的长度:"+arrayList.size());

Insert picture description here
4.for-each

for(string str:arrayList){
    
    
System.out.println(str);
}

2. Set interface
Description:
1) This interface inherits the Collection interface
2) It is allowed to repeat
3) No new methods are introduced, so Set is a Collection, but its behavior is different.
Specific implementation classes: HashSet, TreeSet
1, HashSet (hash)
can be fast To locate an element, you generally need to override the hashCode() method.
List of construction methods:
HashSet()
HashSet(Collection<? extends E>c)
HashSet(int capacity)
HashSet(int capacity, float fill)
Example: Demonstrate the use of generic HashSet based on the String type

public static void main(String[] args) {
    
    
		HashSet<String>hashSet=new HashSet<String>();
		hashSet.add("first");
		hashSet.add("second");
		hashSet.add("third");
		hashSet.add("forth");
		System.out.println(hashSet);
		for(String str:hashSet){
    
    
			System.out.println(str);
		}

Insert picture description here
2. The TreeSet (tree structure)
objects are stored in ascending order, and the access and retrieval speed is fast.
List of construction methods:
TreeSet()
TreeSet(Collection<? extends E>c)
TreeSet(Comparator<? super E>comp)
TreeSet(SortedSetsortSet)
example: demonstrate the use of generic TreeSet based on the String type

public static void main(String[] args) {
    
    
		TreeSet<String>treeSet=new TreeSet<String>();
		treeSet.add("first");
		treeSet.add("second");
		treeSet.add("third");
		treeSet.add("forth");
		System.out.println(treeSet);
		for(String str:treeSet)
		{
    
    
			System.out.println(str);
		}
	}

Insert picture description here
Analyze from the running results: TreeSet elements are sorted and stored in string order. This requires the elements placed in it to be sortable. The collection framework provides two practical interfaces for sorting: Comparable and Comparator. A sortable class should implement the Comparable interface (usually, when the sorting basis is not specified, objects added to the TreeSet must implement Comparable).
3.
Description of Map interface (key-value pair) :
1) Repeating is not allowed.
2) The entrySet() method of Map returns a collection of objects that implement the Map.Entry interface, making it possible to manipulate Map items ("key/value") individually.
3) Map.Entry method list:
K getKey()
V getValue()
int hashCode()
boolean equals(Object obj)
V setValue(V v)
Two more commonly used implementations: HashMap, TreeMap
1, HashMap class (hash table )
a. The order in which elements are added to the hash map is not necessarily the order in which they are read by the iterative method.
b. Null values ​​and null keys are allowed.
Example: Demonstrate the use of generic HashMap based on String and Integer types.

public static void main(String[] args) {
    
    
		HashMap<String,Integer>hashMap=new HashMap<String,Integer>();
		hashMap.put("Tom", new Integer(23));
		hashMap.put("Rose", new Integer(18));
		hashMap.put("Jane", new Integer(26));
		hashMap.put("Black", new Integer(24));
		hashMap.put("Smith", new Integer(21));
		Set<Map.Entry<String,Integer>>set=hashMap.entrySet();
		for(Map.Entry<String, Integer>entry:set){
    
    
			System.out.println(entry.getKey()+":"+entry.getValue());
		}
		System.out.println("---------------");
		Set<String>KeySet=hashMap.keySet();
		StringBuffer buffer=new StringBuffer("");
		for(String str:KeySet){
    
    
			buffer.append(str+',');
		}
		String str=buffer.substring(0,buffer.length()-1);
		System.out.println(str);
	}

Insert picture description here
2. TreeMap
1) Quick search
2) The elements are arranged in ascending order
Construction method:
TreeMap()
TreeMap(Comparator<? super K>comp)
TreeMap(Map<?extends K,?extends V>m)
TreeMap(SortedMap<K,? extends V>m)
Example: Demonstrate the use of generic TreeMap based on String and Integer.

public static void main(String[] args) {
    
    
		TreeMap<String,Integer>treeMap=new TreeMap<String,Integer>();
		treeMap.put("Tom", new Integer(23));
		treeMap.put("Rose", new Integer(18));
		treeMap.put("Jane", new Integer(26));
		treeMap.put("Black", new Integer(24));
		treeMap.put("Smith", new Integer(21));
		Set<Map.Entry<String,Integer>>set=treeMap.entrySet();
		for(Entry<String,Integer>entry:set){
    
    
			System.out.println(entry.getKey()+":"+entry.getValue());
		}
		System.out.println("-----------");
		Set<String>KeySet=treeMap.keySet();
		StringBuffer buffer=new StringBuffer("");
		for(String str:KeySet)
		{
    
    
			buffer.append(str+",");
		}
		String str=buffer.substring(0,buffer.length()-1);
		System.out.println(str);
	}

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45618376/article/details/111406632