≡ Menu

The 5 Basic Concepts of any Programming Language – Concept #3

Hi everyone,

Without further delay, let’s jump right into our 3rd basic concept of programming languages:

  1. Variables
  2. Control Structures
  3. Data Structures
  4. Syntax
  5. Tools

Data structures, what are they, why are they useful?  Well, let’s turn to a quick definition from wiki:

In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

Okay, that definition is a little more down to earth.  But, there is a lot of ‘meat’ behind data structures.  Let me try to explain the point of data structures by giving you an example… Let’s use a list of contacts as the example!  You probably have a list of contacts somewhere in your life, whether it’s in one of your email programs or an address book in a kitchen drawer.  There are a bunch of contacts, and that list of contacts could grow (or shrink) at any given moment.  If you were to try and represent all those contacts as variables in a computer program, how would you do it?  Well, there’s a right way, and a wrong way.  For  the purposes of our example, let’s say we need to keep track of 10 contacts.

First, the WRONG WAY:

If we need to store 10 contacts, we would probably define 10 variables, right?

String contact1, contact2, contact3, contact4, contact5, contact6, contact7, contact8, contact9, contact10;
contact1 = "John Smith (john.smith@someCompany.com)";
contact2 = "Jane Smith (jane.smith@someCompany.com)";
contact3 = //... etc.

In the world of programming, this is just a terrible way of trying to store 10 different variables.  This is because of two main reasons:

  1. The sheer amount of text that you’ll need to write in your program
    • Sure, right now we only have 10 contacts, so it’s not too bad, but what if we had 1,000 contacts!  Imagine typing that out a thousand times!  Forget about it!
  2. The flexibility of code
    • If we need to add another contact, we wouldn’t be able to do it without manually editing our code. We would have to go into our code, physically write out contact11, and then try to store whatever information is needed into the new variable. This is just crazy talk!

So, what is the RIGHT WAY?

I’m glad you asked, it’s a data structure of course!

In this case, we have a list of contacts, and with Java there is a data structure called a List!  Okay, so what does it look like?  Here’s the code:

List contacts = new ArrayList();

Again, let’s not worry about all those symbols and confusing brackets and semi-colon, we’ll cover that later.  All I need you to understand right now, is that there is a way to store a bunch of contacts into something called a data structure (in the case of our example, a List).  Okay, so, what’s so great about a List?  Well, for one thing, you can add and remove things from a list with ease.  So if you started with 10 contacts, it’s a piece of cake to add another contact to the list, how you ask?  Just like this:

contacts.add("John Smith (john.smith@someCompany.com)");
contacts.add("Jane Smith (jane.smith@someCompany.com)");

Voila, we’ve added another contact to our contacts list!  So, you may be saying, the right way looks like just as much typing as the wrong way.  You’ve got a point, but the main difference is that with the first approach, you had to “create” 10 unique variables (i.e. contact1, contact2, contact3, contact4…), but with the second approach, we only created 1 variable (contacts).  Because we only had to create 1 variable, and because we can say contacts.add(someRandomContact), means that our code is much more flexible and dynamic.  When I say dynamic, I mean that the outcomes of the program can change depending on what variables you give to it.  That’s really the key to using data structures!   We want our code to be as dynamic as possible, we want it to be able to handle a bunch of situations without having to write more and more code as the days go by.

In essence, a data structure is just a way to get around having to create tons of variables.  Now, there are a bunch of other data structures in Java, but I’ll just quickly touch on the ones we use most often when programming.  The next one I’ll talk about is the HashMap.  This doesn’t sound as straightforward as our last data structure (the List), but it’s really just a fancy name for a fairly simple concept.  So, what is a hash map?  Here’s a visual representation:

“Honda” -> “Civic”, “Prelude”
“Toyota” -> “Corolla”, “Celica”, “Rav4”
“Ford” -> “Focus”, “Mustang”
“Audi” -> “R8”

What you see here, is the make of a car on the left, which points to different models of that make.  This is known in the programming world as a Key/Value pair.  The key in this case, is the Make of the car (Toyota, Honda, Ford, Audi), the value in the case, is the model (Civic, Corolla, Focus, R8).  That is how a HashMap works, and it’s just another data structure!  This concept is use all over the place on the web.  For example, if you have ever filled out a quote for automobile insurance online, you’ve probably had to choose the make and model of your car.  I bet you that information was presented on screen by using a HashMap (just like I showed you above)!  Because when you select “Toyota” from one drop down list, the second drop down list will need to change its contents to only show models of cars made by Toyota.  Then if you change the first drop down to Honda, that second drop down list will change again to show cars made by Honda.

