This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

The problem

What is the fundamental difference between the Set

and List

interfaces?

answer

Answer 1

List is an ordered List, while Set is an unordered List.

List<E>:

Ordered collections, also known as sequences, use this interface to precisely control where each element in a list is inserted. Elements can be accessed by their index (position in the list) and searched for elements in the list.

Set<E>:

A collection that does not contain repeating elements. More precisely, the collection does not contain elements E1 and e2, making e1.equals (e2), and contains at most one empty element.

Answer 2

An ordered List (unique or not) that implements the interface List and can be accessed through an index.

Implementation:

  • LinkedList
  • ArrayList

A list of unique elements that implement the interface Set and can be accessed without an index.

Implementation:

  • HashSet(disorder)
  • LinkedHashSet(in order)
  • TreeSet(Sort by natural order or comparator provided)

The two interfaces, Set and List, are implemented for Collection.

Answer 3

The List:

  1. Is an ordered grouping of elements.
  2. Used to collect elements with duplicates.
  3. There are new methods defined in the List interface.

Set:

  1. Is an unordered grouping of elements.
  2. SetUsed to collect elements that are not duplicated.
  3. SetThere are no new methods defined inside the interface, so we just need theSetUse a subclassCollectionInterface methods.

Answer 4

The List:

List allows duplicate elements and null values. Using the corresponding index of the element is easy to search, and it displays the 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); }}}Copy the code

Output:

1 1 555 333 888 555 NULL Null Value: 1 Value: 555 Value: 333 Value: 888 value: 555 Value: NULL Value: NULLCopy the code

Set:

The Set does not allow any duplicate elements and allows a single null value. It does not guarantee any order in which elements are displayed. Only TreeSet can be displayed 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"); }}}Copy the code

Output:

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

Translation content sources Stack Overflow:stackoverflow.com/questions/1…