what is a collection in Java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what is a collection in Java?

collection

22nd Aug 2016, 6:07 AM
yasmin
3 Answers
+ 1
its an array, just better. it can store any data type, and has generic implementation. it can be dinamically modified, and easier to use and acces. A simple example: java.util.Collections ArrayList<Integer> numbers = new ArrayList<Integers>(); for (int i = 0; i <=10;i++){ numbers.add(i); } now we can just delete everything that can be divided by 2: for(int j = 0; i < numbers.size; i++){ if(i % 2 == 0){ numbers.remove(i); } } now, to sort them, just use Collections.sort(List, Comparator) where List is the list to be sorted, and the Comparator to be used as reference. this is just the basic stuff, i like to use these. If you have to replace multi dimensional arrays, try Maps there. you can find their methods on oracle docs, i just shared the basics.
22nd Aug 2016, 8:23 AM
Molnár Gábor
Molnár Gábor - avatar
+ 1
collection is dynamic storage structure which is used store homogeneous and heterogeneous elements
6th Nov 2016, 7:07 PM
ANAND ALABAL
ANAND ALABAL - avatar
0
Collections are use in java to store, collect and manipulate data easily. you can perform actions on data like searching, sorting, update, insert and delete using collections.
25th Aug 2016, 7:13 PM
Pradeep Saini
Pradeep Saini - avatar