Let me first ask: What do you think makes you a good developer? Please do not feel threatened by that question of mine. I was just trying to boost your confidence. But do you know all the essential datastructures in object oriented programming lanugage ? And most importantly do you know when to use one or the other ?
Let me just ask you two more question. How long have you been in this field ? Can you evaluate yourself to find out your grasp of datastructures ? Regardless of the answers I was only trying to motivate and make you think about how far you’ve come. If you’re just beginning do not worry, as I will be providing you with details of the datastructures and their practical usage in this post.
Datastructures is one of the most important topic in the field of computer science. But as a beginner I found myself always wondering this: What in the name of supreme power is Datastructures ? Where do I even start grasping the concepts behind it. And most importantly how do I use something when I don’t really know what is it. And yes the answer to that came later in my career but I found it out the hard way.
Let us explore what data structure really is before digging right into the heart of this article.
Quick Navigation
What are data structures
Datastructures exist in all programming language in one form or the other. And there is at least one data structure common in every one of the with slight variation. They are there to help you manage and efficiently manipulate the data types in that programming language. They are well developed piece of code which makes efficient use of memory and contain multiple functionalities such as: Searching, Sorting, Dynamically Adding and Removing the data.
A dead simple definition of datastructures is: Datastructures are just ways of structuring data so that they can be efficiently manipulated whenever required. And the most important thing to remember is when we are talking about datastructures. It is always the related to memory (RAM), as we are manipulating data in memory. Though storage level data are also structured, they are quite different. Now let me explain in detail and in a bit formal manner, see below.
“A data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.” – Wegner, Peter; Reilly, Edwin D
I have provided a definition above and and yes that is taken from ACM Digital Library. Datastructures provide you an efficient way of manipulating the data in your computer’s memory. You might be thinking what has a memory got to do with me as I code in X language and it handles memory in itself. Well, if you’re in such mind-state then you might need to do a little more digging about computers in general. And it better be soon because as a developer ignoring such details will only hamper one’s own growth.
Practical example of data structures
Let me provide you with a practical example regarding why you might feel the need for datastructures.
Let us assume you have a list of employee objects and you don’t know how many of them there will be. And when a new object is prepared you have to insert it but the data cannot be duplicate. What will you do now ? Your first thought maybe to use an array of initial capacity. Then resize the array in each insert, but how will you make sure no duplicates are in the array. You might write an algorithm but how efficient will it be when handling large data set. Yes you might not know how to optimize it for efficient access.
The solution: Datastructures
Now that we have established a clearer picture of the situation. Let us look at 3 essential datastructures in object oriented programming. And how you might use them in practical situations.
3 essential data structures
Now that we have understood what is datastructures are. Let us look at the the most essential datastructures which we will be needing on a daily basis. And i’m speaking it as a developer.
Arrays
Surprised ? I guess so. This is one of the first data structure which you have ever come across as a beginner. And they are really very helpful. Especially in ECMAScript (JavaScript) there is only array in the name of Datastructure, though others exist.
Example in Java:
// declaring and initializing the array
int dailySavedAmount[] = new int[10];
// now add some data to it
dailySavedAmount[0] = 100; // in rupees maybe ?
// now get the amount at day 0
dailySavedAmount[0]
Code language: Java (java)
Example in Java Script:
// declaring and initializing the array
var dailySavedAmount = [];
// now add some data to it
dailySavedAmount[0] = 100; // in rupees maybe ?
// now get the amount at day 0
dailySavedAmount[0]
Code language: JavaScript (javascript)
We have looked at how to declare, initialize, assign and retrieve data from array let us look at some practical use case of when an array might be used.
When to use an array ?
There are multiple use cases of using an array let us briefly discuss when you might want to use one.
If you’re just storing primitive types such as int
, float
, double
, etc. you need to use an array because their Object might be a bit overkill. If you’re wrapping these types with anything else you might want to use other datastructures.
Also, if you’re planning to create a custom datastructures for your specific need. You might want to use an array as it can be quite fast manipulating an array. Let us look at another one of our datastructures in object oriented programming.
Lists
These are yet another one of many datastructures which have multiple implementation. List is just an abstract concept their implementation might be LinkedList
, ArrayList
, etc. which have their own use cases. A List can be defined as the the ordered sequence of stored data types which may be primitive or user defined.
These Lists might provide efficient ways to manipulate the stored ways to manipulate your stored values. These datastructures are the ones you will be using mostly in real world scenario. Let us look at an example of list in Java we don’t really have lists in JavaScript.
// declaring and initializing a list.
ArrayList<String> myFreinds = new ArrayList<String>();
// adding value to the list
myFreinds.add("Friend #1");
myFreinds.add("Friend #2");
myFreinds.add("Friend #3");
// getting the value from the list
myFriends.get(0);
// check if the list has the name of our friend
myFriends.contains("Friend #1");
Code language: JavaScript (javascript)
One thing you might have noticed above is the use of ArrayList
instead of List
. Why is that so ? It is because in Java the List is just an interface
which cannot be instantiated, learn more about interfaces. And ArrayList
is one of the implementation of List
. It has something to do with open closed principle in object oriented programming, read here.
Also Read: Interface in OOP: Guide to Polymorphism and Callbacks
TECHENUM
That was a short introduction about List in general with the use of Java. The concept in other language is completely same. They’re just a sequence of values.
When to use a list ?
But if you’re new to object oriented programming and you’ve just heard about List
. You might be wondering when should you use a List
? And the simple answer to that is most of the time. Really List
is your to go datastructures which you must use if you’re confused about what to use a sequence of values that has object.
If you’re handling the primitives array might be a better option. Nevertheless lists will help you and make your work a lot easier. If you any specific use cases then you might want to use other datastructures such as a Map
but mostly List
is all you need.
Maps
Now this brings us to the final datastructures in out article about datastructures in object oriented programming. Let us take a look at what are Map
and when to use them in everyday or in place of a List
.
Maps are just like list but they have a key to identify the values at specific position. That is you will have a pointer of your convenience to the value stored. For example from above example where we created a List
of friends. We do not know what kind of friends they are i.e Work or School or Home. Now if such cases arises where you must identify the value with a key you must use Map
.
Let us look at an example to clear our understanding about Maps
more. Please do read the comments to gain some understanding. Keep in mind that Map also is an interface we need to use an implementation.
// declare and initialize the HashMap
HashMap<String, String> myDifferentFriens = new HashMap<String, String>();
// store some data
myDifferentFriens.put("MyMate", "Friend #1");
myDifferentFriens.put("HerMate", "Friend #2");
// retrieve the data from the map
myDifferentFriens.get("MyMate");
Code language: JavaScript (javascript)
Upon immediate inspection we can see that the method for adding data into the Map
is quite different. We have used .put()
instead of .add()
like in List
. it is because Map works that way and it is predefined to accept two things: identifier and the value.
At this point you might have already understood what a map is. It’s just like a list but with a value identifier / pointer. Let us see when we might use this.
When to use a map ?
Now that we have understood how to declare and use a Map
. Let us understand when we might have a need for one. Maps are really helpful for storing the related data with some pointer.
So if you have some data which must be identified with certain key. Think of this like the values such as username
and password
. You cannot just store these values in a List
because then we cannot identify value at which index is what.
ArrayList<String> cred = new ArrayList<String>();
cred.add("username");
cred.add("password");
Code language: JavaScript (javascript)
If we take a look at the example above we cannot identify the username and password because we have nothing to separate them. Now let us look at same example with map and you will understand what I mean.
HashMap<String, String> cred = new HashMap<String, String>();
cred.put("user", "random_user");
cred.put("pass", "thi$is@password");
Code language: JavaScript (javascript)
Did you notice ? Now you can identify the username
and password
independently. Now you might have understood when to use a Map
.
This is just the beginning
But wait there’s more.
This is just the beginning, there is so much to explore. I have presented just a couple of datastructures. But now it’s your turn to explore more. There a ton of datastructures out there which you might be of really good use in your career. You have to just keep exploring and keep learning about datastructures in object oriented programming.
Also Read: 7 Sites to Learn Programming Languages Online For Free
TECHENUM