Declaring a LinkedList in Java

会有一股神秘感。 提交于 2019-12-04 01:57:14

Isn't LinkedList ob = new LinkedList() 100% correct?

Well I'd suggest using the generic form, but sure - if you want to use functionality which is specific to LinkedList, you need to declare the variable accordingly.

You might want to check whether the Deque<E> or Queue<E> interfaces have what you want though. If they do, use those in-keeping with the idea of describing what you need rather than what implementation you'll use.

Yes,

LinkedList<...> items = new LinkedList<...>();

is perfectly correct if you know that items will depend on methods of LinkedList<T> that are not captured in the List<T> interface.

You should always try to keep the declaration at the highest level possible, meaning that you should stop at the highest level that provides all the functionality that you need: if List methods are not enough, you're perfectly fine with your LinkedList declaration.

the rule "always code to interfaces" must be taken with some flexibility. what you are suggesting is fine, and as you came to the conclusion, the only option.

as a side note, coding to concrete classes like this is faster is most JVMs. deciding whether the performance is worth breaking the rule is the hard thing to decide.

If you actually have a need to use methods that are not on the List interface, there is certainly nothing wrong with using LinkedList's API. The general rule of programming to the List interface recognizes that 1) it's pretty rare to need those methods, and 2) in most people's experience, it's way more likely that I discover I need to sort the list and/or use a lot of random access, and decide to switch to an ArrayList, than it is I need one of the methods only LinkedList has.

It may be also that you could be programming to the Queue interface, if you find List isn't giving you what you need.

LinkedList is a generic. You should be doing:

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

(or whatever else you need to store in there instead of String)

Not exactly 100% correct.

A preferred way to declare any collection is to include the data type it's holding. So, for your example, it'd be LinkedList<Integer> ob = new LinkedList<Integer>();.

Nope.. This would be wrong, at the later stages if he wants to change his implementation from linked list to any other implementation of list type he will go wrong... So better to use the interface level declaration.

I won't always suggest you to use generics ..... Coz sometimes you may need to wrap different objects as here....

          String str="a string";
          boolean status=false;

          LinkedList ll = new LinkedList();
          ll.add(str);
          ll.add(status);

In some situations like case of RMI, u can only send serialized data.....and suppose you want to send a class object(which is unserialized).......There you can wrap the members of the class(primitives) in a LinkedList and pass that object as a whole.......not worrying about the huge number of arguments...... Consider for eg:

       public Class DataHouse
       {
             public int a;
             public String str;
             .
             .
             .
       }

Now Somewhere u need to pass the objects.... You can do the following....

             DataHouse dh =new DataHouse();
             LinkedList ll = new LinkedList();
             ll.add(dh.a);
             ll.add(dh.str);

             // Now the content is serialized and can pass it as a capsuled data......

you can still have access to LinkedList methods by using List, all you have to do is to type cast for example

((LinkedList)ob).add()

The point of using generic List and not LinkedList is because in case you simply change the type of lists you are using (let's say double linked list) your program will still work Generics are to simplify your code to be more portable and more "changeable"

Actually it would be better if it would be parametrized as both are raw types.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!