Saturday, 25 May 2024

Interview question java practical

Take a string and remove duplicate chars.


 import java.util.stream.Collectors;

public class Main {

  public static void main(String[] args) {

    System.out.println("Hello world!");

    String myName="pawan";

    String removedname=removeDuplicates(myName);

      System.out.println(removedname);

    

  }

  public static String removeDuplicates(String str) {

      // Convert the string to a stream of characters, remove duplicates and collect back to a string

      return str.chars()

                .distinct()

                .mapToObj(c -> String.valueOf((char) c))

                .collect(Collectors.joining());

  }

}

Tuesday, 21 May 2024

MongoDB lookup example

 

To join three tables (or collections) in MongoDB, you can use the $lookup aggregation stage multiple times within an aggregation pipeline. This allows you to perform multiple left outer joins. Here’s an example scenario and how you can implement it.

Example Scenario

Let's assume we have three collections: orders, customers, and products.

orders Collection:

json
[ { "_id": 1, "order_id": "ORD001", "customer_id": 1, "product_id": 101, "amount": 500 }, { "_id": 2, "order_id": "ORD002", "customer_id": 2, "product_id": 102, "amount": 200 }, { "_id": 3, "order_id": "ORD003", "customer_id": 1, "product_id": 103, "amount": 300 }, { "_id": 4, "order_id": "ORD004", "customer_id": 3, "product_id": 101, "amount": 700 } ]

customers Collection:

json
[ { "_id": 1, "name": "John Doe", "email": "john@example.com" }, { "_id": 2, "name": "Jane Smith", "email": "jane@example.com" }, { "_id": 3, "name": "Mike Johnson", "email": "mike@example.com" } ]

products Collection:

json
[ { "_id": 101, "product_name": "Laptop", "price": 1000 }, { "_id": 102, "product_name": "Phone", "price": 500 }, { "_id": 103, "product_name": "Tablet", "price": 300 } ]

Joining the Collections

We want to create a report that includes orders along with the customer details and product details.

Aggregation Pipeline with Multiple $lookup Stages

Here’s how to use multiple $lookup stages to join the three collections:

javascript
db.orders.aggregate([ { $lookup: { from: "customers", // First join with the 'customers' collection localField: "customer_id", // Field from 'orders' collection foreignField: "_id", // Field from 'customers' collection as: "customer_info" // Name of the array field to add } }, { $unwind: "$customer_info" // Unwind the 'customer_info' array }, { $lookup: { from: "products", // Second join with the 'products' collection localField: "product_id", // Field from 'orders' collection foreignField: "_id", // Field from 'products' collection as: "product_info" // Name of the array field to add } }, { $unwind: "$product_info" // Unwind the 'product_info' array }, { $project: { order_id: 1, amount: 1, "customer_info.name": 1, "customer_info.email": 1, "product_info.product_name": 1, "product_info.price": 1 } } ])

Explanation:

  • First $lookup: Joins the orders collection with the customers collection using the customer_id field.
  • First $unwind: Deconstructs the resulting customer_info array so that each document contains a single customer object.
  • Second $lookup: Joins the resulting documents with the products collection using the product_id field.
  • Second $unwind: Deconstructs the resulting product_info array so that each document contains a single product object.
  • $project: Specifies the fields to include in the final output, creating a concise and readable result.

Result:

The result of the above aggregation will be:

json
[ { "_id": 1, "order_id": "ORD001", "amount": 500, "customer_info": { "name": "John Doe", "email": "john@example.com" }, "product_info": { "product_name": "Laptop", "price": 1000 } }, { "_id": 3, "order_id": "ORD003", "amount": 300, "customer_info": { "name": "John Doe", "email": "john@example.com" }, "product_info": { "product_name": "Tablet", "price": 300 } }, { "_id": 2, "order_id": "ORD002", "amount": 200, "customer_info": { "name": "Jane Smith", "email": "jane@example.com" }, "product_info": { "product_name": "Phone", "price": 500 } }, { "_id": 4, "order_id": "ORD004", "amount": 700, "customer_info": { "name": "Mike Johnson", "email": "mike@example.com" }, "product_info": { "product_name": "Laptop", "price": 1000 } } ]

In this result:

  • Each order document now includes the customer details (customer_info) and the product details (product_info).
  • The project stage ensures that only the relevant fields are included in the output.

Additional Tips:

  • Complex Joins: For more complex joins, you can use additional aggregation stages like $group, $match, or nested $lookup stages.
  • Optimizing Performance: Ensure that the fields used in joins are indexed to improve performance.
  • Pipeline Customization: Customize the pipeline stages to fit specific requirements, such as filtering, sorting, or additional calculations.

By using multiple $lookup stages in the aggregation pipeline, you can effectively join multiple collections and create comprehensive reports or views in MongoDB.

Monday, 20 May 2024

SSO explained

 https://www.pingidentity.com/en/resources/identity-fundamentals/authentication/single-sign-on.html

Thursday, 16 May 2024

springboot file upload example

 

@PostMapping("/admin/upload-employee-pictures")
ResponseEntity<ApiSuccess> uploadEmployeeProfilePictures(@RequestParam("file") MultipartFile file) {
Map data = new HashMap();
try {
// Get the file name
String fileName = file.getOriginalFilename();
// Get the file content
byte[] bytes = file.getBytes();
// Write the file to the upload directory
FileOutputStream fos = new FileOutputStream("upload/" + fileName);
fos.write(bytes);
fos.close();
} catch (IOException e) {
System.out.println( "Error uploading file: " + e.getMessage());
}
return new ResponseEntity<>(new ApiSuccess(ApplicationResponseCode.SUCCESS_2032.getCode(), ApplicationResponseCode.SUCCESS_2032.getDescription(), data), HttpStatus.OK);
}

Wednesday, 8 May 2024

Springboot tutorial

 https://www.knpcode.com/p/spring-boot-tutorials.html

interview question for java

 https://medium.com/@veenaraofr/core-java-interview-questions-for-experienced-5-8-years-cec119cb5c27

Tuesday, 7 May 2024

Java generic example

 https://howtodoinjava.com/java/generics/complete-java-generics-tutorial/

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static <T> T fromJson(String json, Class<T> clazz) throws Exception {
        return objectMapper.readValue(json, clazz);
    }

    public static void main(String[] args) {
        try {
            // JSON representing a student
            String jsonStudent = "{\"name\":\"John\",\"age\":20,\"grade\":12}";

            // Convert JSON to Student object
            Student student = fromJson(jsonStudent, Student.class);

            // Print student details
            System.out.println("Name: " + student.getName());
            System.out.println("Age: " + student.getAge());
            System.out.println("Grade: " + student.getGrade());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Student {
    private String name;
    private int age;
    private int grade;

    // Getters and setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }
}

Friday, 3 May 2024

Completeable feature latest link

https://codeflex.co/java-multithreading-completablefuture-explained/