Interview Questions


Java 8 Steams - Example

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

 * Using Java 8 Steams to process collections and perform SQL like operations with Collections.
public class StreamOps {

        public static void main(String[] args){
                Item item1 = new Item(1,"Prod1",Status.NEW);
                Item item2 = new Item(2,"Prod2",Status.ORDERED);
                Item item3 = new Item(3,"Prod3",Status.NEW);
                Item item4 = new Item(4,"Prod4",Status.SHIPPED);
                Item item5 = new Item(5,"Prod5",Status.NEW);

Hystrix - Multiple Serivce Instance Endpoints

        The following code example implements Hystrix Command for multiple service endpoints.
Assume there are two service endpoints (different hosts/port) providing same service. In this case we would have two hystrix command to call the service at both endpoints. The following code chooses the endpoint(hystrix command) on round robin fashion . Each command will have its own circuit breaker. The properties are configured using archaius.

Java - Lambda Expressions

package com.test;

public class Lambda {

        public static void main(String[] args) {
                Runner r = new Runner();
      , y) -> {
                        System.out.println("Lambda expression executed");
                        System.out.println("hello world");
                        return 5 + x;

      , y) -> {

                        return 5 - x;

      , y) -> 5 * x);

      , y) -> 5 * x + y);


interface Executable {

        int execute(int x, int y);

class Runner {

        public void run(Executable e) {
                System.out.println("inside run method");

Hystrix - Example

package com.test.hystrix;



public class Example {

        public static void main(String[] args){

 class CommandHelloWorld extends HystrixCommand<String> {

    private final String name;

    public CommandHelloWorld(String name) {

Java - Copy 1000 files from one directory to another

import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class FileHandler {

        public static void main(String[] args) throws Exception {
                String soursePath = "C:/logs";

                int threadCount = 25;
                List<File> files = getFileList(soursePath);
                List<File> threadFiles = new ArrayList<>();

Servlet Filter - Redirect Session Timeout

Use the following to code to override the doFilter method, which redirects to login page on session timeout. The web.xml should be configured to use this filter.

        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
                String ipAddress = request.getRemoteAddr();
                System.out.println("IP " + ipAddress + ", Time " + new Date().toString());

                boolean authorized = false;
                String username = null;
                String role = null;
                if (request instanceof HttpServletRequest)

Spring MongoDb Example

package com.test;

import java.util.List;


import com.mongodb.MongoClient;

public class MongoSpringTest {

        private static String MONGO_HOST = "localhost";
        private static int MONGO_PORT = 27017;
        public static final String DB_NAME = "employeeDb";

Spring - Test Rest API using RestTemplate

package com.test;

import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.web.client.RestTemplate;

public class TestRestAPI {

            public static final String SERVER_URI = "http://localhost:8080/SpringTest-0.0.1-SNAPSHOT";
            public static void main(String args[]){
            private static void testGetAllEmployee() {
                RestTemplate restTemplate = new RestTemplate();

JPA - Pagination Searching and sorting

The following code can be used for the following requirements

1.) To retrieve limited records to support pagination
2.) To support pagination searching & sorting

        public List<Employee> listEmployee(int fromValue, int pSize,
                        final String search, final String sort, final String filter,
                        final String order) {

                final CriteriaBuilder criteriaBuilder = entityManager
                final CriteriaQuery<Employee> criteriaQuery = criteriaBuilder

JPA - Pagination Query

Use the following query to fetch records based on limit for Pagination using JPA

public List<Employee> getEmpWithPagination(int fromValue,int toValue)
                return entityManager.createQuery("from Employee", Employee.class).setFirstResult(fromValue).setMaxResults(toValue).getResultList();
                }catch(final Exception e){
                        log.debug("Exception Occured -> "+ExceptionUtils.getFullStackTrace(e));
                        return null;