Difference Between List And Set

Съдържание:

Difference Between List And Set
Difference Between List And Set

Видео: Difference Between List And Set

Видео: Difference Between List And Set
Видео: #2 - Difference between List/Set/Map Based Classes (Ordering/Access/Key-Value/Duplicate/Thread-Safe) 2024, Април
Anonim

Key Difference – List vs Set

Most programming languages use arrays to store a set of data of the same type. One major drawback of arrays is that, once the array size is declared, it cannot be modified. If the programmer wants to store a values exceeding the array size, then he should create a new array and copy the existing elements to the new array. In these situations, collections can be used. It is possible to add elements, delete elements and many other operations with the support of collections. There are different types of collections available in programming languages such as Java. List and Set are interfaces of collections hierarchy. The base interface for other interfaces is Collection. The key difference between List and Set is that List supports storing the same element multiple times while Set does not support storing the same element multiple times. Therefore, a Set does not allow duplication.

CONTENTS

1. Overview and Key Difference

2. What is List

3. What is Set

4. Similarities Between List and Set

5. Side by Side Comparison – List vs Set in Tabular Form

6. Summary

What is List?

The list is an interface that extends the Collection interface. There is a number of methods in Collection interface. The add method helps to add an element. The ‘remove method’ is to remove an element. There is ‘addAll method’ to add multiple elements while ‘removeAll method’ to remove the elements from the collection. The contains method helps to find whether a specific object is present in the List or not. The ‘containsAll’ is to find whether a set of objects are present in the collection. The iterator method is used to loop through the items of the list. As List extends Collection, all the methods of Collection belong to List. Other than those methods, the List has methods such as get and set. The programmer can get a value at a specific index using get method. The programmer can set a value at a specific index using the set method. The ‘indexOf’ is used to find the index of an element.

In a List, the operations can be performed according to the position. The programmer can provide the data element that is to be added to the index. So it will be added to the specific index. If the programmer does not give an index, the element will be added to the end of the List. It also maintains the inserted order. If element 1 is added and then element2 is added, then element1 will be before element2.

Разлика между списък и набор
Разлика между списък и набор

Figure 01: List and Set

ArrayList, LinkedList, Vector are some classes that implement List. In an ArrayList, accessing an element is fast but inserting and deleting is lower. ArrayList is not thread-safe. Accessing the same ArrayList from multiple threads might not give the same result. In a LinkedList, the elements are linked to both backward and forward. Inserting and deleting elements using a LinkedList is faster than the ArrayList. The LinkedList implements List and Queue Both. Vector is similar to ArrayList, but it is tread-safe because all the methods are synchronized.

What is Set?

Set е интерфейс, който разширява интерфейса за събиране. Тъй като интерфейсът Set разширява Collection, всички методи на Collection също принадлежат на Set. Наборът не поддържа дублиране на стойности. Следователно програмистът не може да съхранява един и същ елемент два пъти. Той поддържа уникален набор от елементи. Интерфейсът SortedSet разширява интерфейса Set. SortedSet поддържа елементите в сортиран ред. Интерфейсът NavigableSet разширява SortedSet. NavigableSet осигурява навигационни методи като долна, пода, тавана и т.н.

HashSet, LinkedHashSet и TreeSet са някои класове, които реализират интерфейса Set. HashSet реализира Set интерфейс. Той не поддържа вмъкнатия ред. Ако стойностите са вмъкнати като a, x, b, той може да съхранява като, x, a, b. LinkedSet поддържа вмъкнатия ред. Ако елементите са вмъкнати в a, x, b ред, съхраняването ще бъде a, x, b. TreeSet реализира Set и NavigableSet. Той не поддържа реда на вмъкване, но съхранява елементите в сортирания ред. Ако вмъкнатият ред е a, c, b, тогава елементите ще се съхраняват като a, b, c. Всички HashSet, LinkedHashSet и TreeSet няма да имат дублиращи се елементи.

Какви са приликите между списък и набор?

  • Интерфейсите List и Set разширяват интерфейса за събиране.
  • И списък, и набор поддържат операции като добавяне, премахване на елементи.

Каква е разликата между списък и набор?

Списък срещу набор

List Interface е подинтерфейсът на Collection, който съдържа методи за извършване на операции като вмъкване, изтриване въз основа на индекса. Set Interface е подинтерфейс на Collection, който съдържа методи за извършване на операции като вмъкване, изтриване на елементи, като същевременно се запазват уникалните елементи.
Класове
ArrayList, Vector и LinkedList са класове, които изпълняват списъчен интерфейс. HashSet, LinkedHashSet и TreeSet са класове, които реализират интерфейс Set.
Дублиране на елемент
Списъкът поддържа дублиране на елементи. Комплектът не поддържа дублиране на елементи. Елементите са уникални.

Резюме - Списък срещу набор

Колекциите се използват за динамично съхраняване на елементи. Езици за програмиране като Java предоставя интерфейс за събиране. List и Set са два интерфейса, които принадлежат към интерфейса на Collection. И двата интерфейса разширяват Collection. Тази статия обсъжда разликата между Списък и Набор. Ключовата разлика между List и Set е, че List поддържа съхраняване на един и същ елемент няколко пъти, докато Set не поддържа съхраняване на един и същ елемент многократно. Комплектът винаги поддържа уникални елементи.

Препоръчано: