Introduction
In most programming languages (and definitely in both Java & Javascript), there are some basic data structures which we need to use in our everyday programming tasks to accomplish various goals. These basic data structures are lists, sets and maps. In this article, I am going to explain these basic data structures and when each one should be used and their individual benefits. Also I will show sample code to use these data structures. But before the code, let us understand the uniqueness of these data structures:
- Lists or Arrays – These are basically arrays or a collection of items and their main strength is their ability to maintain insertion order of the items and we can access the individual elements fast enough. There are various sub types but that is for another article.
- Sets – These are also a collection but their unique feature is that they don’t allow duplicates. Also they don’t normally maintain insertion order.
- Maps – These are also a collection but not of individual items but of key-value pairs, so they can contain lot of key value pairs.
Sample code in Java and Javascript
Now that we have seen what are the basic data structures, let us see the sample code in Javascript where I have declared lists, sets & maps and added items to each and then printing their content to the console (when you run the program using the command node DSDemo1.js, you can see that the list prints the data in the same order, set has rejected the duplicate item oranges and may or may not have maintained the insertion order ) :
const fruitsList = ["apples", "oranges", "bananas", "oranges" ];
console.log(fruitsList);
const fruitsSet = new Set(["apples", "oranges", "bananas", "oranges" ]);
console.log(fruitsSet);
const fruitsMap = new Map([["apples", 10], ["oranges", 20], ["bananas", 40 ]]);
console.log(fruitsMap);
Once you have run the Javascript code, you can copy the below java code which also declares an ArrayList (basic list), a set and a map and added items in all of them and then printed the content (You can compile the java program using the command javac DSDemo1.java and run it using the command java DSDemo1. ) :
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class DSDemo1 {
public static void main(String[] args) {
List<String> fruitsList = new ArrayList<>();
fruitsList.add("apples");
fruitsList.add("oranges");
fruitsList.add("bananas");
fruitsList.add("oranges");
for (String fruit: fruitsList) {
System.out.println(fruit);
}
System.out.println("****************************");
Set<String> fruitsSet = new HashSet<>();
fruitsSet.add("apples");
fruitsSet.add("oranges");
fruitsSet.add("bananas");
fruitsSet.add("oranges");
for (String fruit: fruitsSet) {
System.out.println(fruit);
}
System.out.println("****************************");
Map<String,Integer> fruitsMap = Map.of("apples", 10, "oranges",
20, "bananas", 40);
for (Entry mapEntry: fruitsMap.entrySet()) {
System.out.println(mapEntry.getKey()+":"+mapEntry.getValue());
}
}
}
Running the java program also gives the same output – list contains all the items and also has the same order , set rejected the duplicate item oranges and map listed the key value pair of 3 items.
Conclusion
So that’s it – hope you got a taste of how we can implement lists, sets and maps in both Java & Javascript. The code in both the languages look similar. But I would still recommend you to run both the programs as that will give you more confidence. Feel free to share your comments/ feedback.