What is the difference between Set and List?

2019-01-01 06:02发布

What is the fundamental difference between the Set<E> and List<E> interfaces?

标签: java list set
26条回答
与风俱净
2楼-- · 2019-01-01 06:40

List:

  1. Allowed duplicates.
  2. Ordered in grouping elements.(In other words having definite order.No need to sorted in ascending order)

Set:

  1. Not allowed duplicates.
  2. Unordered in grouping elements.(In other words having no definite order.It might or might not arranged in ascending order )
查看更多
情到深处是孤独
3楼-- · 2019-01-01 06:41

Difference based on following points

1) Duplicity: List allows duplicate elements. Any number of duplicate elements can be inserted into the list without affecting the same existing values and their indexes. Set doesn’t allow duplicates. Set and all of the classes which implements Set interface should have unique elements.

2) Null values: List allows any number of null values. Set allows single null value at most.

3) Order: List and all of its implementation classes maintains the insertion order. Set doesn’t maintain any order; still few of its classes sort the elements in an order such as LinkedHashSet maintains the elements in insertion order.

查看更多
与风俱净
4楼-- · 2019-01-01 06:43

As we are talking about the Java interfaces, why not look at the Javadoc ?!

  • A List is an ordered collection (sequence), which typically allows duplicates
  • A Set a is collection that contains no duplicate elements, iteration order may be guaranteed by the implementation

There is NO mention about lack of order concerning Sets: it depends on the implementation.

查看更多
无色无味的生活
5楼-- · 2019-01-01 06:43

All of the List classes maintain the order of insertion. They use different implementations based on performance and other characteristics (e.g. ArrayList for speed of access of a specific index, LinkedList for simply maintaining order). Since there is no key, duplicates are allowed.

The Set classes do not maintain insertion order. They may optionally impose a specific order (as with SortedSet), but typically have an implementation-defined order based on some hash function (as with HashSet). Since Sets are accessed by key, duplicates are not allowed.

查看更多
若你有天会懂
6楼-- · 2019-01-01 06:43

List:
List allows duplicate elements and null values. Easy to search using the corresponding index of the elements and also it will display elements in insertion order. Example:(linkedlist)

import java.util.*;

public class ListExample {

 public static void main(String[] args) {
    // TODO Auto-generated method stub

    List<Integer> l=new LinkedList<Integer>();
    l.add(001);
    l.add(555);
    l.add(333);
    l.add(888);
    l.add(555);
    l.add(null);
    l.add(null);

    Iterator<Integer> il=l.iterator();

    System.out.println(l.get(0));

    while(il.hasNext()){
        System.out.println(il.next());
    }

    for(Integer str : l){
        System.out.println("Value:"+str);
    }
 }

}

Output:

1
1
555
333
888
555
null
null
Value:1
Value:555
Value:333
Value:888
Value:555
Value:null
Value:null

Set:
Set isn't allow any duplicate elements and it allow single null value.It will not maintain any order to display elements.Only TreeSet will display in ascending order.

Example:(TreeSet)

import java.util.TreeSet;

public class SetExample {

 public static void main(String[] args) {
    // TODO Auto-generated method stub

    TreeSet<String> set = new TreeSet<String>();
    try {
        set.add("hello");
        set.add("world");
        set.add("welcome");
        set.add("all");

        for (String num : set) {
            System.out.println( num);

        }
        set.add(null);
    } catch (NullPointerException e) {
        System.out.println(e);
        System.out.println("Set doesn't allow null value and duplicate value");
    }

 }

}

Output:

all
hello
welcome
world
java.lang.NullPointerException
Set doesn't allow null value and duplicate value

查看更多
长期被迫恋爱
7楼-- · 2019-01-01 06:44

List Vs Set

1) Set does not allow duplicates. List allows duplicate. Based on the implementation of Set, It also maintains the insertion Order .

eg : LinkedHashSet. It maintains the insertion order.Please refer click here

2) contains method. By nature of the Set it will give better performance to access. Best case its o(1). But List has performance issue to invoke contains.

查看更多
登录 后发表回答