Home » Collection

Collection

Last Updated on July 9, 2023 by KnownSense

41. Explain important methods of LinkedList?

MethodDescription
boolean add( Object o)It is used to append the specified element to the end of the LinkedList.
boolean contains(Object o)It a method that returns true if this list contains the specified element.
void add (int index, Object element)Inserts the element at the specified element in the vector.
void addFirst(Object o)It is used to insert the given element at the beginning.
void addLast(Object o)It is used to append the given element to the end.
Int size()This method can be used to return the total number of elements in a list.
boolean remove(Object o)It can remove the first occurrence of the specified element from this list.
int indexOf(Object element)This Java method returns the index with the first occurrence of the mention element in this list, or -1.
int lastIndexOf(Object element)It is a Java method that returns the index with the last occurrence of the specified element in this list, or -1.

42. Explain for-each loop with example?
For-Each Loop is another form of for loop used to traverse the array. It reduces the code significantly, and there is no use of the index or rather the counter in the loop.
Example of for each loop:
class ForEachExplain {
  public static void main(String[] args) {
    String[] arrData = {"A", "B", "C", "D", "E"};
    //The conventional approach of using the for loop
    System.out.println("Using conventional For Loop:");
    for(int i=0; i< arrData.length; i++){
      System.out.println(arrData[i]);
    }
    System.out.println("\nUsing Foreach loop:");
    //The optimized method of using the for loop - also called the foreach loop
    for (String strTemp : arrData){
      System.out.println(strTemp);
    }
  }
}

43. What is diamond Operator?
Diamond operator enables the compiler to collect the type arguments of generic class. In Java SE, developer can substitute the parameterized constructor with an empty parameter sets (<>) known as diamond operator.

44. What is map.entry in Map?
Map.entry is a Java interface of java.util. It has a nested interface in Map. This interface must be qualified by the name of class or interface, which it is a member. Therefore it is qualified as a Map. Entry. It represents a key and value pair that can forms element of a Map.
This method returns a view of the collection. For example, consider cityMap as a map. The developer can use entrySet() to get the set view of map having an element Map.Entry. Programmer can also use getKey() and getValue() of the Map.Entry to get the pair of key and value of the map.

45. How ArrayList performance can be measured?
The performance of ArrayList can be measure by:

  • Adding an element: Developer can add an element at the end of ArrayList using add(E e) method. It is O(1). In the worst scenario, it might go to O(n). This can happen if the developer add more elements than the array capacity.
  • Retrieving an element: Developer can access the array index using get(int index). The performance, in this case, can be measure using ArrayList get() is O(1).
  • Removing an element: In case, if the developers are removing element using the remove(int index), then the performance of ArrayList can be calculated using said remove(int index) operation is O(n – index) method.

46. How to iterate a map?
The developer cannot directly iterate map, but, this interface has two methods that gives view set of map. These methods are:

  • Set<Map.Entry<K, V>>entrySet(): It is a method that returns a set having the entries mention in the map. These entries are generally objected, which has type Map. Entry.
  • Set<K>keySet(): This Java method returns a set that having the map key.

47. What is Big-O?
The Big-O notation depicts the performance of an algorithm as the number of elements in ArrayList. A developer can use Big-O notation to choose the collection implementation. It is based on performance, time, and memory.
For example, ArrayList get(index i) is a method to perform a constant-time operation. It does not depend on the total number of elements available in the list. Therefore, the performance in Big-O notation is O(1).

48. Explain types of Queues in java?
There are three types of queues in Java:

  • Priority queue: It is a special type of Queue wherein elements are sorted as per their natural ordering or custom comparator.
  • Circular Queue: It is a type of Queue in which user operations are performed based on the FIFO method. The last element is connected to the first position in order to make a circle.
  • Double-ended Queue: A double-ended queue is an abstract data type that generalizes a queue. The elements in this queue can be added or removed from either head or tail.

49. What are the advantages of Stack?
The advantages of the stack are:

  • It helps you to manage the data in a Last In First Out (LIFO) method, which is not possible with the Linked list and array.
  • When a function is called, the local variables are stored in a stack, and it is automatically destroyed once returned.
  • A stack is used when a variable is not used outside that function.
  • It allows you to control how memory is allocated and deallocated.
  • Stack automatically cleans up the object.
  • Not easily corrupted
  • Variables cannot be resized
LinkedList

50. What does Collections.emptySet() return?
This method returns a serializable set that is empty and immutable.

Authored by codingknownsense.com

Scroll to Top