Created
May 25, 2015 23:03
-
-
Save nijjwal/4d4614f561e3e18b0f9a to your computer and use it in GitHub Desktop.
java collections for interview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Why do we need collection framework? | |
- If you want to represent 10,000 values, then declaring 10,000 variables | |
to represent those 10,000 values is worst programming practice. | |
Because readability of code is going to be hard. To overcome this problem | |
we should go for the next level - array concept. The biggest advantage of | |
array is we can represent huge number of values by using a single variable. | |
So, the readabilit of code is going to improve. Suppose, arrays concept is | |
not there, then we should go for the individual variables. To represent | |
10,000 values we will need 10,000 variables, then the readability of code | |
is going to go down. | |
2. Limitations of array: | |
i. Arrays are fixed in size | |
- To use an array we must know the size in advance, which might not be possible | |
always. For example, If I declare that there will be 20 students for a presentation, | |
but only 2 showed up during the runtime, then 18 seats are wasted. | |
ii. Arrays can hold only homogeneous data elements | |
Student[] s = new Student[1000]; | |
s[0] = new Student(); | |
s[1] = new Customer(); //compile time error : incompatible type found: Customer required: Student | |
Solution: | |
If you want to store any type of object, then create an array of Object | |
Object[] a = new Object[1000]; | |
a[0] = new Student(); //valid | |
a[1] = new Customer(); //valid | |
iii. No underlying data structure | |
Array concept is not implemnted based on some standard data structure, but every collection class is implemented | |
based on some data structure. Underlying data structure is not there for arrays.Ready made methods we | |
cannot expect for arrays. | |
For example searching and sorting methods are not available. | |
contains method is not available in array. | |
3. Why do we need collections? | |
To overcome these problems, next level we should go for is collections. | |
Collections feature: | |
i. Collections are growable in nature | |
ii. Collections can hold both homogeneous and heterogeneous objects. | |
iii. Every collection class is implemented based on some standard data structure. | |
Ready made method support is available. | |
+-------------------------------------------------------+ | |
|Collections - Difference between Arrays and Collections| | | |
| | | |
+-------------------------------------------------------+ | |
1. Which concept is recommended to use array or collection? | |
- If you know the size in advance, it is highly recommended to go for arrays concept | |
only. In collections we are getting growable nature, that growable nature we are not | |
getting free of cost. We have to pay something, because of that we are getting growable | |
nature. So, when we are growing what are we paying for performance. Performance is a very crucial | |
or critical thing. | |
For example, | |
Suppose we have an array of size 10. I inserted 1st,2nd,3rd, 4th element and so on. | |
If 11th elements comes, then immediately array will tell I can't provide support. | |
But assume if it is array list, now if the 11th element comes, then array list can | |
provide support. How this 11th element is going to be inserted? | |
Don't feel that another cell will be created and within that cell the 11th element will | |
be inserted. Once an arraylist reaches its max capacity, a bigger arraylist is going to be created | |
internally. All these 10 elements will be copied. After copying the 11th element will be inserted. | |
And re-assigned the reference variable to the new object. And the old object is ready for garbage | |
collection. | |
Suppose, existing arraylist consists of 1 million objects, then to add 1millionth 1 elemen , there | |
will be 1 million copies. Suppose if you are telling could you please add one element, then after | |
six months or 1 year, it will tell successfully inserted. That's why collections are not for the | |
performance. | |
If you know the size in advance, it is highly recommended to go for the array and not for collections. | |
2. Differences between arrays and collections(list, set, map): | |
Arrays Collections | |
--------------------------------------***------------------------------------ | |
1. Array are fixed in size. | 1. Collections are growable in nature. | |
2. Memory wise arrays are not | 2. With respect to memeory collections | |
recommende, because during creation | are recommended to use. Because based | |
time if you created an array of size | on requirement we can increase or decrease | |
1000, but during runtime you only | the size, so only two memebers came. | |
filled 2 spaces with some usable | 3. With respect to performance, collections | |
values, then the remaining 998 | are not recommended. | |
spaces will be wasted. | | |
3. With respect to performance, array | | |
are too good. | | |
4. Arrays can hold only homogeneous | 4. Collections can hold both homogeneous and | |
data. | heterogeneous data. | |
5. Arrays do not contain underlying | 5. Every collection class is implemented based | |
data structure, so ready made methods | on some standard data structure. So, for every | |
are not available. | collection, ready made method support is available. | |
6. Arrays can hold both primitivies | 6. Only objects we can hold, but primitives we cannot | |
and objects. | hold. | |
3. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Initial commit. Chapter 1 and 2.