-1

I know you can create your own Linked List class or import one with java.util. Like so :

import java.util.*;  

LinkedList<String> ll=new LinkedList<String>();  

and then do some stuff to the list with the already existing methods, like add, get, set... But you can also create your own Linked List liked that :

class LinkedList {
Node head; // head of list


class Node {
    int data;
    Node next;

    Node(int d) { data = d; }
}
}

But if you're doing it like that you need to create all of the methods. My question is pretty simple, should I use method 1 or 2 ? And if both are fine when would it be better to use one over another.

TTiM3R
  • 41
  • 8
  • Honestly, unless you know why you are specifically using `LinkedList`, it is usually better to just use `ArrayList`. Also why would you create your own implementation of `LinkedList` to begin with? – Nexevis Aug 18 '21 at 12:24
  • It’s better to use the already-implemented one unless you want to write your own. – Dave Newton Aug 18 '21 at 12:25
  • You should use the existing, standard `LinkedList`, because it has been tested and used millions of times by many thousands of programmers and works fine. You should only write your own if it's specifically required, for a school assignment, for example. – Jesper Aug 18 '21 at 12:25
  • @Nexevis To learn how to write a linked list implementation. – Dave Newton Aug 18 '21 at 12:26
  • @Dave Newton Well then he wouldn't be asking if he should just use `LinkedList` instead of writing his own if thats his intended purpose? – Nexevis Aug 18 '21 at 12:31
  • @Nexevis You asked why, I answered ¯\\_(ツ)_/¯ – Dave Newton Aug 18 '21 at 12:32
  • @Nexevis - because Java's implementation of linked list has issues, which are explained in [this answer](https://stackoverflow.com/questions/67602528/67606719#67606719) . – rcgldr Aug 18 '21 at 13:16

2 Answers2

3

If you need a general-purpose List, use java.util.LinkedList. It's a standard class, mature, well-tested, well-understood by almost any Java dev... and, ofc, it's concise, because you don't have to implement it.

Actually, don't use it: use java.util.ArrayList or java.util.ArrayDeque, they out-perform LinkedList in almost all circumstances. See When to use LinkedList over ArrayList in Java?

If you really need something more specialized, implement your own, but only if you really, really need it.

Only make your class implement java.util.List if you actually need it to.

A good starting point to avoid having to implement "everything" in the interface is to extend java.util.AbstractList: this only requires you to provide an implementation of get(int), size() and, if you want the list to be modifiable, set(int, E) (you may well want to override others to get better performance for a linked list).

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
2

Don't reinvent the wheel unless you need something extremely specific that the existing wheel doesn't provide.

Also, might be worth mentioning that linked lists have poor performance characteristics since the memory they occupy isn't continuous, causing the multiple reads from main memory (link).

Malt
  • 28,965
  • 9
  • 65
  • 105
  • That's an insightful article to read. I will have to check myself if it's true in java as the author explained. Thank you for sharing this. – Marek Żylicz Aug 18 '21 at 12:47