Can every set be transformed into a list Can every list be t

Can every set be transformed into a list? Can every list be transformed into a set? Explain your reasoning, including the characteristics which differentiate sets from lists.

Solution

Note
You have not mentioned the specific programming language for answer. I have Choose JAVA

a)YES every set can be transformed into a list because set will never allows duplicate value if some tried too it will replace older value.Set is unordered collection . conversion will not elliminate any data as data is unique.
For conversion from Set to List in java there is List.addAll() method .It accepts a collection as as argument and your set is a Collection
eg
mylist.addAll(set);

we can use constructor to covert
List<?> list=new ArrayList<?>(set);

Guava collect Library
Lists.newArrayList([set])

b)No every List cannot be Conveted into Set as we know list is ordered set of duplicate items ,if some tries to convert list into set when list contains multiple copies of some elements then all those copies except one will not make it into set .
If some one still want to convert list to set then
there is straight forward conversion
List list=new ArrayList(set)

c)

Duplicate Objects-
List allows duplicates while set doesn\'t allow duplicates .

Order-
List is an ordered collection ,it maintains insertion order or element.
Set is an unordered collection ,user cannot keep record which order element will be sorted.Though some of the set implementation like LinkedHashSet maintain order ,Also Sortedset and SortedMap

Null Elments-
List allows null elements and we can have many of them because it allows duplicates .
Set just allows one null element

Implementation-
List interface can be implemented using ArrayList,LinkedList and Vector class
Set interafec can be implemented using Hashset,LinkedHashset and Treeset

When To use-
If you need to access elements by using index frequently then use List or If you want ot store elements and maintain an order on which they are insertted into collection then use List.

If you want to create collection of unique elemetns and dont want duplicate then choose set or If you store data in form of key and value than choose Set.

 Can every set be transformed into a list? Can every list be transformed into a set? Explain your reasoning, including the characteristics which differentiate s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site