Posts

Spring Security

 Spring Security is a powerful and customizable authentication and access-control framework for Java applications. It provides a comprehensive suite of tools for securing applications, focusing on both authentication (verifying the identity of a user) and authorization (determining what resources the user can access). Key Concepts in Spring Security Authentication : UserDetailsService : An interface used to load user-specific data. It provides a method loadUserByUsername(String username) that returns a UserDetails object. AuthenticationManager : The central interface for authentication in Spring Security. It authenticates a given Authentication object. AuthenticationProvider : A component that performs authentication logic. It typically uses a UserDetailsService to load user data and verify credentials. SecurityContext : Holds the Authentication object, which contains the principal (user), credentials, and granted authorities (roles/permissions). Authorization : GrantedAuthori...

Basics Of sample Collection

 // Online Java Compiler // Use this editor to write, compile and run your Java code online import java.util.*; class Main {     public static void main(String[] args) {              }     void sample1() {           List<String> games = new ArrayList<>();         games.add("Batminton");         games.add("Cricket");         games.add("Batminton");         games.add("Football");         System.out.println(games);                  Set<String> favouriteGames = new HashSet<>(games);         System.out.println(favouriteGames);      }    void convertArrayToList(int[] integers) {     System.out.println(Arrays.asList(integers)); }

Java Program to Calculate Total Income from a Collection of Room Objects

import java.util.*; class Main {     public static void main(String[] args) {        Room a = new Room("Delux", "enSuite", 4, 550);        Room b = new Room("Single", "Single", 1, 150);        Room c = new Room("Double", "Double", 2, 250);        Room d = new Room("Premier", "Premier", 4, 550);        Collection<Room> rooms = List.of(a, b, c, d);        System.out.println(calculateTotalIncome(rooms));     }     static double calculateTotalIncome(Collection<Room> rooms) {         return rooms.stream()         .mapToDouble(r -> r.getRate())   // .mapToDouble(Room::getRate)         .sum();     } } class Room {     String name;     String type;     int capacity;     double rate;    Room(String name, ...

Implement an immutable collection holder

  To implement an immutable collection holder, you need to ensure that the list of strings it holds cannot be modified after it is assigned. This can be done by creating an unmodifiable copy of the list in the constructor and returning an unmodifiable view of the list in the getter method. Using Collections.unmodifiableList is a common way to achieve this in Java. import java.util.List; import java.util.ArrayList; import java.util.Collections; public class Main {     public static void main(String[] args) {         List<String> list = Arrays.asList("apple", "banana", "cherry");         ImmutableCollectionHolder holder = Answer.createImmutableCollectionHolder(list);         System.out.println("Immutable list: " + holder.getItems());         // Try to modify the list through the getter         try {             holder.getItems().add("date"...

Filter and sort with a stream

 A method filters a list of Item objects based on a maximum price and then sorts the filtered items by price. Here's a summary of what the method does: Filter : It filters the items where the price is less than maxPrice . Sort : It sorts the filtered items by their price in ascending order. Collect : It collects the sorted items into a list. Here is the code with comments and a main method added for testing: import java.util.List; import java.util.stream.Collectors; class Answer {     // Change these boolean values to control whether you see     // the expected answer and/or hints.     static boolean showExpectedResult = true;     static boolean showHints = false;     // Filter and sort the items array.     static List<Item> getFilteredData(List<Item> items, float maxPrice) {         return items.stream()                     .filter(item -...

Sort a list with a lambda expression

   The Java code snippet shows a class   Answer   with a method   sortList   that sorts a list of   Item   objects based on their price using a lambda expression.  Here's the full code with some explanations and a test example added: import java.util.ArrayList; import java.util.List; class Answer {     // Change these boolean values to control whether you see     // the expected answer and/or hints.     static boolean showExpectedResult = false;     static boolean showHints = false;     // Return the largest number in the 'numbers' array.     static List<Item> sortList(List<Item> items) {         List<Item> result = new ArrayList<>(items);         // Sort the result list using a lambda expression.         result.sort((a, b) -> Float.compare(a.getPrice(), b.getPrice()));         ...

Do Math With Lambda

To create a map of mathematical operations using lambda expressions in Java, you need to define BiFunction instances for each operation and use these to populate the results map. Here is the completed code: import java.util.HashMap; import java.util.Map; import java.util.function.BiFunction; class Answer {     // Change these boolean values to control whether you see     // the expected answer and/or hints     static boolean showExpectedResult = false;     static boolean showHints = false;     // Create constants representing the four available math functions     public static final String ADD = "ADD";     public static final String SUBTRACT = "SUBTRACT";     public static final String MULTIPLY = "MULTIPLY";     public static final String DIVIDE = "DIVIDE";     // Do mathematical calculations using lambda expressions     public static Map<String, Float> calculate(float va...