Data structure is a tool that can be used to store many data. Not only store many data, but also solve specific problems using data structure. There are many built-in data structure available in Java.
Array
Array is a data structure that can store many data with the same type. This is the basic syntax to create a new array.
data_type[] array_name = new data_type[length_of_array];
Notice that the array in Java has a fixed length. Because of this, specify the length correctly to avoid index out of bound exception when reading the data from the array. The array data structure is illustrated in this picture below.
This is the example of array usage.
public class MyApp {
public static void main(String[] args) {
// create new array that contains int data
int[] numbers = new int[4];
// assign the data
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
// access the value from the array
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
Output
1
2
3
4
Based on the code above, to insert a value into array can be done by specify the index then assign a value. This is the basic syntax to insert a value into array.
array_name[index] = value;
To access or retrieve the value from the array, can be done by using for
loop with numbers.length
used in loop to avoid index out of bound.
index out of bound caused by accessing a value from array that has a index greater or equals the length of the array.
To access a value in a specific index inside array, use this syntax.
array_name[index]
The recommended way to retrieve all the data from the array or other collection data type is using for each. This is the basic syntax of for each.
for(data_type variable_name: array_name) {
// code..
}
This is the example of accessing the value from the array using for each.
public class MyApp {
public static void main(String[] args) {
// create new array that contains int data
int[] numbers = new int[4];
// assign the data
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
// access the value from the array with for each
for (int temp: numbers) {
System.out.println(temp);
}
}
}
Output
1
2
3
4
List
List is a data structure that similiar with array but with the flexible size or length. This is the basic syntax to create a new list.
List<data_type> list_name = new ArrayList<>();
This is the example of using list to store many data that has a type of String
.
import java.util.ArrayList;
import java.util.List;
public class MyApp {
public static void main(String[] args) {
// create a new list
List<String> names = new ArrayList<>();
// insert some data
names.add("John");
names.add("Brad");
names.add("Tyler");
// access the data using for each
for (String name: names) {
System.out.println(name);
}
}
}
Output
John
Brad
Tyler
Based on the code above, the list works similiar with array but with the flexible length or size. The list that commonly used is ArrayList
.
This is the basic syntax to use the method.
list_name.method_name();
This is the example.
names.add("New name");
This is the list of basic methods that can be used in List.
Method | Description |
---|---|
get(index) |
access a data inside list in specific index |
add(data) |
insert a data into the list |
remove(index) |
remove a data inside list in specific index |
add(index, data) |
insert a data into the list in specific index |
set(index, data) |
edit or update a data in specific index |
isEmpty() |
check if a list is empty |
size() |
get the size or length of the list |
The remove mechanism works like this.
Set
Set is a data structure that only allows the unique data to be inserted. This is the basic syntax to create a new Set.
Set<data_type> set_name = new HashSet<>();
This is the example of Set
data structure usage. The commonly used of Set
is HashSet
.
import java.util.HashSet;
import java.util.Set;
public class MyApp {
public static void main(String[] args) {
// create a new set data structure using HashSet
Set<Integer> numbers = new HashSet<>();
// add some data
numbers.add(1);
numbers.add(2);
numbers.add(3);
// retrieve all data using foreach
for (int number: numbers) {
System.out.println(number);
}
}
}
This is the output.
1
2
3
Based on the code above, some data is added using add()
method. The data can be retrieved using for each.
This is the list of basic methods that can be used in Set
.
Method | Description |
---|---|
add(data) |
insert a data |
remove(data) |
remove a specific data |
isEmpty() |
check if a Set is empty |
size() |
get the size or length of the Set |
Map
Map is a data structure that can be used to store a data with key value pairs. This is the basic syntax of create a new Map. The Map that commonly used is HashMap
.
Map<key_data_type, value_data_type> map_name = new HashMap<>();
This is the illustration of Map data structure.
This is the example of using Map data structure.
// import wildcard using '*' notation
import java.util.*;
public class MyApp {
public static void main(String[] args) {
// create a new map using hashmap
Map<String, Integer> scores = new HashMap<>();
// insert some data
scores.put("Joe", 88);
scores.put("Zoe", 70);
scores.put("Riccardo", 90);
// get the value from key named "Joe"
System.out.println("The Joe's score is: " + scores.get("Joe"));
// retrieve all the keys
Collection<String> names = scores.keySet();
for (String name: names) {
System.out.println(name);
}
// retrieve all the values
Collection<Integer> collectedScores = scores.values();
for (int score: collectedScores) {
System.out.println(score);
}
}
}
Output
The Joe's score is: 88
Joe
Zoe
Riccardo
88
70
90
Based on the code above, the key of a element or data must be unique. The value can be unique or not. This is the list of basic methods that can be used in a Map
.
Method | Description |
---|---|
put(key, value) |
insert a key value pair data |
get(key) |
get a value by specific key |
remove(key) |
remove a data by specific key |
putIfAbsent(key, value) |
insert a key value pair data if the key value pair data is absent / not exists |
replace(key, value) |
update a value by specific key |
keySet() |
retrieve all the keys from Map |
values() |
retrieve all the values from Map |
isEmpty() |
check if a Map is empty |
size() |
get the size or length of the Map |
String
String is a data structure that can store many alphanumeric characters. To create a new String use this syntax.
String string_name = "value";
This is the example of String usage.
public class MyApp {
public static void main(String[] args) {
// create a new String
String name = "Joe";
String email = "joe@xmail.com";
// some method usages
System.out.println(name + ": " + email);
System.out.println("Uppercase name: " + name.toUpperCase());
System.out.println("Lowercase name: " + name.toLowerCase());
System.out.println("Is the `@` exist in email ? " + email.contains("@"));
}
}
Output
Joe: joe@xmail.com
Uppercase name: JOE
Lowercase name: joe
Is the `@` exist in email ? true
Based on the code above, some methods is available for String manipulation. This is the list of basic methods that can be used in String
.
Method | Description |
---|---|
toUpperCase() |
convert all the characters into uppercase |
toLowerCase() |
convert all the characters into lowercase |
contains(str) |
check if a given character or String (str) is exists inside String |
length() |
get the length of the String |
isEmpty() |
check if a String is empty |
substring(begin) |
get the String value from beginning index |
substring(begin, end) |
get the String value from beginning index until the end index |
concat(str) |
concatenate the String with the given String (str) |
equals(str) |
check if a given String (str) is equals with the compared String |
equalsIgnoreCase(str) |
check if a given String (str) is equals with the compared String but the case is ignored |
This is the substring(begin, end)
mechanism in String
.
The String can be concatenated using
+
operator. For example in this code:System.out.println(name + ": " + email);
Notes
Make sure to use the data structure based on the requirement. For example, Using Set
to store many unique data or using Map
to store a data with key value pairs.
Sources
- Learn more about List data structure in this link.
- Learn more about Set data structure in this link.
- Learn more about Map data structure in this link.
- Learn more about String data structure in this link.
I hope this article is helpful for learning the Java programming language. If you have any thoughts or comments you can write in the discussion section below.
Top comments (0)