Wednesday 15 November 2023

Profiles setting in intellij Springboot

  1. Goto Run > Edit Configuration
  2. Choose the configuration you want to edit, in the left under Application.
  3. On the right side > Under Environment Variable, update spring.profiles.active=<your profile name> example spring.profiles.active=dev (observer:- the variable should be without -D flag)
  4. Save the changes and Run the Spring boot app with the same configuration.





Sunday 14 July 2019

Java Sorting Algorithm How to find the duplicates elements in array

There are multiple ways to find the duplicate elements in java. 

I am giving examples of  finding the duplicate elements in java.

Best way to find the duplicate elements in java is to use the sort the elements of the array and compare each element with their next element. 

I have commented some of the method, if you remove the comment before the function call then you can check one by one.  I found this is very useful in real time scenario and solving the interview question. 
Using this sorting technique we can perform String manipulation as well, because String is nothing but it's also built up with array. You can be able to check the duplicate character in string as well. 


Example:

package com.vikash.java8;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class ArrayDuplicate {

public static void main(String args[]) {

int array[] = { 5, 5, 3, 8, 2, 1, 8, 3, 3, 1 };
ArrayDuplicate dup = new ArrayDuplicate();
// dup.myFunction(array);
System.out.println();
// dup.withoutSorting(array);
// dup.bubbleSort(array);

dup.findDuplicateUsingMap(array);

}

public void myFunction(int array[]) {

Arrays.sort(array);
System.out.println("After Sorting the elements");
for (int a : array) {

System.out.print(a + " ");

}
System.out.println();

for (int i = 0; i < array.length - 1; i++) {
if (array[i] == array[i + 1]) {
System.out.println("Duplicate found::" + array[i] + ":::" + array[i + 1]);
}
}

}

public void withoutSorting(int[] array) {
for (int i = 1; i < array.length; i++) {
for (int j = 0; j < i; j++) {
if (array[j] == array[i]) {
System.out.println("Duplicate Found::" + array[j] + "::" + array[i]);
}
}
}
}

public void bubbleSort(int[] array) {

int temp;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;

}
}
}

for (int e = 0; e < array.length - 1; e++) {
if (array[e] == array[e + 1]) {
System.out.println("Duplicate Found::" + array[e]);
}

}
}

public void findDuplicateUsingMap(int[] array) {
Map<Integer, Integer> myMap = new HashMap();
for (int i = 0; i < array.length; i++) {
if (myMap.containsKey(i)) {
myMap.put(array[i], myMap.get(i) + 1);
} else {
myMap.put(array[i], 1);
}
}
for (Map.Entry<Integer, Integer> itr : myMap.entrySet()) {
System.out.println(itr.getKey() + " " + itr.getValue());
}
}
}


Thursday 18 December 2014

Controller in AngularJS

Conroller in AngularJS :-The controller is responsible for construction of the model and connects it to the view.
Controller should be straight forward and simply contain the business logic needed for a view.

There are many ways to define a controller in Angular js.

Examples are as follows:

<div ng-controller="myCtrl">
<p>{{name}}</p>
</div>


function myCtrl($scope)
{
   $scope.name="vikash Angular Solution";
}


Thursday 5 June 2014

Example 1.

<!DOCTYPE html>
<html lang="en">


<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
</head>

<body ng-app="myapp">


<div ng-controller="HelloController" >

    <h2>Hello {{helloTo.title}} !</h2>

</div>


<script>
angular.module("myapp", [])
    .controller("HelloController", function($scope) {
        $scope.helloTo = {};
        $scope.helloTo.title = "World, AngularJS";
    } );
</script>

</p>

</body>

</html> 

What is Angular.js with example

Example 1.

<!DOCTYPE html>
<html lang="en">


<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
</head>

<body ng-app="myapp">


<div ng-controller="HelloController" >

    <h2>Hello {{helloTo.title}} !</h2>

</div>


<script>
angular.module("myapp", [])
    .controller("HelloController", function($scope) {
        $scope.helloTo = {};
        $scope.helloTo.title = "World, AngularJS";
    } );
</script>

</p>

</body>

</html> 

Thursday 15 May 2014

Difference Between Comparable and Comparator

Here are the main difference between in Comparable and Comparator


1. Comparable belongs to java.lang.package

2. Comparable is by default uses by java collections to sort their element in sorted element.

3. Comparable has the method named compareTo which is use to compare two object and returns -1, +1  
 and o zero.

4. If we want to use comparable interface then the class 

                 public int Object1.compareTo(Object2) ;

      If object1 is less than object2 it will return -1, and if the object1 is greater than object2 then it will teturn   +1 and if both are equal then it will return 0.

5. Suppose we have a class Employee and the employee has many details. But if we want to sort the Employee details according to their age using the collections then we have to implement the comparable in he employee class and override the compareTo method there. Means a class which want to be sorted using collections using comparable it must have to implement Comparable interface.

6. But using comparator we not need to implement the Comparator interface instead of this we can create an different java class that will help to sort the class (Employee)





Saturday 12 April 2014

Java String Handling

1. How to reverse a given String


public class ReverseString {
public static void main(String args[]){
    String name="flipkart";
    for(int i=name.length()-1;i>=0;i--){
        System.out.print(name.charAt(i));
    }
}
}