Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List vs Queue vs Set of collections in Java

what is the difference among list, queue and set?

like image 972
Sumithra Avatar asked Oct 15 '10 09:10

Sumithra


People also ask

What is the difference between list and Queue in collections?

You can add an element anywhere in the list, change an element anywhere in the list, or remove an element from any position in the list. A queue is also ordered, but you'll only ever touch elements at one end. All elements get inserted at the "end" and removed from the "beginning" (or head) of the queue.

What is difference between list and Set in Java collections?

List is an ordered sequence of elements. User can easily access element present at specific index and can insert element easily at specified index. Set is an unordered list of elements. Set does not have duplicate elements.

What is collection in Java difference between Set list and map and Queue?

The map allows a single null key at most and any number of null values. List implementation classes are Array List, LinkedList. Set implementation classes are HashSet, LinkedHashSet, and TreeSet. Map implementation classes are HashMap, HashTable, TreeMap, ConcurrentHashMap, and LinkedHashMap.


1 Answers

In brief:

A list is an ordered list of objects, where the same object may well appear more than once. For example: [1, 7, 1, 3, 1, 1, 1, 5]. It makes sense to talk about the "third element" in a list. You can add an element anywhere in the list, change an element anywhere in the list, or remove an element from any position in the list.

A queue is also ordered, but you'll only ever touch elements at one end. All elements get inserted at the "end" and removed from the "beginning" (or head) of the queue. You can find out how many elements are in the queue, but you can't find out what, say, the "third" element is. You'll see it when you get there.

A set is not ordered and cannot contain duplicates. Any given object either is or isn't in the set. {7, 5, 3, 1} is the exact same set as {1, 7, 1, 3, 1, 1, 1, 5}. You again can't ask for the "third" element or even the "first" element, since they are not in any particular order. You can add or remove elements, and you can find out if a certain element exists (e.g., "is 7 in this set?")

like image 148
VoteyDisciple Avatar answered Sep 28 '22 06:09

VoteyDisciple