Hello. I'm Wataku, a server-side programmer who is studying programming at a certain school. : relaxed:
This time I tried to summarize the collection framework in Java. Since it was a complicated field for me personally, I am also summarizing it for studying, so if you have any suggestions such as mistakes, please comment.
Let's go.
In the first place, you can think of a collection as something that can manipulate a large number of values at once like an array, although the size is not fixed.
Since the size is not fixed, you can store as many data as you need.
There are three main types in the collection, depending on the application, as shown below.
-** List ** Stored in order -** Set ** Stored not necessarily in order -** Map ** Stored in pairs
The ** Collection Framework ** is designed to be handled flexibly with such types.
As the name "Array" suggests, it can be handled like an array.
By the way, there are other classes such as ** LinkedList ** and ** Stack ** in the class that implements List, but I will not introduce them here. I would like to write if there is another opportunity.
ArrayList<Mold>Variable name= new ArrayList<Mold>();
At the end of the class, there is a part that specifies the type surrounded by ** "<" ** and ** ">" **. Although it is an unfamiliar way of writing, it is called ** Generics function ** newly introduced from J2SE 5.0, and specifies what type of value is stored in this ArrayList. Generics Specify the class name for this type part. And the value that can be stored as an element will be the object of the class specified here. Please note that String is okay, but int and float cannot be specified. This is because String is a String class, but ints and floats are not classes but basic types.
Therefore, if you want to handle basic types such as int type and float type, class ** (wrapper class) ** corresponding to each basic type is prepared, so use it.
Corresponding class | Basic type |
---|---|
Boolean | boolean |
Character | char |
Byte | byte |
Short | short |
Integer | int |
Long | long |
Float | float |
Doubule | double |
For example, to create an ArrayList that stores integers or an ArrayList that stores Strings, it will be as follows.
ArrayList<Integer> array = new ArrayList<Integer>();
ArrayList<String> array = new ArrayList<String>();
--Insert data
add(value)
** add (index, value) ** allows you to insert a value at the specified index. --Data retrieval
get(index)
--Getting the number of elements (length)
size()
--Whether it is empty
isEmpty()
--Whether the value is included
contains(value)
--Which index the value is in
indexOf(value)
--Delete specified index
remove(index)
Set
--Manage data with duplicates eliminated. --I can't ** get () ** because I don't manage the value by index → Loop to see the data --The interface ** SortedSet ** sorts the contents of the set. Its implementation class is ** TreeSet **
Use the ** Italator ** interface.
<Main methods>
・ Next():Get the following value
・ HasNext():Whether there is the following value
for(Iterator ite = stock.itarator; ite.hasNext;) {
//ite.Play with next
}
①List
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class UseList {
public static void main(String[] args) {
//TODO auto-generated method stub
List<Integer> store = new ArrayList<>();
System.out.println("Before registration: The total number of elements" + store.size() + "This is it.");
System.out.println("Is the list currently empty?" + store.isEmpty());
store.add(54);
store.add(24);
store.add(47);
store.add(0);
store.add(6);
store.add(24);
store.add(85);
store.add(1);
store.add(5);
store.add(6);
store.add(98);
store.add(47);
store.add(54);
store.add(96);
store.add(9);
System.out.println("Before registration: The total number of elements" + store.size() + "This is it.");
System.out.println("Is the list currently empty?" + store.isEmpty());
System.out.println("The value of index 8 is" + store.get(8) + "is.");
System.out.println("Is the value 1 included?" + store.contains(1));
System.out.println("What is the index of value 1?" + store.indexOf(1));
System.out.println("-------------Let's take a look at the contents--------------");
for(Iterator<Integer> ite = store.iterator(); ite.hasNext();) {
Integer element = ite.next();
System.out.println(element);
}
System.out.println("Notice that some values are duplicated");
store.remove(8);
System.out.println("After deletion: All elements" + store.size() + "This is it.");
System.out.println("-------------Let's take a look at the contents again--------------");
for(Integer element : store) {
System.out.println(element);
}
store.add(12, 346);
System.out.println("After registration: All elements" + store.size() + "This is it.");
System.out.println("-------------Let's take a look at the contents again--------------");
for(Integer element : store) {
System.out.println(element);
}
}
}
result
Before registration: The total number of elements is 0.
Is the list currently empty? true
Before registration: The total number of elements is 15.
Is the list currently empty? false false
The value of index 8 is 5.
Is the value 1 included? true
What is the index of value 1? 7
-------------Let's take a look at the contents--------------
54
24
47
0
6
24
85
1
5
6
98
47
54
96
9
Notice that some values are duplicated
After deletion: There are 14 elements in total.
-------------Let's take a look at the contents again--------------
54
24
47
0
6
24
85
1
6
98
47
54
96
9
After registration: There are 15 elements in total.
-------------Let's take a look at the contents again--------------
54
24
47
0
6
24
85
1
6
98
47
54
346
96
9
②Set
import java.util.HashSet;
import java.util.Set;
public class UseSet {
public static void main(String[] args) {
//TODO auto-generated method stub
Set<Integer> store = new HashSet<>();
store.add(54);
store.add(24);
store.add(47);
store.add(0);
store.add(6);
store.add(24);
store.add(85);
store.add(1);
store.add(5);
store.add(6);
store.add(98);
store.add(47);
store.add(54);
store.add(96);
store.add(9);
System.out.println("The number of elements is" + store.size() + "This is it.");
System.out.println("-------------Let's take a look at the contents--------------");
for(Integer element : store) {
System.out.println(element);
}
}
}
result
The number of elements is 11.
-------------Let's take a look at the contents--------------
0
96
1
98
85
5
54
6
24
9
47
Map
Map<string, Integer> maps new HashMap<>();
--Data registration
put(Key,value)
--Is it included in the key?
containsKey(Key)
--Is it included in the value?
containsValue(value)
--Delete specified key
remove(Key)
(Note) Since Map cannot be looped as it is, loop the Set object cut out using "entitySet".
Set entity = maps.entitySet();
for(Iterator ite = entity.itarator; ite.hasNext;) {
//ite.Play with next
}
-** HashMap : When you want to manage values with Map for the time being - TreeMap : When you want to sort by key - LinkedHashMap **: When you want to keep the put order
Map + Entity In Java, create an entity class and use the Map key as the DB primary key. * O-R mapping * can be done by storing the "new" entity in the value.
①
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class UseMap {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
System.out.println("The current number of elements is" + map.size() + "This is");
System.out.println("Is the Map empty?" + map.isEmpty());
map.put(1, "Tanibe");
map.put(3, "Kuroi");
map.put(5, "Kaneko");
map.put(7, "Inoue");
map.put(11, "Yamamoto");
map.put(13, "Nakamura");
map.put(15, "With morning");
map.put(17, "Nakajima");
map.put(19, "Takeda");
map.put(21, "Kimoto");
map.put(25, "Saito");
map.put(27, "Asuka");
map.put(29, "Hoshino");
System.out.println("The current number of elements is" + map.size() + "This is");
System.out.println("Is the Map empty?" + map.isEmpty());
System.out.println("--------------------Let's take a look at the contents(Iterator version)------------------------");
Set<Map.Entry<Integer, String>> entries = map.entrySet();
for(Iterator<Map.Entry<Integer, String>> ite = entries.iterator(); ite.hasNext();) {
Map.Entry<Integer, String> element = ite.next();
Integer key = element.getKey();
String value = element.getValue();
System.out.println("key=" +key + ":value=" + value);
}
System.out.println("--------------------Let's take a look at the contents(Convenient version)------------------------");
for(Map.Entry<Integer, String> element : map.entrySet()) {
Integer key = element.getKey();
String value = element.getValue();
System.out.println("key=" +key + ":value=" + value);
}
System.out.println("I" + map.get(25) + "is");
System.out.println("Do you have No22?" + map.containsKey(22));
System.out.println("Do you have Mr. Tanaka? :" + map.containsValue("Tanaka"));
String name = map.put(9, "Nakayama");
System.out.println(name + "Overwritten");
for(Map.Entry<Integer, String> element : map.entrySet()) {
System.out.println(element);
}
name = map.remove(25);
System.out.println(name + "Was killed");
for(Map.Entry<Integer, String> element : map.entrySet()) {
System.out.println(element);
}
Set<Integer> keySet = map.keySet();
for(Integer integer : keySet) {
System.out.println(integer);
}
Collection<String> values = map.values();
for(String eachName : values) {
System.out.println(eachName);
}
}
}
result
The current number of elements is 0
Is the Map empty? true
The current number of elements is 13
Is the Map empty? false false
--------------------Let's take a look at the contents(Iterator version)------------------------
key=1:value=Tanibe
key=3:value=Kuroi
key=5:value=Kaneko
key=7:value=Inoue
key=11:value=Yamamoto
key=13:value=Nakamura
key=15:value=With morning
key=17:value=Nakajima
key=19:value=Takeda
key=21:value=Kimoto
key=25:value=Saito
key=27:value=Asuka
key=29:value=Hoshino
--------------------Let's take a look at the contents(Convenient version)------------------------
key=1:value=Tanibe
key=3:value=Kuroi
key=5:value=Kaneko
key=7:value=Inoue
key=11:value=Yamamoto
key=13:value=Nakamura
key=15:value=With morning
key=17:value=Nakajima
key=19:value=Takeda
key=21:value=Kimoto
key=25:value=Saito
key=27:value=Asuka
key=29:value=Hoshino
I'm Saito
Do you have No22? false false
Do you have Mr. Tanaka? : False
Overwrote null
1=Tanibe
3=Kuroi
5=Kaneko
7=Inoue
9=Nakayama
11=Yamamoto
13=Nakamura
15=With morning
17=Nakajima
19=Takeda
21=Kimoto
25=Saito
27=Asuka
29=Hoshino
I killed Mr. Saito
1=Tanibe
3=Kuroi
5=Kaneko
7=Inoue
9=Nakayama
11=Yamamoto
13=Nakamura
15=With morning
17=Nakajima
19=Takeda
21=Kimoto
27=Asuka
29=Hoshino
1
3
5
7
9
11
13
15
17
19
21
27
29
Tanibe
Kuroi
Kaneko
Inoue
Nakayama
Yamamoto
Nakamura
With morning
Nakajima
Takeda
Kimoto
Asuka
Hoshino
②
import java.util.HashMap;
import java.util.Map;
public class UseMapAndEntity {
public static void main(String[] args) {
//TODO auto-generated method stub
Map<Integer, PersonalData> stock = new HashMap<>();
PersonalData taro = new PersonalData(46887, "Taro", 72.8, 167.3, 23, 1);
PersonalData jiro = new PersonalData(12547, "Jiro", 57.7, 170.4, 28, 1);
PersonalData hanako = new PersonalData(63544, "Hanako", 53.1, 155.4, 19, 0);
PersonalData keisuke = new PersonalData(34544, "Keisukr", 98.4, 192.4, 35, 1);
stock.put(46887, taro);
stock.put(12457, jiro);
stock.put(63544,hanako);
stock.put(34544, keisuke);
System.out.println("The element is" + stock.size() + "This is");
System.out.println("--------------Let's take a look at the contents---------");
for(Map.Entry<Integer, PersonalData> element :stock.entrySet()) {
Integer key = element.getKey();
PersonalData person = element.getValue();
StringBuffer sbCurry = new StringBuffer();
sbCurry.append("Membership number");
sbCurry.append(key);
sbCurry.append("Person of:");
sbCurry.append("Name =");
sbCurry.append(person.getName());
sbCurry.append(";");
sbCurry.append("Height =");
sbCurry.append(person.getHeight());
sbCurry.append("cm;");
sbCurry.append("Weight =");
sbCurry.append(person.getWeight());
sbCurry.append("kg");
System.out.println(sbCurry.toString());
}
}
}
public class PersonalData {
private Integer _id;
private String _name;
private Double _weight;
private Double _height;
private Integer _age;
private Integer _sex;
//constructor
public PersonalData (Integer id, String name, Double weight, Double height, Integer age, Integer sex) {
_id = id;
_name = name;
_weight = weight;
_height = height;
_age = age;
_sex = sex;
}
//Accessor method
public Integer getId() {
return _id;
}
public void setId(Integer id) {
_id = id;
}
public String getName() {
return _name;
}
public void setName(String name) {
_name = name;
}
public Double getWeight() {
return _weight;
}
public void setWeight(Double weight) {
_weight = weight;
}
public Double getHeight() {
return _height;
}
public void setHeight(Double height) {
_height = height;
}
public Integer getAge() {
return _age;
}
public void setAge(Integer age) {
_age = age;
}
public Integer getSex() {
return _sex;
}
public void setSex(Integer sex) {
_sex = sex;
}
}
result
There are 4 elements
--------------Let's take a look at the contents---------
Person with membership number 34544:Name = Keisukr;Height = 192.4cm;Weight = 98.4kg
Person with membership number 46887:Name = Taro;Height = 167.3cm;Weight = 72.8kg
Person with membership number 63544:Name = Hanako;Height = 155.4cm;Weight = 53.1kg
Person with membership number 12457:Name = Jiro;Height = 170.4cm;Weight = 57.7kg
that's all. If you have any suggestions such as something wrong, please contact us. Thank you for reading to the end.
Recommended Posts