Tuesday, August 30, 2022

prg

 package com.java.util;


import java.util.*;
import java.util.stream.Collectors;

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

List<Country> items = Arrays.asList(
new Country("India", 1700000),
new Country("India", 5000000),
new Country("USA", 400000),
new Country("USA", 800000),
new Country("Germany", 300000)
);

// Group by CountryName and calculates the count
Map<String, Optional<Country>> counting = items.stream().collect(
Collectors.groupingBy(Country::getName, Collectors.maxBy(Comparator.comparing(Country::getPopulation))));

for (Map.Entry<String, Optional<Country>> item : counting.entrySet()) {
Country country = item.getValue().get();
System.out.println(country.getName());
System.out.println(country.getPopulation());

for (Country co : items) {
if (country.getName().equals(co.getName())) {
co.setPopulation(country.getPopulation());
}
}
}

items.forEach(System.out::println);

}
}


package com.java.util;


public class Country{

String name;
long population;

public Country() {
super();
}
public Country(String name,long population) {
super();
this.name = name;
this.population=population;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getPopulation() {
return population;
}
public void setPopulation(long population) {
this.population = population;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((name == null) ? 0 : name.hashCode());
result = prime * result + (int) (population ^ (population >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Country other = (Country) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (population != other.population)
return false;
return true;
}

public String toString()
{
return "{"+ name +","+population+"}";
}
}

Sunday, August 21, 2022

@Component vs @Bean

 

@Component is suitable for component scanning and automatic wiring.

Sometime an automatic configuration is not an option. When?

Let’s imagine that you want to wire components from 3rd party libraries (you don’t have source code so you can’t annotate its classes with @Component), so automatic configuration is not possible.

The @Bean annotation returns an object that spring should register as bean in application context. The body of the method bears the logic responsible for creating the instance.

Disable CORS in Spring Security

 If you want to disable CORS in spring security, then below is a way to do so.





Spring Stereotype Annotations

 

The Spring framework provides you with some special annotations. These annotations are used to create Spring beans automatically in the application context. The stereotype annotations in spring are @Component, @Controller, @Service & @Repository.


The main stereotype annotation is @Component.

“Annotations denoting the roles of types or methods in the overall architecture (at the conceptual, rather than implementation, level)” from java docs.

 

There are some stereotype meta-annotations which is derived from @Component annotation.



Meta Annotation: - Generally in java, an annotation is termed as meta-annotation if it is used on another annotation.

 

@Controller: - Used to create spring beans at the Controller layer.

@Service: - Used to create spring bean at the Service Layer. We mark beans @Service to indicate that they are holding business logic. Besides being used in the service layer, there is not any special use for this annotation.

@Repository: - Used to create a spring bean at the Persistence Layer, which will act as a database repository at the DAO layer. Its job is to catch persistence-specific exceptions and re-throw them as one of the Spring’s unified unchecked exceptions.


To detect these beans automatically, spring uses classpath scanning annotations.

Then it registers each bean in the ApplicationContext.

The major difference between these stereotypes is that they are used for different classifications. When we annotate a class for auto detection, we should use the respective stereotype.



For more info read the Spring doc here.


Saturday, August 20, 2022

Java program -- WIP

  public static void main(String[] args) {


// Input:
// N = 4, arr[] = [1 3 2 4]
// Output:
// 3 4 4 -1
// N = 4, arr[] = [1 3 2 4]
long start = System.currentTimeMillis();

int N = 4;
long arr[] = {1, 3, 2, 4};
long[] res = nextLargerElement(arr, N);
for (long re : res) {
System.out.println(re);
}

long end = System.currentTimeMillis();

long total = end - start;
System.out.println(total);
}

//Function to find the next greater element for each element of the array.
public static long[] nextLargerElement(long[] arr, int n) {
long[] res = new long[arr.length];
for (int i = 0; i < arr.length; i++) {
long temp = arr[i];
long[] subArr = Arrays.copyOfRange(arr, i, arr.length);
long biggerEl = -1;
innerloop:

for (long lo : subArr) {
if (lo > temp) {
biggerEl = lo;
break innerloop;
}
}

// while (lo > temp){
//
// }


res[i] = biggerEl;
}
return res;
}

public static long getBiggerElement(int i, long[] arr) {
long temp = arr[i];
// long[] tempArray = Arrays.copyOfRange(arr, i, arr.length);
long[] subArr = new long[arr.length - i];
for (int a = 0; a < subArr.length; a++) {
subArr[a] = arr[i + a];
}

long biggerEl = -1;
for (long lo : subArr) {
if (lo > temp) {
biggerEl = lo;
break;
}
}
return biggerEl;
}

Find missing integer in Java

 This is the program to find the missing numbers in the first n integers.


public static int findMissingNumber(int[] nums) {
int n = nums.length + 1;
// formula for sum of first n integers is n*(n+1)/2
// but we have n+1 integers as one integer is missing
int sum = (n * (n + 1)) / 2;
for (int i = 0; i < nums.length; i++) {
sum -= nums[i];
}
return sum;
}

Bubble Sort implementation in Java

 Below is the demo of Bubble sort algorithm.


package com.javadevelopersnotes.algo;

import java.util.Arrays;

public class BubbleSortAlgo {


public static void main(String[] args) {
int[] input = {33, 21, 45, 64, 55, 34, 11, 8, 3, 5, 1};
System.out.println("Before Sorting : ");
System.out.println(Arrays.toString(input));
bubbleSort(input);
}

private static int[] bubbleSort(int[] list) {
int i, j, temp = 0;
for (i = 0; i < list.length - 1; i++) {
for (j = 0; j < list.length - 1 - i; j++) {
if (list[j] > list[j + 1]) {
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
System.out.println("After Sorting : ");
System.out.println(Arrays.toString(list));
return list;
}
}

Friday, August 19, 2022

find duplicate items in array

 Find duplicate items in array in java


This is the java program to find the duplicate elements inside an array.


package com.javadevelopersnotes.controller;

import java.util.HashSet;
import java.util.Set;

public class FindDuplicates {

public static void main(String[] args) {
int arr[] = {1, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9};
int n = arr.length;

Set<Integer> uniqueNumbers = new HashSet<>();
Set<Integer> duplicateNumbers = new HashSet<>();

for (int num : arr) {
if (uniqueNumbers.add(num) == false) {
duplicateNumbers.add(num);
}
}

System.out.println("Below are the unique items");

for (int item : uniqueNumbers) {
System.out.println(item);
}

System.out.println("Below are the duplicate items");

for (int item : duplicateNumbers) {
System.out.println(item);
}

}
}

Ruby Basics

Basics of Ruby   Start Ruby interpret On terminal just type rib ruby_docs $ irb 3 . 0 . 0 : 001 > name = "This is the first ...