Container (two)
Six. Iterator
Use Iterator to traverse container elements (List/Set/Map)
Iterators provide us with a unified way to traverse the container
Iterator to traverse the List
public static void testIteartorList() {
List<String> list=new ArrayList<>();
list.add("aa");
list.add("bb");
list.add("cc");
Iterator<String> iter=list.iterator();
while(iter.hasNext()) {
//看作指针移动
String temp=iter.next();
System.out.println(temp);
}
}
Iterator traverses Set
public static void testIteartorSet() {
Set<String> set=new HashSet<>();
set.add("aa");
set.add("bb");
set.add("cc");
Iterator<String> iter=set.iterator();
while(iter.hasNext()) {
//看作指针移动
String temp=iter.next();
System.out.println(temp);
}
}
Iterator traverses the Map (1)
1. In the nodes where the keys and values are stored in the Map, first obtain the set (ss) node through map.entrySet();
2. Then traverse the set
public static void testIteartorMap() {
Map<Integer,String> map=new HashMap<>();
map.put(100,"aa");
map.put(200,"bb");
map.put(300,"cc");
//获得结点
Set<Entry<Integer,String>> ss=map.entrySet(); //import java.util.Map.Entry;
Iterator<Entry<Integer, String>> iter=ss.iterator();
while(iter.hasNext()) {
Entry<Integer, String> temp=iter.next();//得到的是结点
System.out.println(temp.getKey()+"```"+temp.getValue());
}
}
Iterator traverses the Map (2)
public static void testIteartorMap() {
Map<Integer,String> map=new HashMap<>();
map.put(100,"aa");
map.put(200,"bb");
map.put(300,"cc");
//获得键的集合
Set<Integer>keySet=map.keySet(); //import java.util.Map.Entry;
Iterator<Integer> iter=keySet.iterator();
while(iter.hasNext()) {
Integer key=iter.next();
System.out.println(key+"```"+map.get(key));
}
}
Summary of methods for traversing collections
1. Traverse the List
Method one:
ordinary for loop
for(int i=0;i<list.size();i++){
//list为集合的对象名
String temp = (String)list.get(i);
System.out.println(temp);
}
Method 2:
Enhance the for loop (use generics!)
for (String temp : list) {
System.out.println(temp);
}
Method 3:
Use Iterator (1)
Iterator<String> iter=list.iterator();
while(iter.hasNext()) {
//看作指针移动
String temp=iter.next();
System.out.println(temp);
}
/同理
for(Iterator iter= list.iterator();iter.hasNext();){
String temp = (String)iter.next();
System.out.println(temp);
}
Method 4:
Use Iterator (2)
Iterator iter =list.iterator();
while(iter.hasNext()){
Object obj = iter.next();
iter.remove();//如果要遍历时,删除集合中的元素,建议使用这种方式!
System.out.println(obj);
}
2. Traverse the Set
Method one:
enhance the for loop
for(String temp:set){
System.out.println(temp);
}
Method 2:
Use Iterator
Iterator<String> iter=set.iterator();
while(iter.hasNext()) {
//看作指针移动
String temp=iter.next();
System.out.println(temp);
}
/同理
for(Iterator iter = set.iterator();iter.hasNext();){
String temp = (String)iter.next();
System.out.println(temp);
}
3. Traverse the Map
Method 1:
Get value according to key
Map<Integer, Man> maps = new HashMap<Integer, Man>();
//获得键的集合
Set<Integer>keySet=map.keySet(); //import java.util.Map.Entry;
for(Integer id : keySet){
System.out.println(maps.get(id).name);
}
/
Iterator<Integer> iter=keySet.iterator();
while(iter.hasNext()) {
Integer key=iter.next();
System.out.println(key+"```"+map.get(key));
}
}
Method two:
use entrySet
//获得结点
//import java.util.Map.Entry;
Set<Entry<Integer,String>> ss=map.entrySet();
//指结点的迭代器
Iterator<Entry<Integer, String>> iter=ss.iterator();
while(iter.hasNext()) {
Entry<Integer, String> temp=iter.next();//得到的是结点
System.out.println(temp.getKey()+"```"+temp.getValue());
}
Set<Entry<Integer, Man>> ss = maps.entrySet();
for (Iterator iterator = ss.iterator(); iterator.hasNext();) {
Entry e = (Entry) iterator.next();
System.out.println(e.getKey()+"--"+e.getValue());
}
Seven. Collections tools
The java.util.Collections class provides auxiliary methods for sorting, filling, and finding elements of Set, List, and Map.
It is an auxiliary class, not a Collection interface
Common methods of Collections tools
project | Value |
---|---|
void sort(List) | Sort the elements in the List container, the sorting rule is to sort in ascending order |
void shuffle(List) | Randomly arrange the elements in the List container |
void reverse(List) | Reverse arrangement of elements in List container |
void fill(List, Object) | Rewrite the entire List container with a specific object |
int binarySearch(List, Object) | For sequential List containers, a binary search method is used to find specific objects. |
Example
public class TestCollections {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
for(int i=0;i<10;i++) {
list.add("dj"+i);
}
System.out.println(list);
//随机排列List中的元素
Collections.shuffle(list);
System.out.println(list);
//逆序排列List
Collections.reverse(list);
System.out.println(list);
//按照递增顺序排列,自定义的的类用Comparable接口。
Collections.sort(list);
System.out.println(list);
//二分法查找(折半查找),返回索引值
System.out.println(Collections.binarySearch(list, "dj9"));
}
}
Summary
1. Collection represents a group of objects. It means to concentrate and collect, which means to collect some data.
2. Two sub-interfaces of the Collection interface:
1) The elements in the List are in order and can be repeated. Commonly used implementation classes are ArrayList, LinkedList and vector.
Ø ArrayList features: high query efficiency, low efficiency of addition and deletion, and unsafe thread.
Ø LinkedList features: low query efficiency, high efficiency of addition and deletion, and unsafe threads.
Ø vector features: thread safety, low efficiency, other features are similar to ArrayList.
2) The elements in the set have no order and cannot be repeated. Commonly used implementation classes are HashSet and TreeSet.
Ø HashSet features: It adopts hash algorithm to achieve high query efficiency and efficiency of addition and deletion.
Ø TreeSet features: The stored elements need to be sorted internally. Therefore, our corresponding class needs to implement the Comparable interface. In this way, the size between objects can be compared according to the compareTo() method, and internal sorting can be performed.
3. The class that implements the Map interface is used to store key-value pairs. The implementation classes of the Map interface include HashMap and TreeMap. The key-value pairs stored in the Map class are identified by keys, so the key values cannot be repeated.
4. The Iterator object is called an iterator, which is used to conveniently realize the traversal operation of the elements in the container.
5. The java.util.Collections class provides tools and methods to manipulate Set, List, and Map.
6. In the following situations, we may need to rewrite the equals/hashCode method:
1) Put our custom objects into HashSet for processing.
2) We need to process our customized object as the key of HashMap.
3) After putting the custom object in the Collection container, methods such as remove and contains may be called.
7. Generics have been added after JDK1.5. The benefits of generics:
1) Ensure data security when adding data to the collection.
2) No casting is required when traversing the collection elements.