when to use List, when to use LinkedList ?(java) [duplicate] - java

This question already has answers here:
What does it mean to "program to an interface"?
(33 answers)
Closed 5 years ago.
I was a bit confused, when we use
List<String> lst = new LinkedList<>();
when we use
LinkedList<String> lklst = new LinkedList<>();
At the beginning, I thought they are the same, but today, I realized they are not the same. For example, if I call lst.getFirst() It will tell me there is a error. However, if i do lklst.getFirst(), it works fine. My question is when do we use lklst then? why they are different? Also, does it apply same rule for Map. THanks!

On the left hand side you're declaring the type of the variable, lst. Since lst's type is List you can only access methods of a List, even if the object lst points to is really a LinkedList. There's an inherent tradeoff between declaring a variable of a concrete type like LinkedList (access to more methods / behavior) vs. a more abstract interface (safer, better compartmentalized code).
This is a big topic, and there isn't one simple answer for when to do one vs. the other (though there's lots of advice out there about it!) - you'll need to figure out which is appropriate for your use case.
Effective Java - Item 52: Refer to objects by their interfaces is a pretty canonical citation for this issue, and as the title implies suggest preferring List rather than LinkedList.

Related

how JVM does memory management(or efficiently use memory) for the interface references? [duplicate]

This question already has answers here:
What does it mean to "program to an interface"?
(33 answers)
Closed 7 years ago.
we always use this code
List mylist=new ArrayList();
I look this statement into deep and i found that mylist is an interface reference which is referring to the ArrayList which is a class.I found(on internet) that there is some benefits of it like Loose coupling ,memory management etc.But How ?what are the benefits of using interface refernce?
There is no memory management benefits.
However, using the interface Type allows you to make use of polymorphism.
Here, it means you can use any class inheriting List as a working implementation replacement.
This is allowing loose coupling because you are not tied with ONE and ONLY list implementation.
https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html
https://docs.oracle.com/javase/tutorial/java/concepts/interface.html

What is the difference [duplicate]

This question already has answers here:
Why should the interface for a Java class be preferred?
(10 answers)
Closed 8 years ago.
everyone!
Just started studying Java and can not understand one moment, for example:
Set<Integer> intset = new HashSet<Integer>(); (1)
HashSet<Integer> intset2 = new HashSet<Integer>(); (2)
What is the difference between these examples? Or they are the same? (1) example looks like up-casting, so it means that we have HashSet with only Set(interface) methods? Thank you for your reply!
When you write
Set intset = new HashSet();
You give the guarantee that the following code, if not casting, won't use any of the specificities of the HashSet class. This means future implementations may replace HashSet with any other implementation of Set, or maybe receive it from elsewhere. This is actually a best practice as it means your code is less coupled.
Set is an interface and HashSet is an hash implementation of Set interface.
The inset is a Set, so, it can refer any implementation of a Set, like HashSet. On other hand, inset2 can only refer an HashSet object.
This is polymorphism.
What is the difference between these examples?
Example one creates a "pointer" to the space in memory of a type of a Set. That means that you will have ability to manipulate that space in memory using all methods declared in the Set interface.
Example two creates a "pointer" to the space in memory of a type of a HashSet. That means that you will have ability to manipulate that space in memory using all methods declared in the HashSet class.
Or they are the same?
It depends on what you are going to do after those two lines. Set is a common contract for the Set-like collections but sometimes we need more specific methods. That is when you need to downcast you Object to a more specific type. Interfaces are supposed to deal with the commonality.
(1) example looks like upcasting, so it means that we have HashSet with only Set(interface) methods?
Yes.
Set[Set][1] is an interface, HashSet[HashSet][2] is a class, Implementing Set interface methods in HashSet classes, HashSet class implements Set interface

What is different between this two arrayList definition? [duplicate]

This question already has answers here:
Type List vs type ArrayList in Java [duplicate]
(15 answers)
Closed 10 years ago.
I use this two statements to ArrayList definition:
ArrayList<String> my = new ArrayList<>();
List<String> my2 = new ArrayList<>();
Which one should i use?
With your second definition, you could later replace ArrayList constructor for another type of List, like a LinkedList or a high-performant list, or any other kind of list that may exist in the future. In your first definition, you are tied to the ArrayList implementation forever. You cannot change it, because the rest of your code is trusting in this reference being explicitly an ArrayList.
By using an interface, on the contrary, your code relies on a contract, that of the interface, and not in a particular implementation (like ArrayList). That gives you the power of changing implementations without affecting its users. And change is something that we must foster and plan ahead, simply because we cannot prevent things from changing.
The latter. Program to an interface, not to an implementation.
Depends on what you need. It's generally advised to use interfaces (hence List) if possible, otherwise you're sticked to implementation class. Say, you use ArrayList as input parameter of some method. For some reason (e.g. performance) you decide at some point to switch from ArrayList to LinkedList. So you must change also the type of the input parameter. If you use interface (List), you're more free to switch implementation without the need of refactoring code.

Java arrayList vs. C++ vector [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's the C++ version of Java's ArrayList
I was reading a book called "Cracking the Coding Interview" and most (all?) of the code is in Java and arrayList is used a lot. During an interview, would using a vector instead of arrayList be acceptable if the language is C++? I'm asking because I haven't seen even one example of C++ code for similar questions using a vector but I'm not sure if there's a significant difference or not.
And is there also an equivalent in C?
The answer is two-fold: Firstly, you cannot compare utility classes between C++ and Java like that - different languages come with different cultures, naming conventions etc. If there was a Vector class in a C++ library, there's no connection whatsoever to any Vector class in Java, except for the name.
Secondly, the Vector class in Java is in practice deprecated, and I would discourage you from using it. In fact, forget about it :) The combination of List and ArrayList is the way to go. Use interfaces where you can, say:
List myList = new ArrayList();
Example deliberately missing generic typing.
Ignoring synchronization, the main difference between Vector and ArrayList is that Vector is a resizable array (similar to a C++ STL Vector) and ArrayList is a List that happens to be backed by an array.
arraylist-vs-vectors
In the back end, they are both arrays with functions on top to assist the programmer. Now, how different are they fundamentally?
check here :
http://www.reddit.com/r/learnprogramming/comments/l6o65/arraylist_java_vs_vectors_c/

what is the difference between these 2 lines? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why should the interface for a Java class be prefered?
ArrayList<Integer> al = new ArrayList<Integer>();
List<Integer> l = new ArrayList<Integer>();
what is the difference between these 2 lines? Is there any rules that I should use former one rather than later one in any case? Or vise versa? What is advantage or disadvantage of using particular one?
Thanks.
The first line creates an ArrayList and stores it in a variable of type of ArrayList, the second line stores it in a variable of type List.
List is an interface of which ArrayList is an implementation. The rule of thumb for deciding which type to store the instance in (List or a specific implementation, like ArrayList) is that you should store at the most generalized level suitable for your needs. This means that if you know that the variable must conform to behavior only exhibited by ArrayList and not a List in general, then you should use ArrayList, otherwise, use List. (This holds for LinkedList or other List implementations, too)
The reason why you want to use the second line it's because it's usually better to program to interfaces than to classes, sometimes it's related to good practices to do that and one of the benefits it's that you end up with code that:
It's better to test
Can use different implementations
It's not coupled to ArrayList
For more information you can take a look at the "Hollywood principle" or the Strategy pattern

Categories