Saturday, September 21, 2019

Collection Interview Questions (With Java 8 Enhancements) - Part 2 List Interface

1. List Interface - Specialty & implementations.

List is an Ordered collection of Objects.It can have duplicates as well. In addition to the methods available from collection interface, it also supports following operations :

  • positional access - get,set,remove,add,addall.. etc
  • search - indexOf, lastIndexOf
  • iteration - listIterator
  • range view- sublist
Implementations - ArrayList(better performing),LinkedList

Most of the polymorphic algorithms in Collections class supports list.Algorithms like - sort,reverse,shuffle,rotate,binarySearch etc.


polymorphic algorithms : In general, an algorithm is called polymorphic if it can achieve the same functionality using different data structures

2. How to convert an array of String to arraylist?

Arrays.asList(1,2,3);

3. Do List maintain Insertion Order?

Yes. By Definition, Lists are ordered collection of objects.So insertion order is maintained by default.

4. Collection operation

List<String> list = people.stream()
.map(Person::getName)
.collect(Collectors.toList());

4. Can List Store null values?


Yes. 

List<String> a = new ArrayList<>();
  List<String> b = new LinkedList<>();

  System.out.println(a);
  System.out.println(b);

Output:

[null] 
[null]
5. Difference between Vector and ArrayList?
6. Difference between ArrayList and LinkedList?
7. 

No comments:

Post a Comment