What are collections, lists etc used for in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

What are collections, lists etc used for in java

Hi ppl, can sameone pls tell me what the practical application of collections, lists, stacks are. I need to knew where n when I will hav to use them.

14th Mar 2018, 9:47 AM
mpumelelo hlongwane
mpumelelo hlongwane - avatar
6 Answers
+ 12
@Freetoast ... well put. 👨‍🎓
28th Mar 2018, 5:02 AM
Charlie Mitchell
Charlie Mitchell - avatar
+ 3
The term collections refers to the java objects that store values (List, Map, Set, etc). A true stack follows the FIFO principle. There are three thing you can do to a stack. You can put in a value on top of the stack, you can look at the top value, and you can remove the top value. You carbonyl take off the top value because for ex you have a stack of plates, if you grab the middle of the stack it will fall over. You can only take off the top one. FIFO stands for “first in first out”.
14th Mar 2018, 5:06 PM
Freetoast
Freetoast - avatar
+ 2
Yes, stacks and lists only differ in the way you can manipulate the data. Lists can actually do everything stacks can but break FIFO because they can manipulate any value of ones choosing.
14th Mar 2018, 5:15 PM
Freetoast
Freetoast - avatar
+ 1
Lists are used for storing objects while keeping the order of insertion. Lists are mutable unlike arrays so you can change the values. An example of a use of a list is: say I️ am collecting id for a database. Since there are lots of values returned it is best that they are returned in a list instead of an array because I️ might need to change them. /* Example code in java */ List<Integer> idList = Database.getIds(); /* Now I️ can add, remove, and change values */ /* Here I️ will remove the first value */ idList.remove(0); /* Here I️ will change the fifth value */ idList.set(4, 8285738); /* I️ can also easily check for values in the list. Assume value is some id*/ boolean containsValue = idList.contains(value); /* Here I️ will add a value to the end of the list */ idList.add(8372850); In java 8 streams were added making it very easy to iterate and operate in values. Stacks are used for storing objects like a stack of plates. You can put a new plate on the stack and you can take the top one off. I️ have actually had no use for stacks, but I️ am sure there are some. I️ have heard the java stack implementation is bad because it inherits from the Vector class and therefore isn’t a real stack. It is advised to make your own stack implementation for serious applications.
14th Mar 2018, 4:45 PM
Freetoast
Freetoast - avatar
+ 1
thanks this is helpful I kanda get it now, so we can say that collections are a super array. in terms of stacks iv heard the example of plates from a lot of sources but am not show I get wat those "plates" are. n y put them in a stack
14th Mar 2018, 5:01 PM
mpumelelo hlongwane
mpumelelo hlongwane - avatar
+ 1
I get it thanks a lot. so stacks also hold objects its just differs from other collections by the way u deal with the content??? 😂😁thanks for explaining wat FIFO at the end. felt like I was descending into more confusion as I was reading..
14th Mar 2018, 5:12 PM
mpumelelo hlongwane
mpumelelo hlongwane - avatar