slider

Recent

Search This Blog

Fourni par Blogger.

Blog Archive

Navigation

What is the difference between a SET and a LIST in JAVA

Welcome you all to another article from WitCentre. We are mainly discussing about differences between a Set and a List in Java through out this article. Thus, hope you all have at least a basic idea about Java to understand some concepts here in this article, and try to give you a better explanation about the topic. 


First of all, let's try to understand what is a set and what is a list in java. Then, move towards their differences. That is a better way to grab the whole idea!

What is a SET in Java?

In the mathematical form, a set can be defined as a collection or a group of distinct objects which are under their own rights. Hence, the background for a set in Java is also coming from its mathematical base. Thus, the main constraint in the Java is also similar that, a Set is a Collection where it cannot contain any duplicate or redundant element. Further, we can say that sets do not contain a pair of elements ( e1 & e2) such that e1.equals(e2) in more formally. It may contain at most one null element. 

How to implement a set in Java?

Java provides an interface as Set<E> where we can implement sets in Java. Actually, this interface models the mathematical set abstraction as mentioned above. There are three general-purpose set implementations in Java. Refer the following table to get an idea about them in brief quickly.
In the set implementation in Java, a set contains only methods which are inherited from Collection interface and set adds restrictions itself to avoid having duplicates. As an important usage, Java Sets allow to use equal() function to check whether two sets are identical. If two sets contain exact same elements with same set size, then both are equal. Apart from that, there are more bunch of methods to be used for Java sets from collection interface. You may learn them further from here!

What is a List in Java?

As Java documentation states, List is an ordered Collection or can be called as a sequence in Java which may contain duplicate elements. This is also another interface provided by Java for many valuable usage by inheriting its Collection interface. Thus, List also inherits all the methods from Collection and in addition to that it includes some other operations/methods inside itself. Those operations make higher value for a list implementation in every program. Those additional operations can be shown as follows. 
  1. Positional Access - Hold the control of elements over their numerical position in the list. get, set, add, addAll, and remove are methods it includes.
  2. Search - This allows to search for a specific object and return its numerical position. indexOf and lastIndexOf are the search methods.
  3. Iteration - This allows to achieve list's sequential nature by extending the Iterator semantics. listIterator method is there for this.
  4. Range-view - This is a very usable property in practice to have sub lists from range operations. Thus, sublist method allows it.
How to implement a list in Java?

Similar to the Set<E>, Java provides List<E> interface for list implementation and it inherits the same Collection interface too. Further, there are two general-purpose List implementations supported by Java platform such as ArrayList and LinkedList. Refer the following table to have a brief idea.

As mentioned above, ArrayList implementation is the most widely used List implementation in Java as compared to the LinkedList implementation. However, both contain advantages of using in the correct place as necessary. As another outstanding feature in List implementation is that it provides special iterator called  ListIterator. This allows more functionality other than the Iterator interface provides such as element insertion, replacement, and bidirectional access. In addition to the mentioned functionality, Java Lists have many more important methods from Collection interface and itself. You may have a look for more details here!

What is the difference between them?

This section addresses to the main topic of the article and hope you all could have brief look at Sets and Lists in Java separately. Now we are in the point of summarizing all and formulating the differences between Sets and Lists in Java! I hope that a tabular format will give you more insights for a better understanding. Thus, have a look at the following table for listing the differences. 


Hope this could give a good explanation for the topic question. However, there can be advantages and disadvantages of using Sets and Lists in program at certain instances. The most important thing to keep in mind is that always look at the requirement of having a data structure in programs and select the best one based on the requirement. It will directly affect to the performance of the program. The best selection will produce the best program!

Thanks for reading and do not hesitate to have any comments or contact us (WitCentre) via the contact form right there!
Share
Banner

Post A Comment:

0 comments: