“This is the 12th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
Java collection
Most real-world applications deal with collections like files, variables, records from files, or database result sets.
The most common type of collection is arrays, which we’ve discussed separately before, but now we’ll focus on the other collection types.
The list ofList
A List is an ordered set, also known as a sequence.
The List collection can only contain objects (not primitive types such as ints).
To use List, we need to import it into our program:
import java.util.List;
Copy the code
List is an interface, so it cannot be instantiated directly (that is, new List
!). To declare a List, use the following syntax:
List<String> listOfStrings = new ArrayList<String>();
Copy the code
or
List<String> listOfStrings = new ArrayList<>();
Copy the code
So we declare a fairly common kind of List, ArrayList.
【 note 】
- To use the code above, first:
import java.util.List; import java.util.ArrayList; Copy the code
- We will be
ArrayList
Object assigned to aList
Type variable. In Java programming, it is possible to assign a variable of one type to another as long as the assigned variable is the superclass or interface that the assigned variable implements.
Formal type
The types in Angle brackets (<>) in the preceding code snippet are known as formal types, meaning what kind of collection this List is.
As in the previous example, where the formal type is String, this List can only contain instances of String.
If you write the formal type as
Use the list
- Put entities in
List
中add(E element)
Method to add the element Element toList
At the end of.add(int index, E e
Method to add the element Element toList
The index of the forindex
(index <= List.size()
).
- ask
List
How big is it now?- Want to ask
List
How big, callablesize()
- Want to ask
- from
List
Get entity from- From you to
List
中retrieveAn item that can be calledget()
And pass it the index of the desired item
- From you to
- from
List
Delete entity from- From you to
List
中deleteAn item that can be calledremove()
And pass it the index of the desired item
- From you to
Logger l = Logger.getLogger("Test")
/ / statement List
List<Integer> listOfIntegers = new ArrayList<>();
// Insert data
listOfIntegers.add(Integer.valueOf(238));
listOfIntegers.add(0, Integer.valueOf(987));
// Check the size
l.info("Current List size: " + listOfIntegers.size());
// Get the element
l.info("Item at index 0 is: " listOfIntegers.get(0));
// Delete elements
listOfIntegers.remove(0);
Copy the code
Related code: usrlist.java
For more information on List, see the JDK documentation.
Iteration variable可迭代
If a collection implements java.lang.Iterable, it is said to be a collection of iterated variables.
Iterable can start at one end and process the collection item by item until all items are processed.
It is used in the form of the for-echo loop mentioned in the loop:
for (objectType varName : collectionReference) {
// Start using objectType (via varName) right away...
}
Copy the code
Because List extends java.util.Collection (which implements Iterable), you can use the shorthand syntax to iterate over any List:
List<Integer> listOfIntegers = obtainSomehow();
Logger l = Logger.getLogger("Test");
for (Integer i : listOfIntegers) {
l.info("Integer value is : " + i);
}
Copy the code
This code implements the same effect as the following for loop:
List<Integer> listOfIntegers = obtainSomehow();
Logger l = Logger.getLogger("Test");
for (int aa = 0; aa < listOfIntegers.size(); aa++) {
Integer I = listOfIntegers.get(aa);
l.info("Integer value is : " + i);
}
Copy the code
setSet
A Set is a collection structure with no repeating elements. It guarantees uniqueness in its elements, but does not care about the order of the elements.
A List can contain the same object hundreds of times, whereas a Set can contain a particular instance only once.
A Set can also contain only objects, not built-in types directly.
As with lists, sets cannot be instantiated directly (new Set
()), but can be implemented with hashSets, etc., inherited from sets.
operationSet
- increase
add(E e)
- delete
remove(Object o)
- Get the size
size()
- Access to the entity
- Used in “iteration variable (
可迭代
The for-each loop introduced in theSet
Take it out. But note that objects may be printed out in a different order than they were added in.
- Used in “iteration variable (
import java.util.Set;
import java.util.HashSet;
import java.util.logging.Logger;
public class TrySet {
public static void main(String[] arg) {
Logger l = Logger.getLogger("Tset");
Set<Integer> setOfIntegers = new HashSet<>();
setOfIntegers.add(Integer.valueOf(12));
setOfIntegers.add(Integer.valueOf(20));
setOfIntegers.add(Integer.valueOf(5));
setOfIntegers.add(Integer.valueOf(5));
l.info(setOfIntegers.toString());
setOfIntegers.remove(Integer.valueOf(20));
l.info("" + setOfIntegers.size());
for (Integer i : setOfIntegers) {
i += 100; System.out.println(i); } l.info(setOfIntegers.toString()); }}Copy the code
Output result:
. TrySet main info: [20, 5, 12]... TrySet main info: 2 105 112... TrySet Main info: [5, 12]Copy the code
mappingMap
A Map is a convenient collection construct that you can use to associate one object (key) with another object (value).
The Map’s keys must be unique and can be used to retrieve values later.
Like List and Set, a Map collection can contain only objects, and a Map is an interface, so it cannot be instantiated directly. We use HashMap to implement a Map.
Common operations
- Add elements:
put(K key, V value)
- Fetching elements:
get(Object key)
- Delete element:
remove (Object key, [Object value])
- Get size:
size()
- To get a Key
Set
:keySet()
, you can use this method to achieve traversal using a combination of Set and MapMap
.
/ / declare
Map<String, Integer> mapOfIntegers = new HashMap<>();
/ / add
mapOfIntegers.put("168", Integer.valueOf(168));
/ / value
Integer oneHundred68 = mapOfIntegers.get("168");
Copy the code
Set<String> keys = mapOfIntegers.keySet();
Logger l = Logger.getLogger("Test");
for (String key : keys) {
Integer value = mapOfIntegers.get(key);
l.info("Value keyed by '" + key + "' is '" + value + "'");
}
Copy the code
Related code: trymap.java
GitHub:
TryList.java
TrySet.java
TryMap.java