So how about I show you what a HashMap looks like in Java code?

Map<String, List<String>> cars = new HashMap<String, List<String>>();
cars.put("Toyota", new ArrayList(Arrays.asList("Corolla", "Celica", "Rav4")));

Wow, there’s a lot of stuff going on in there, but don’t be intimidated, all you need to understand right now is the concept that this HashMap data structure stores key/value pairs.  In our case, a make is the key, and a List of models is our value.  Now, that’s kind of interesting, did you see what we did there?  I just said that we have a HashMap data structure, and inside the HashMap we are storing a List (which is another data structure).  This is done on a fairly regular basis in the programming world.  We can use one data structure inside of another!  So, if the point of data structures is to help us minimize the number of variables we need to create, then we are really saving ourselves the hassle of creating a lot of variables!

Want Free Access to my Best Java Courses?

Alright, so if you’re read this far down the article, then you’re clearly interested in learning how to code. You can currently get access to my 2 best courses on Java for free for 30 days (then it’s just $10/month afterwards, or $97/year).

When you sign up you’ll receive login information to start learning about Java. If you decide you don’t like the courses, you can cancel at any time. If you decide you love the courses then you don’t have to do anything and you can stay subscribed for as long as you like for just $10/month or $97/year.

It’s like Netflix for coders!

Go ahead an click the button below to get started!

Okay, so let’s sum up everything we’ve talked about.  A data structure is a way of storing and organizing data in such a way that it can be used efficiently.  The point is to avoid creating crap loads of variables people!  Java allows us to do this by using Lists and HashMaps (as well as many other data structures, but these two are the most common).  These two data structures can grow and shrink without you having to worry about coding all of that ‘behind the scenes’ stuff.  When you add to a List, the new element is there and is usable (like magic), when you remove from the List, that element is now gone (poof!).  This saves us from creating and deleting variables in our code manually.

Alright, that about sums up what I need you to know for our next section: syntax.  In my opinion as a veteran programmer,  syntax is the one thing that will discourage you from learning to program.  It takes practice and patience to understand, but I promise you, it will become second nature in time.  So please be patient, and more importantly, be excited to learn about syntax in Java, because once you get the hang of it, you’ll be on your way to programming like a champ!  If you’re eager to learn right now, follow this to the next Java tutorial.

