Summary of List, Set, Map in the collection

Collections: 
In programming, we need to manage many object sets, such as all classmate information of a certain class, personnel information of a certain company, and so on. JAVA collection is to provide support for a certain data structure, that is, to store objects, and to store these objects according to a certain data structure. 
The difference between a collection and an array: 
1. The number of elements stored in an array is fixed when the array is defined. 
The collection can add and delete elements through methods. 
2. The type of the array is unified. 
The types stored in the collection may not be uniform.

Collection is the parent interface of List and set. It encapsulates the common methods of sub-interfaces List and Set. 
The two most commonly used subclasses in List: ArrayList and LinkedList 
The two most commonly used subclasses in Set: HashSet and TreeSet 
The two most commonly used subclasses in Map Classes: HashMap and TreeMap

Common methods in List: 
size(): View the size of the collection, that is, the length 
add(): Add data to the collection 
get(): Get the data in the collection by subscript 
set(): Modify the data in the collection by subscript 
remove (): Modify the data in the set by subscript or value. If the parameter is a value, only delete the first data with this value 
clear(): Clear all data in the set 
contains(): Determine whether the set contains a certain value data

List traversal method: 
for loop:

for(int i = 0 ; i < list.size() ; i ++){
    System.out.println(list.get(i));
}
  • 1
  • 2
  • 3

foreach loop

for(Object o : list){
    System.out.println(o);
}
  • 1
  • 2
  • 3

iterator iterator loop

Iterator iter = list.iterator();
while(iter.hasNext()){
    System.out.println(iter.next());
}
  • 1
  • 2
  • 3
  • 4

One thing to note when using iterator loops: if objects are stored in the collection, the iter.next() statement cannot appear more than twice in the loop. Therefore, you need to define an object to receive the value from the loop, and then use the object to call the property To print, the code is as follows:

Iterator iter = list.iterator();
while(tier.hasNext()){
    Student stu = tier.next();
    Sytem.out.println("学生姓名:" + stu.name + "学生年龄:" + stu.age);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324778610&siteId=291194637