• Skip to main content
  • Skip to primary sidebar
BMA

BeMyAficionado

Inspire Affection

LinkedList Implementation in Java (TDD WAY)

February 28, 2017 by varunshrivastava Leave a Comment

Hey buds, In this article, I’m going to implement a linkedlist in java and compare our linked list on various performance benchmarks in comparison with the original collections linkedlist. Our custom linked list will not contain as many features as offered by the collections framework but it will surely contains all the basic features which are required to be a part of any list. So, let’s begin.

Table of Contents

  • LinkedList Implementation
  • TDD way
    • Benchmarks
  • Conclusion

LinkedList Implementation

First of all we will create an interface List which will contain all the methods that a list must have in order to perform all its various functionalities. This list will be like a contract with its implementor that if it is implementing this interface than it is responsible for providing the necessary implementation for all these methods. Also, a List interface is used to provide the basic functionalities that every data structures must implement in order to be called as a list.

We want our list structures to add an item to it, remove an item from it, retrieve an item from it (GET) so for that I have created a contract (interface) that will force the implementation of these methods to the classes that are implementing it. Have a look at the List Interface below.

Interfaces are a really nice way to tell the implementor about the methods that are needed to be implemented in order to create an object with minimum functionality that is complete in itself. With a proper use of interface you can ease your development by a significant factor.

Next, we need a class which I have named CustomLinkedList which will implement the above List Interface and provide the implementation for those methods. By default, our class will have to provide the implementation for the methods provided above. Now, there are two approaches to provide the implementation for those methods:

Traditional Way – Simply start filling those methods with your code.
TDD Way – Test Driven Development which tells us to start by writing a test case and create your code backward.

I prefer the second option as it is often the best approach when it comes to writing a business logic for your class. I personally prefer this approach for writing business logic as it makes my code crisp and helps me divide it into different small modules which could be used multiple times, thus, saving bandwidth. TDD could be used to write entire application apart from business logic but then it’s not too fruitful.

I have embedded a complete Custom LinkedList class below implementation of all the methods. Just have a look at all the code and quickly move forward to TDD segment where we will be actually writing our code to make it a LinkedList.

TDD way

Normally, we would have to write lots of test cases to cover all the possible scenarios but I have kept it precise for you to understand and have fun with it and also appreciate the fact that it makes you so much more efficient. Once you grasp the concept of TDD then it becomes a playground for you. You automatically enjoy writing multiple test cases to make your application more robust and error free. The more scenarios you cover, more robust you make your application. We will give all the hard work to IDE for writing code for us and we will just write test cases.

Please take a look at below test cases that I have written to implement a linked list. These test cases are documentation in itself. It gives you a complete idea about a method and its functionality. Just by going over the test cases you can get a pretty decent idea of what that method does.

Benchmarks

Our custom linked list is going to be a little slow because of implementation logic followed. Have a look at below benchmarks:

Insertion

Insertion time for 5000 records using Custom Linked List
Total Insertion time: 3157625

Insertion time for 5000 records using Original LinkedList
Total Insertion time: 1034067

Deletion

Removal time Custom Linked List
Total Removal time: 21554
Removal time Original LinkedList
Total Removal time: 32844

Conclusion

TDD is the best way to program logic in your application. You don’t need any more reviews or approvals or testing because you know that your code is right. This is the kind of confident it develops in you. On top of that, it makes you a better programmer because you start to think in a different way which helps you to become more efficient.

Stay updated as I’m going to post some videos in which I will show you the approach on how to write test cases and leave all the difficult task of writing code and formatting to our smart IDE’s. Simply subscribe to our blogs and I will let you know once the videos are available.

Also, if you are more of a bookish guy then have a GO on the book mentioned below,

You might also want to read below articles,

  • A beginners guide to understand collections
  • Thinking in OOPS, How to think in terms of objects
  • Lear real world object mapping with Shopping Cart Implementation in Java

If you have any doubt, simply comment below. Did you made any modification to your custom linked list to achieve higher performance then do let me know.

And as always
Be my aficionado 🙂

Related

Filed Under: Programming Tagged With: datastructure, java, linkedlist, oops, programming

Primary Sidebar

Subscribe to Blog via Email

Do you enjoy the content? Feel free to leave your email with me to receive new content straight to your inbox. I'm an engineer, you can trust me :)

Join 874 other subscribers

Latest Podcasts

Recent Posts

  • Is The Cosmos a Vast Computation?
  • Building Semantic Search for E-commerce Using Product Embeddings and OpenSearch
  • Leader Election with ZooKeeper: Simplifying Distributed Systems Management
  • AWS Serverless Event Driven Data Ingestion from Multiple and Diverse Sources
  • A Step-by-Step Guide to Deploy a Static Website with CloudFront and S3 Using CDK Behind A Custom Domain

Recent Comments

  • Varun Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Vaibhav Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Varun Shrivastava on Should Girls Wear Short Clothes?
  • D on Should Girls Wear Short Clothes?
  • disqus_X5PikVsRAg on Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java

Categories

  • Blogging
  • Cooking
  • Fashion
  • Finance & Money
  • Programming
  • Reviews
  • Software Quality Assurance
  • Technology
  • Travelling
  • Tutorials
  • Web Hosting
  • Wordpress N SEO

Archives

  • November 2024
  • September 2024
  • July 2024
  • April 2024
  • February 2024
  • November 2023
  • June 2023
  • May 2023
  • April 2023
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • November 2021
  • September 2021
  • August 2021
  • June 2021
  • May 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016

Tags

Affordable Hosting (4) algorithms (4) amazon (3) aoc-2020 (7) believe in yourself (4) best (4) database (4) earn money blogging (5) education (4) elementary sorting algorithms (4) experience (3) fashion (4) finance (6) Financial Freedom (7) food (7) friends (3) goals (5) google (5) india (10) indian cuisine (5) indian education system (4) java (16) life (16) life changing (4) love (4) make money (3) microservices (9) motivation (4) oops (4) podcast (6) poor education system (4) principles of microservices (5) problem-solving (7) programmer (5) programming (28) python (5) reality (3) seo (6) spring (3) success (10) success factor (4) technology (4) top 5 (7) typescript (3) wordpress (7)

Copyright © 2025 · Be My Aficionado · WordPress · Log in

Go to mobile version