What are Algorithms and Data Structures?
In programming, we often come across Algorithm and Data Structure. So let's see what they mean and why they are needed.
An algorithm is a set of instructions for solving a problem or performing a task. That is, it is a sequence of steps on how to solve the problem. Algorithms make it possible to perform operations on data.
A Data Structure is a special tool for organizing and storing information (data) in memory so that we can perform operations on the stored data more efficiently.
The Collections Framework provides an implementation of data structures in Java.
Classes and interfaces are marked in the picture. Let's first see what are the differences between class and interface. These are OOP (object-oriented programming) concepts and OOP is a broad topic. Let's briefly look at the differences between a class and an interface.
If it is not an abstract class, an object can be obtained from the class. A class can be extended from a class, and it can implement interfaces. When implementing, the default methods in the interface will be Overridden.
An interface can only be extended from an interface and implemented only in a class.
It is not shown in the figure, but it extends the Collection interface from the Iterable interface. Therefore, all sub-interfaces of the Collection interface are extended from the Iterable interface. In other words, the Iterable interface (java.lang.Iterable) is the root interface of Java Collection (java.util.Collection) classes (interfaces).
As you can see from the picture, Map is not extended from Collection interface and the reason it is different from List, Queue, Set is that Map is not Iterable. That is, Map uses key-value pairs, but Collection subinterfaces only use values.
Data Structures are a very broad topic, in this article we will look at the differences between List, Set and Map.
What is a list?
The List interface (java.util.List) is an ordered sequence of objects. Elements in a List can be inserted, accessed, duplicated, and deleted in order.
package list;
import java.util.ArrayList;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
List<String> bookNames = new ArrayList<>();
bookNames.add("Outliers");
bookNames.add("The Alchemist");
bookNames.add("What Men Live By");
bookNames.add(null);//any number of null values can be added.
bookNames.add(4,"Removable");
bookNames.add(null);
for (String book : bookNames)
System.out.println(book);
System.out.println(bookNames.get(1));
bookNames.remove(4);
System.out.println(bookNames);
bookNames.clear();
}
}
What is a set?
There are similarities between Set java.util.Set and List. Both interfaces are composed of elements.
package set;
import java.util.HashSet;
import java.util.Set;
public class SetExample {
public static void main(String[] args) {
Set<String> writers = new HashSet<>();
writers.add("Malcolm Gladwell");
writers.add("Paulo Coelho");
writers.add("Removable");
writers.add("Leo Tolstoy");
for (String writer : writers)
System.out.println(writer);
writers.remove("Removable");
System.out.println(writers);
writers.clear();
}
}
What is a map?
The Map interface consists of java.util.Map key/value pairs. Each key is associated with a specific value and stored as such in memory. In Map it is possible to get the values via get(key) key. If we select the keys as integers starting from 0, then Map and List will show similarity because we define the keys as index as in List.
package map;
import java.util.HashMap;
import java.util.Map;
public class MapExample {
public static void main(String[] args) {
Map<String,String> books = new HashMap<>();
books.put("Malcolm Gladwell","Outliers");
books.put("Paulo Coelho","The Alchemist");
books.put("Leo Tolstoy","What Men Live By");
books.put("Removable",null);
books.put(null,null);
books.put(null,null);
System.out.println(books.get("Leo Tolstoy"));//getting value by key
for (Map.Entry<String,String> entry : books.entrySet())
System.out.println(entry.getKey() + " - " + entry.getValue());
books.remove("Removable");
System.out.println(books);
books.clear();
}
}
Differences between List, Set and Map:
- The List interface allows repeating elements, while Set and Map do not.
- List has an insertion order, that is, each input value is stored in memory with a unique index, while Set and Map do not have an insertion order.
- It is possible to add any number of null values to List. It can only be possible once in a Set. Map can have only one null key and any number of values with different keys.
- List interface is implemented by ArrayList, LinkedList, Vector classes. Set interface is implemented by HashSet, LinkedHashSet, TreeSet classes. Map interface is implemented by HashMap, HashTable, TreeMap, LinkedHashMap classes.
- You can call any element according to its index through the get(index) method of the List interface. But this is impossible in Set, Map.
- If in the program we write, indexing the elements in the data will be used a lot, we need to take a List. If unique elements will be used Set, if key/value pair will be used Map should be used.
- The List interface uses a ListIterator to iterate through the elements. Set Iteration and Map use keyset, value and entry sets.