+ 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.
6 Answers
+ 12
@Freetoast ... well put. šØāš
+ 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ā.
+ 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.
+ 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.
+ 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
+ 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..