Reasons to use the package in Java

I am currently studying algorithms and data structures, and when I read the 4th edition of the Book of Algorithms, I discovered a data structure Bagalong with Stackand Queue. After reading the explanation for this, I'm still not clear what would you prefer to use Bag(which has no method remove()) as compared to other data structures, such as Stack, Queue, LinkedListor Set? As far as I understand from the book, the implementation Bagis the same as for Stack, just replacing the name push()with add()and deleting the method pop().

Thus, the idea Bagconsists mainly in the fact that it is able to collect objects, and then sort through the collected objects, check whether the bag is empty and find the number of elements in it. But under what circumstances would I rather use Bagone of the collections mentioned above? And why does Bagn't it have a method remove()in principle? Is there a specific reason for this?

Thanks in advance.

+4
source share
1 answer

Stack - this is an ADT collection of elements with a specific remote order = LIFO (last-in-first-out), allows you to duplicate,

Queue - this is an ADT collection of elements with a specific remote order = FIFO (first-in-first-out), allows you to duplicate,

LinkedList is an implementation of a list,

Set - ADT , ,

Bag - ADT , .

, , , Collection. , , Bag, Set. , , List. , ( ), Queue. , ( ), Stack.

: Java LinkedList - , , , , , , (add ~ addLast ~ push, peekLast, removeLast ~ pop), . , Stack, , peek Queue , ( ). LinkedList " " Deque.

Bag remove(Object) , e. . Bag, . get(int) . get(int) e. . Bag , O (n/2), - (-) , O ( 1).

Bag , . , .

, , Set Bag. , , Stack Queue, Bags . Bag Stack Queue, api .

- ( + ). , Bag, .

+8

Source: https://habr.com/ru/post/1674881/


All Articles