Skip to content

Arraylist programming problems in JAva

Arraylist scenario based questions

  1. How to remove duplicates from ArrayList in Java?
  2. How to get the first element from the ArrayList?
  3. How to get the last element from the ArrayList?
  4. How to reverse ArrayList in Java?
  5. How to loop over ArrayList in Java?
  6. How to create and initialize ArrayList in one line?
  7. How to sort ArrayList in Java?
  8. How to convert ArrayList to String in Java?
  9. How to get sublist from ArrayList in Java?
  10. How to remove objects from ArrayList in Java?
  11. How to make ArrayList read only in Java?
  12. How to sort ArrayList in descending order in Java?
  13. How to read all elements in ArrayList by using iterator?
  14. How to copy or clone a ArrayList?
  15. How to add all elements of a list to ArrayList?
  16. How to delete all elements from my ArrayList?
  17. How to find does ArrayList contains all list elements or not?
  18. How to copy ArrayList to array?
  19. How to get sub list from ArrayList?
  20. How to sort ArrayList using Comparator?
  21. How to reverse ArrayList content?
  22. How to shuffle elements in ArrayList?
  23. How to swap two elements in a ArrayList?
  24. How to convert list to csv string format?
  25. How do you increase the current capacity of an ArrayList?
  26. How do you decrease the current capacity of an ArrayList to the current size?
  27. How do you find the number of elements present in an ArrayList?
  28. How do you find out whether the given ArrayList is empty or not?
  29. How do you check whether the given element is present in an ArrayList or not?
  30. How do you get the position of a particular element in an ArrayList?
  31. How do you convert an ArrayList to Array?
  32. How do you retrieve an element from a particular position of an ArrayList?
  33. How do you replace a particular element in an ArrayList with the given element?
  34. How do you append an element at the end of an ArrayList?
  35. How do you insert an element at a particular position of an ArrayList?
  36. How do you remove an element from a particular position of an ArrayList?
  37. How do you remove the given element from an ArrayList?
  38. How do you remove all elements of an ArrayList at a time?
  39. How do you retrieve a portion of an ArrayList?
  40. How do you join two ArrayLists?
  41. How do you insert more than one element at a particular position of an ArrayList?
  42. How to find the longest palindrome from the Arraylist?
  43. Write a program to implement your own Array List in Java.
  44. How to Implement Quick sort with arrayList in Java.

 

Iterating on an ArrayList

There are many ways of traversing over an ArrayList:

Method 1 

Simply use a traditional for loop that iterates one by one on each element.

IMG-20180722-WA0011

Method 2

img-20180722-wa0012.jpg

 

Very basic questions for freshers

Question 1 : What is the package you need to import for using Arraylist in your java program?

Answer : import java.util.ArrayList

where ‘L’ in ArrayList is always capital.

Question 2 : What is the package you need to import for using Iterator for iterating over ArrayList?

Answer : import java.util.Iterator

 

 

 

 

Singleton Design Pattern

There are only two points in the definition of a singleton design pattern,

  1. there should be only one instance allowed for a class and
  2. we should allow global point of access to that single instance.

Spring MVC Architecture

Hi EveryOne

Today we will discuss about the Architecture of Spring MVC module.

Spring MVC is the web component of Spring’s framework. It provides a rich functionality for building robust Web Applications. The Spring MVC Framework is architected and designed in such a way that every piece of logic and functionality is highly configurable. Also Spring can integrate effortlessly with other popular Web Frameworks like Struts, WebWork, Java Server Faces and Tapestry.

spring-mvc-request-process-lifecycle

Spring’s web MVC framework is, like many other web MVC frameworks, request-driven, designed around a central servlet that dispatches requests to controllers and offers other functionality that facilitates the development of web applications. Spring’s DispatcherServlet is completely integrated with Spring IoC container and allows us to use every other feature of Spring.

Following is the Request process lifecycle of Spring 3.0 MVC:

  1. The client sends a request to web container in the form of http request.
  2. This incoming request is intercepted by Front controller (DispatcherServlet) and it will then tries to find out appropriate Handler Mappings.
  3. With the help of Handler Mappings, the DispatcherServlet will dispatch the request to appropriate Controller.
  4. The Controller tries to process the request and returns the Model and View object in form of ModelAndView instance to the Front Controller.
  5. The Front Controller then tries to resolve the View (which can be JSP, Freemarker, Velocity etc) by consulting the View Resolver object.
  6. The selected view is then rendered back to client.

The chapter discuss the various ways of structuring an operating system.

Chapter 02

operating systems chapter 1

Arrays in java

favourite interview question- Hashcode and equals

Hi folks,

Today we are going to deal with interview strategy of hashcode() and equals() method.

By default, these methods are present in Object class of java.lang package.

Let’s discuss these methods one by one.

public boolean equals(Object o);

The equals() method takes an Object type argument and returns a boolen false or true.

This method checks if some object is passed to it as an argument is equal to the object on which the method is invoked or not.

Default implementation of the equals() method in the Object class says: Check whether two different object references, let r1 and r2,  refer to the same object or not. This method  returns true if and only if r1 and r2 refer to the same object. This is equivalent to something like:

if(r1==r2) or not.

The default implementation performs Shallow Comparison, because the Object class has no data members that defines its state.

The implementation classes that overrides this method performs Deep Comparison, by actually comparing the relevant data members.

Equals() method follows Equivalence Relation properties that you might have studied during your school days.

Equivalence Relation follows Reflexive, Symmetric and Transitive in nature.

Let’s see what does it mean, in Java context.

Assume two object references r1 and r2,

Reflexive:  r1.equals(r1) always returns True.

Symmetric: If r1.equals(r2) returns true/false, then

r2.equals(r1) also returns true/false.

means you can use this property interchangeably

Transitivity: If r1.equals(r2) returns true and r2.equals(r3) returns true, then

r1.equals(r3) also returns true.

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

Note that it is generally necessary to override the hashCode method, if you are overriding equals() method, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hashCodes.

Relationship status of these methods on FACEBOOK.

If two objects are equal, then they must have the same hash code, however the opposite is NOT true

Synchonized keyword in java

The synchronized keyword is used with the definition of a method. This would ensure that only one thread can enter that method at the same time. Another threads which is calling this method would wait until the first threads leaves this method.