Although I am doing a trial of Java Silver, I did not understand it so much, so I will review it.
So far, we've used various objects as method arguments and return values. If you want to pass multiple objects together instead of a single object, you can use an array. However, it is necessary to determine the number of data (objects) to be handled in the array in advance. Also, you cannot increase or decrease the size of the array later. Therefore, a collection framework is provided as a unified idea for handling multiple objects at once. Objects provided under the collection framework are called collections. Along with the collection framework, different types of classes and interfaces are provided for different purposes, allowing for consistent management and manipulation (that is, storing, retrieving, and deleting objects in a collection). The objects stored in the collection are called elements or elements.
Located in the root hierarchy of the collection is the java.util.Collection interface, which is the super interface of the collection. There are other types of collections called maps, but these super interfaces are Maps and are not derived from the Collection interface. The figure below summarizes the relationship between these two types of interfaces.
Even though it is a collection, the requirements for it vary from application to application, so some collections are allowed to have duplicate elements, while others are not. .. Also, some collections are ordered, while others are not. The Collection interface only negotiates the basic functions of the collection framework, and the List, Set, and Queue interfaces that inherit from the Collection interface have their own functions added according to their requirements. This time, we will review how to use the class that implements the List, Set, and Map interfaces.
If you want to allow duplicate elements and order them, use the implementation class of List interface. This time, the implementation classes of the List interface are collectively called a list.
The list is like a ** resizable array **. In the case of an array, the number of elements that can be held is fixed, but the list can freely add or remove elements. Like arrays, you use subscripts to manage elements in order. The elements stored in the list can be duplicated. ArrayList is a class that implements the List interface. Not limited to the ArrayList class, each collection-related class and interface is included in the java.util package, so import this package when using it. When creating an ArrayList object, specify the data type of the element held by this object in ** <> **. The data to be stored can be any reference type. Details of <> will be described later. Use the ** add () ** method to store the element and the ** get () ** method to get it. The following is an example using the ArrayList class.
1¥Main.java
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>(); //Instantiate an ArrayList class that can only store Integer objects
Integer i1 = 10;
int i2 = 2;
Integer i3 = i1;
list.add(i1); // add()Store Integer object using method
list.add(i2); //int data is automatically converted to Integer type by Boxing
list.add(i3); //i1 and i3 have the same reference (that is, the same object) and duplicate elements
list.add(1, 5); //Add 5 to the first index
// list.add("abc"); //Compile error when uncommenting because you are trying to assign a character string (String type)
System.out.println("size : " + list.size()); //The number of elements stored in the ArrayList object is size()Use method
for (int i=0; i<list.size(); i++) {
System.out.println(list.get(i) + " "); //Get to retrieve data()Use method
}
System.out.println();
for (Integer i : list) { System.out.print(i + " "); } //Extract using extended for statement
}
}
Execution result
size : 4
10 5 2 10
10 5 2 10
If you do not want to allow duplicate elements in the data structure, use the implementation class of the Set interface. This time, the objects of the class group that implements the Set interface are collectively called a set.
A set is like storing elements in a bag. In other words, each stored element is an image that fits in the bag separately, so it is managed in no particular order without subscripts. Unlike lists, sets can only store unique elements. HashSet is a class that implements the Set interface. The following example uses the HashSet class.
2¥Main.java
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
String[] ary = {"CCC", "AAA", "BBB"};
HashSet<String> hashSet = new HashSet<String>();
hashSet.add(ary[0]);
hashSet.add(ary[1]);
hashSet.add(ary[2]);
hashSet.add(ary[0]); //Since we are trying to add an element that has already been stored, it is considered to be an equivalent element and the element is not stored.
System.out.println("HashSet size : " + hashSet.size());
for (String s : hashSet) { System.out.print(s + " "); }
}
}
Execution result
HashSet size : 3
AAA CCC BBB //The order when the elements are taken out is not the stored net
If you want to manage data as a key / value pair, use the implementation class of Map interface. This time, the objects of the class group implemented by the Map interface are collectively called a map.
A map is like an employee number and employee name, and holds a unique key and a value (object) for it as a pair. The key must be unique so that it can be identified, but the value corresponding to the key can be duplicated. HashMap is a class that implements the Map interface. The following is an example using the HashMap class.
3¥Main.java
import java.util.*;
public class Main {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<Integer, String>(); //Instantiate HashMap that handles Integer type key and String type value
map.put(0, "AAA");
map.put(1, "BBB");
map.put(2, "AAA"); //Stored as individual pairs due to different keys
map.put(1, "CCC"); //The value is overwritten because the key is duplicated
for (int i=0; i<map.size(); i++) {
System.out.println(map.get(i) + " "); //Get with key as argument()Use method to retrieve value
}
System.out.println();
Set<Integer> keys = map.keySet(); //KeySet to get a set of keys()Use method
for (Integer key : keys) {
System.out.print(key + " ");
}
System.out.println();
Collection<String> values = map.values(); //Values to get a set (collection) of values()Use method
for (String value : values) {
System.out.print(value + " ");
}
}
}
Execution result
AAA CCC AAA
0 1 2
AAA CCC AAA
Recommended Posts