Types
of collections The basic structure of
the collection framework What is the collection
framework Three major categories: sets, lists, and maps Objects in a
set
are not sorted in a specific way, and there are no duplicate
objects
. Objects in a list set are corrupted by index position, and there can be duplicate objects in
a
map set. Each element of contains a key object and a value object, the key can not be repeated, the value can be repeated
Interator <-- Collection <-- List
of collections The basic structure of
the collection framework What is the collection
framework Three major categories: sets, lists, and maps Objects in a
set
are not sorted in a specific way, and there are no duplicate
objects
. Objects in a list set are corrupted by index position, and there can be duplicate objects in
a
map set. Each element of contains a key object and a value object, the key can not be repeated, the value can be repeated
Interator <-- Collection <-- List
<-- Set
//list routine
import java.util.List;//Import List interface
import java.util.ArrayList;//Import the ArrayList class
public class Test{
public static void main(String args []){
//Generate an ArrayList object
//ArrayList<xxx> xxx represents the type of object
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("a");//Add elements
arrayList.add("b");
arrayList.add("c");
arrayList.add("d");
arrayList.remove(1);//Remove an element according to the subscript (the following elements will not come up automatically after removal)
int lenght = arrayList.size();//Get the length of the list
String s = arrayList.get(1);//Take out an element corresponding to the following table
System.out.println(s);
}
}
//collection routine
import java.util.Set;//Import the Set interface
import java.util.HashSet;//Import HashSet implementation class
import java.util.Iterator; // import Iterator interface
public class Test{
public static void main(String args []){
//
HashSet<String> hashSet = new HashSet<String>();
Set<String> = set = hashSet;//Upcast
set.add("a");//Add element
set.add("b");
set.add("c");
set.add("d");
set.add("a");//The "a" here is repeated, and the length will not change
set.remove("a");//Remove the element "a"
boolean b = set.isEmpty();//Determine whether the element is empty
int lenght = set.size();//Get the number of elements
set.clear();//Clear all elements of the collection
// Generate an iterator object, which is used to traverse the entire Set
Iterator<String> it =set.iterator();
boolean h = it.hasNext();//Return whether there is a next object
String s = it.Next();//Remove the value of the current cursor and move it back one bit
System.out.println(s);
}
}
//mapping routine
import java.util.Map;
import java.util.HashMap;
public class Test{
public static void main(String args []){
//Define a hashMap class
//Hashmap<a,b> where a represents the type of the key and b represents the type of the value
HashMap<String,String> hashMap = new HashMap<String,String>();
Map<String,String> map = hashMap;//Upcast to map
map.put("1","a");//Store key-value pairs
map.put("2","b");
map.put("3","c");
map.put("3","d");//The key value is the same as the existing one, the old value will be overwritten by the new one
String s = map.get("2");//Read the object corresponding to the key
}
By Urien April 14, 2018 14:06:47