Detail the list set map traversal and enhancement of for loops in Java
- 2020-06-03 06:40:41
- OfStack
Detail list,set,map traversal and enhancement of for loops in Java
The Java collection class can be divided into three chunks, List, Set, and Map type collections stored as key-value pairs from the Collection interface.
With respect to the enhanced for loop, it should be noted that the enhanced for loop cannot access the subscript value of the array, and it adopts the related method of Iterator for the collection traversal. Enhancing the for loop does save a lot of code if you just do simple traversal reads.
Set concept:
1. Purpose: Used to store objects
2. It is equivalent to a container containing a set of objects, each of which appears as an element of the collection
3.java's container has collection classes and arrays, the difference is
Distinguish between and its common implementation classes
List interface:
The list of ordered elements is repeatable
Implementation class :ArrayList: Dynamic array list
LinkedList: Two-way linked list
Set interface:
Sets are unordered and elements are not repeatable
Implementation class :HashSet: Hash set
TreeSet: Sort within a tree set
Map interface:
Store data as key-value pairs - keys are not allowed to duplicate
Implementation class :HashSet: Hash set
TreeSet: Sort within a tree set
The collection classes that appear in JDK1.0 are thread-safe but inefficient
The collection classes that appear in JDK1.2 are not thread-safe, but they are efficient
Code examples are as follows:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class ListAndSet{
public static void main(String[] args) {
setTest();
listTest();
}
// traverse Set A collection of
private static void setTest() {
Set<string> set = new HashSet<string>();
set.add("A");
set.add("B");
set.add("C");
set.add("D");
set.add("E");
//set Set traversal method 1: use iterator
Iterator<string> it = set.iterator();
while (it.hasNext()) {
String value = it.next();
System.out.println(value);
}
//set Set traversal method 2: Using enhanced for Cycle.
for(String s: set){
System.out.println(s);
}
}
// traverse list A collection of
private static void listTest() {
List<string> list = new ArrayList<string>();
list.add("111");
list.add("222");
list.add("333");
list.add("444");
list.add("555");
// Traverse the way 1: use iterator
Iterator<string> it = list.iterator();
while (it.hasNext()) {
String value = it.next();
System.out.println(value);
}
// Traversal methods 2: The use of traditional for Loop traversal.
for (int i = 0, size = list.size(); i < size; i++) {
String value = list.get(i);
System.out.println(value);
}
// Traversal methods 3: Using enhanced for Loop traversal.
for (String value : list) {
System.out.println(value);
}
}
}
// about Map Traversal of a collection of types, keySet() with entrySet() methods
// To enhance For cycle
public class Map{
public static void main(String[] args) {
// create 1 a HashMap object , And joined the 1 Some key value pairs.
Map<string, string=""> maps = new HashMap<string, string="">();
maps.put("111", "111");
maps.put("222", "222");
maps.put("333", "333");
maps.put("444", "444");
maps.put("555", "555");
// Traditional traversal map Method of collection 1; keySet()
//traditionalMethod1(maps);
// Traditional traversal map Method of collection 2; entrySet()
//traditionalMethod2(maps);
// Using enhanced For Loop through map Collection method 1; keySet()
//strongForMethod1(maps);
// Using enhanced For Loop through map Collection method 2; entrySet()
strongForMethod2(maps);
}
private static void strongForMethod2(Map<string, string=""> maps) {
Set<entry<string, string="">> set = maps.entrySet();
for (Entry<string, string=""> entry : set) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " : " + value);
}
}
private static void strongForMethod1(Map<string, string=""> maps) {
Set<string> set = maps.keySet();
for (String s : set) {
String key = s;
String value = maps.get(s);
System.out.println(key + " : " + value);
}
}
// use entrySet() Method, get maps Each of the sets 1 The key value pairs,
private static void traditionalMethod2(Map<string, string=""> maps) {
Set<map.entry<string, string="">> sets = maps.entrySet();
// Gets the iterator to iterate over the corresponding value.
Iterator<entry<string, string="">> it = sets.iterator();
while (it.hasNext()) {
Map.Entry<string, string=""> entry = (Entry<string, string="">) it.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " : " + value);
}
}
// use keySet() Method, get maps All the keys in the collection, traversing the keys to obtain the corresponding value.
private static void traditionalMethod1(Map<string, string=""> maps) {
Set<string> sets = maps.keySet();
// Gets the iterator to iterate over the corresponding value.
Iterator<string> it = sets.iterator();
while (it.hasNext()) {
String key = it.next();
String value = maps.get(key);
System.out.println(key + " : " + value);
}
}
}
Thank you for reading, I hope to help you, thank you for your support to this site!