Plenty of Ways to Learn

  • COMMUNITY – Learning with a community is the best way to ensure accountability and support when you need help. Coders’ Campus is a brand new community that is dedicated to new programmers like you!
  • BLOGS – If you feel like you enjoy the way this information is laid out in blog format, then I invite you to click on the “next” button below.
  • BOOKS – If you feel like you are better at learning from a book, then I invite you to check out my ebook via http://javapdf.org. This eBook is organized, and packed full of easy to follow tutorials and videos.
  • VIDEOS – If you feel like you prefer learning by watching Videos, then check out the Java Video Tutorials website. It’s full of fun and easy to follow video tutorials. So far the students enrolled in this video course have rated it 9.5/10!
  • PODCASTS – Finally, if you prefer to learn by just listening, then there’s also a podcast available on iTunes via How to Program with Java Podcast.
{ 27 comments… add one }
  • Charlie August 9, 2012, 5:19 am

    Your code for the HashMap definition has been nerfed by the browser it looks like:

    Map> cars = new HashMap>();

    instead of:

    Map cars = new HastMap();

    (unless this comment get similarly messed up!)

    • Trevor Page August 9, 2012, 6:33 am

      Hi Charlie,

      Thanks for pointing that out, not sure how it got messed up, but I have fixed it now 🙂

  • rOBzUC August 9, 2012, 8:30 am

    …and now:
    Map<String, List> cars = new HashMap<String, List>();

    • Trevor Page August 9, 2012, 8:54 am

      Hmm, it seems to display properly for me… should read:

      Map<String, List<String>> cars = new HashMap<String, List<String>>();

  • John August 9, 2012, 8:36 am


    contacts.add(“John Smith (john.smith@someCompany.com”);
    contacts.add(“Jane Smith (jane.smith@someCompany.com”);

    Should read:

    contacts.add(“John Smith (john.smith@someCompany.com)”);
    contacts.add(“Jane Smith (jane.smith@someCompany.com)”);

  • rOBzUC August 10, 2012, 1:42 am

    …I see:
    “Map<String, List> cars = new HashMap<String, List>();”
    …is it due to my browser character encoding?

    • Trevor Page August 10, 2012, 6:33 am

      It could be, what browser are you using? I use Google Chrome with US-English settings.

      • rOBzUC August 10, 2012, 9:31 am

        Firefox 14.0.1, Unicode (UTF-8). I can’t find US-English encoding in menu “View” => “Character Encoding”. I tried other encodings, but the only visible difference is when the page is full of chinese logograms…

  • Preethi November 26, 2012, 4:09 pm

    You website is awesome!!!! Your way of teaching is really cool..it doesnt put me to sleep nor get me bored..I have so many Java Books but i hate to even touch them ,fear of falling asleep…but you website is soo brain friendly…and its simply cool..I saw your video tutorial too..its just Brilliant…Seriously very cool way to teach people! Kudos to all your hard work!!!! Iam now ur big fan!!!God bless…Iam a Java developer..with 5 years of experience in programming..but it still feels great to go through basics…:-)

  • Durim January 30, 2013, 1:24 pm

    I’m going through your podcast and I really like it:)
    And I always visit howtoprogramwithjava.com/session(that u say) and I understand these concepts very well, I’m another fan Trevor, u r great man!!
    And I hope that I’ll buy your eBook too as soon as I can:D
    thnx for everything!

    • Trevor Page January 30, 2013, 2:36 pm

      Always a pleasure to hear from another happy listener, so thank YOU for your comment.

      I’ll hopefully be releasing another podcast episode tonight.

  • GutterBoy February 11, 2013, 1:54 pm

    Hi Trevor Page-Im a newbie in Java Programming have been self teaching myself over the net for the past year and i must say these are the most clear and brain friendly tutorials ive come across so far.Thank you for giving a poor man a chance in this evil world.

    • Trevor Page February 11, 2013, 3:00 pm

      Haha, no problem kind sir! I like how you worded that comment… thanks for the compliment and best of luck on your journey!

  • apple de belen February 18, 2013, 6:23 am

    your very great ! ! oh , im an I.T student in AMA COMPUTER COLLEGE, I ADMIT java programming(csci) is one of difficult sub.not easy to understand. but i want to learn more, but i really dont know what my teachers discussing on, my parents want me to finish this course but i dont know if i do it 🙁 but, thank you !! so much!! it was awesome,, 🙂

  • epic44 February 28, 2013, 2:04 pm

    Why would we use data structure as we can use database to CRUD operation, isn’t it good then Collections? Where we would use static data like Collections in program? i mean in which situation. Thanks your podcasts/tutorials are awesome 🙂

    • Trevor Page February 28, 2013, 3:44 pm

      Database operations exist on the data layer of your application… but what happens when you’re in the business layers of your code, how can you use the data stored in the database? The best method for doing this in Java is by utilizing Collections.

      Keep reading further in the tutorials for more information around these areas

  • Idowu July 8, 2013, 3:24 pm

    Am really pleased with this piece.

  • David McFeeters-Krone May 31, 2015, 11:13 am

    I run a crowd-sourced typo-alert service – EditMob. One of my MobEditors is a reader of your website and caught some errors. I’d love you to join our project, currently in free beta.

    Regardless, contact me and I’ll point you right to the issue.

    503 493 0477

    The Proof is in the Mob

    EditMob is a shame and schadenfreude free service.

  • isbah January 2, 2016, 4:43 pm

    i have to make a java project with database connectivity.my topic is boutique management system.i’m using eclipse

  • Fares Elias September 23, 2016, 5:46 pm

    I can’t find homework practice file !!

  • Bwambale Brian Mutiba May 14, 2017, 5:20 am


  • John Lardinois May 20, 2017, 10:54 pm

    Then there are Classes… easily the worst thing that’s ever happened to programming. Not that it is a BAD thing, but it’s definitely the worst thing.

    Java is all about classes.

    It’s kind of like a variable linked to a data structure linked to a function.

    Basically used for making custom variable types.

    Easily the largest and most important part of Java.

  • ceril July 22, 2018, 12:12 pm

    i need a help man .the link you given to download the java development kit doesn’t have a tool for 32 bit windows

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.