Wednesday, 11 June 2025

Java Interview Question : JAVA Collection vs. Collections

 

java.util.Collection 

What it is: Collection is an interface that represents the root of the collection hierarchy in the Java Collections Framework. It defines the common behavior for a group of objects, known as its elements.

Purpose: It acts as a blueprint for all concrete collection types (like Lists, Sets, and Queues) that store individual elements. It specifies the basic operations that all collections should support.

import java.util.*;

public class CollectionExample {
    public static void main(String[] args) {
        Collection<String> names = new ArrayList<>(); // ArrayList implements List, which extends Collection
        names.add("Akshay");
        names.add("Ajay");
        names.add("Abhay");

        System.out.println("Collection elements: " + names); // Output: [Akshay, Ajay, Abhay]
       
        names.remove("Akshay");

        System.out.println("After removing Akshay: " + names); // Output: [Ajay, Abhay]

        System.out.println("Size: " + names.size()); // Output: 2
    }
}

java.util.Collections 

What it is: Collections is a utility class that provides static methods that operate on or return collections. It's like a helper class for the Collection interfaces and their implementations.

Purpose: It offers various polymorphic algorithms and utility methods that can be applied to Collection objects (or their sub-interfaces like List and Set). These methods include sorting, searching, shuffling, reversing, and creating thread-safe or unmodifiable versions of collections.

import java.util.*;

public class CollectionsExample {
    public static void main(String[] args) {

        List<Integer> numbers = new ArrayList<>();

        numbers.add(5);
        numbers.add(2);
        numbers.add(8);
        numbers.add(1);

        System.out.println("Original List: " + numbers); // Output: [5, 2, 8, 1]

        // Using Collections.sort()
        Collections.sort(numbers);
        System.out.println("Sorted List: " + numbers); // Output: [1, 2, 5, 8]

        // Using Collections.reverse()
        Collections.reverse(numbers);
        System.out.println("Reversed List: " + numbers); // Output: [8, 5, 2, 1]

        // Using Collections.max()
        System.out.println("Maximum element: " + Collections.max(numbers)); // Output: 8

        // Creating an unmodifiable list
        List<String> immutableList = Collections.unmodifiableList(List.of("apple", "banana"));

        // immutableList.add("orange"); // This would throw UnsupportedOperationException
        System.out.println("Immutable List: " + immutableList); // Output: [apple, banana]
    }
}

Monday, 10 March 2025

Java 8 Stream API MCQ for Interview

 Question: Which of the following operations is a terminal operation in Java 8 Stream API?

Options:

A. map()
B. filter()
C. collect()
D. flatMap()
Answer:
C. collect()
Explanation:
The collect() method is a terminal operation in the Stream API, used to transform the elements of a stream into a collection, list, or another data structure. Terminal operations produce a result or a side-effect and close the stream.

Thursday, 5 September 2024

What is the purpose of @SpringBootApplication annotation?

                                         JAVA with Silan Software

@SpringBootApplication annotation represent that the corresponding application is a spring boot application. It is working for three annotations like @Configuration, @EnableAutoConfiguration, and @ComponentScan.

@Configuration : It is a class level annotation indicating that an object is a source of bean definitions. @Configuration classes declare beans through @Bean annotated method.

@EnableAutoConfiguration : It enables to spring boot to auto-configure the application context. So it automatically creates and registers beans based on both the included jar files in the class path and the beans defined by us.

@ComponentScan : It is used for auto detecting and registering spring managed components like beans, controllers, repository, services etc.

Java Spring Boot First Example in Spring Tool Suite(STS)

 

JAVA with Silan Software

First Spring Boot Application in STS

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

         <modelVersion>4.0.0</modelVersion>

         <parent>

                  <groupId>org.springframework.boot</groupId>

                  <artifactId>spring-boot-starter-parent</artifactId>

                  <version>3.3.3</version>

                  <relativePath/> <!-- lookup parent from repository -->

         </parent>

         <groupId>com.iter</groupId>

         <artifactId>SpringBootFirstExample</artifactId>

         <version>0.0.1-SNAPSHOT</version>

         <name>SpringBootFirstExample</name>

         <description>My First Spring Boot Example</description>

         <url/>

         <licenses>

                  <license/>

         </licenses>

         <developers>

                  <developer/>

         </developers>

         <scm>

                  <connection/>

                  <developerConnection/>

                  <tag/>

                  <url/>

         </scm>

         <properties>

                  <java.version>17</java.version>

         </properties>

         <dependencies>

                  <dependency>

                           <groupId>org.springframework.boot</groupId>

                           <artifactId>spring-boot-starter-web</artifactId>

                  </dependency>

 

                  <dependency>

                           <groupId>org.springframework.boot</groupId>

                           <artifactId>spring-boot-devtools</artifactId>

                           <scope>runtime</scope>

                           <optional>true</optional>

                  </dependency>

                  <dependency>

                           <groupId>org.springframework.boot</groupId>

                           <artifactId>spring-boot-starter-test</artifactId>

                           <scope>test</scope>

                  </dependency>

         </dependencies>

 

         <build>

                  <plugins>

                           <plugin>

                                    <groupId>org.springframework.boot</groupId>

                                    <artifactId>spring-boot-maven-plugin</artifactId>

                           </plugin>

                  </plugins>

         </build>

 

</project>

 

TestController.java

package com.soa.controller;

 

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

 

@RestController

public class TestController {

        

         @GetMapping("/hello")

         public String display()

         {

                  return "Welcome to Java Spring Boot";

         }

        

 

}

 

 




Thursday, 9 March 2023

JAVA program to find the reverse of a string value and check whether the given string is palindrome or not !

 //JAVA program to find the reverse of a string value and check whether the given string is palindrome or not

import java.util.*;

class ReverseString

{

public static void main(String[] args)

{

String str,rev="";

int l;


Scanner s=new Scanner(System.in);

System.out.println("enter the string value:");

str=s.nextLine();


l=str.length();

for(int i=l-1;i>=0;i--)

{

rev=rev+str.charAt(i);

}


System.out.println("The reverse is:"+rev);


if(str.equals(rev))

{

System.out.println("palindrome");

}

else

{

System.out.println("not palindrome");

}

}

}

Output : 






Wednesday, 10 August 2022

JAVA 8 Interview Question-2022 : What is Optional and why it is used ?

 Optional in JAVA 8:

Optional is a built in class present in java.util package also called as container class specifically used to represent optional values that exist or do not exist.

The main advantage is to avoid null. Here there is no NullPointerException will arise at the run time in the form of result.


Note: For getting JAVA, Python, Machine Learning, Deep Learning tutorial in detail, follow:

www.pythontpoints.com


                               -----------------------------Thank You----------------------------



Regards:

Trilochan Tarai  | Associate Manager, Delivery, Majesco India 

+91-9439202111

JAVA Interview Questions-2022 : JAVA 8 new Features !

 JAVA 8 newly Features:

1. Lambda Expression

2. Functional Interface

3. Method References

4. Stream API

5. Date Time API

6. Method References

7. Optional class present in java.util package

8. Nashorn : Highly improved JavaScript engine which enables the execution of JavaScript in JAVA.


                             -------------Thank You----------------- 


Regards:

Trilochan Tarai | Associate Manager, Delivery, Majesco India

+91-9439202111