Tuesday, June 12, 2018

ANGULAR 5

Pre requirement for the angular 5
1) node and npm 
     to check the node and the npm install in the system by two command
     for node 
                        node -v  
      for npm 
                        npm -v

note for the ubuntu(i am using this)

1.     sudo apt-get update
2.     sudo apt-get install nodejs
3.     sudo apt-get install npm

     if the node is not install go to the nodejs official site and follow the instruction how to install               nodejs  in the your system os platform(ios,window,ubuntu,linux)
 2) if the node and npm is install then we required the angular cli for that we have to run the
      
        npm install @angular/cli -g
     
     to check the angulat 
         ng -v or ng --version

   it is display the angular version, node version and os platform information.

3) if all these are completed then we have to create the create project for that
    1.      ng new <project-name> 
    2.      ng new <project-name>  --routing
             for the cli create the routing module and component  (we learn later)
    3.      ng new  <project-name> --style=css --routing 
             for the change the extension of the style sheet file default if is .scss format.
            css3 is the scss previous is .sass

4) the boiler plate is create (initial code)

      it create the  <projectname>/src/

- app.routing.module.ts       (if run the 2nd or 3rd command of 3rt point)
- app.module.ts
-app.component.html
-app.component.spec.ts
-app.component.ts
-app.component.scss/css 





Sunday, May 27, 2018

Set Radio Button To Android Layout

Radio button is the option where we can select one option from multiple, in android its little bit .

We need to take a radio group inside radio group declare all the radio option with one method which will be called at radio selection type and assign the value to a variable.


 <RadioGroup
        android:id="@+id/typeRadio  >

        <RadioButton
            android:id="@+id/radio_male"
            android:text="Male"
            android:onClick="onRadioButtonClicked"/>

        <RadioButton
            android:id="@+id/radio_female"
            android:text="Female"
            android:onClick="onRadioButtonClicked"/>

    </RadioGroup>

After making layout set the  variable in the activity class and asign its value through the onclick method.

private String gender;

protected void onCreate(Bundle savedInstanceState) {

RadioGroup rg = (RadioGroup) findViewById(R.id.ammountTypeRadio);

}

Here Radio button on-click method will respond on each time radio will selected and will be assign to variable.

public void onRadioButtonClicked(View view){

        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.radio_male:
                if (checked)
                    gender = "male";
                    break;
            case R.id.radio_female:
                if (checked)
                    gender="female";
                    break;
        }
    }





Wednesday, May 16, 2018

Get OS Name and create folder and file to linux server/ Operating system

This program is to get the server details using System.getProperty() , and then make a folder in Linux OS. I am making a folder inside the /tmp folder.






public class ReadWriteFile {

public static void main(String[] args) throws IOException {

String osName = System.getProperty("os.name");
Map sysP = System.getProperties();
String tm = System.getProperty("java.io.tmpdir");
File file = new File(tm+"/abc");
File ff= new File(file+"/abdc.txt");
if(osName.startsWith("Linux")){
System.out.println("Linux sysy");
}
if(!file.exists()){
file.mkdirs();
}else{
ff.createNewFile();
}
System.out.println(osName+" "+file.getPath());
System.out.println(tm);
System.out.println(sysP);
}

Friday, March 30, 2018

Sort Map based on the values in java

Sort Map based on the values in java, so using HashMap we can store the values, then swap the values key as value and value as key and create another Map of TreeMap this will sort based on the key, So in previous HasMap values are became key in new TreeMap and those are sorted now.

Now take a LinkedHashMap this will store the values in order, so at end we will get sorted Map based on the values.


import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

public class MapReverseBasedOnValue {

public static void main(String[] args) {

Map <Integer, String> map = new HashMap();
map.put(1, "User6");
map.put(2, "User1");
map.put(3, "User4");
map.put(4, "User9");
map.put(5, "User5");

Map<String , Integer> temp = new TreeMap();

Map<Integer, String> sortedMap = new LinkedHashMap();

for(Map.Entry<Integer, String> mp : map.entrySet()){
temp.put(mp.getValue(), mp.getKey());
}
for(Map.Entry<String, Integer> mp : temp.entrySet()){
sortedMap.put(mp.getValue(), mp.getKey());
}

System.out.println(map);
System.out.println(temp);
System.out.println(sortedMap);
}
}

Friday, March 23, 2018

First non repeating character from string in java

Find first non repeated character from a string using hashmap in java.







import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class NonRepeatingChar{

public static void main(String[] args) {

Scanner scn =new Scanner(System.in);
System.out.println("Type string ");
String s = scn.nextLine();

int index = nonRepeatedChar(s); // non repeating first character

System.out.println(s.charAt(index));
}

public static int nonRepeatedChar(String s) {

Map<Character, Integer> map = new HashMap();
int index =0;

char[] str=s.toCharArray();
for (int i = 0; i < str.length; i++) {

if (map.containsKey(str[i])) {
map.put(str[i], (Integer) map.get(str[i]) + 1);
} else {
map.put(str[i], 1);
}
}
System.out.println(map);
for(int i=0;i<s.length();i++) {
if(map.get(s.charAt(i))==1) {
index = i;
break;
}
}
return index;
}

}





Count the words in a string using java program

Counting words of string using java is a easy using HashMap, where key will become words and value will become number of occurrence of the word, if word already available in the HashMap so increase the value with 1 else add a new word with value 1.



import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class WordCount {

public static void main(String[] args) {

Scanner scn =new Scanner(System.in);
System.out.println("Type string to cound the words");
String s = scn.nextLine();

Map map = wordCount(s);

System.out.println(map);
}

public static Map wordCount(String s) {

Map map = new HashMap();

String[] str = s.split(" |,|\\.");// or(|) for multiple parameter for split
for (int i = 0; i < str.length; i++) {

if (map.containsKey(str[i])) {
map.put(str[i], (Integer) map.get(str[i]) + 1);
} else {
map.put(str[i], 1);
}
}
return map;
}

}

Monday, March 19, 2018

Configure Actuator in Spring Boot



Actuator endpoints let you monitor and interact with your application. Spring Boot includes a number of built-in endpoints and lets you add your own. For example, the health endpoint provides basic application health information.

Actuator Depandancy :-

Maven -

<dependencies>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>
</dependencies>


Gradle -

dependencies {
 compile("org.springframework.boot:spring-boot-starter-actuator")
}


Access actuator - http//localhost:8080/actuator/health
8080 - is a port number it could be anything.

By default only health and info are accessible for security reasons in web based application. if you want to access all the endpoints.

management.endpoints.web.exposure.include=*

in case only selected endoints -

management.endpoints.web.exposure.include=  health, info, env 

for exclude endpoints in case of * selection

management.endpoints.web.exposure.exclude=env,beans

More details  click here - 







Monday, December 25, 2017

Divide A List To Number of Sublist in JAVA

This is basic program in java to Divide A List To Number of Sublist in JAVA, in case we want to devide a list into many sublists based on the condition.


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ListTest {

public static void main(String[] args) {
List list = new ArrayList();
Scanner scn = new Scanner(System.in);
System.out.println("Type list limit n");
int limitList = scn.nextInt();
for (int x = 1; x <= limitList; x++) {
list.add(x);
}
System.out.println("type number of new sub list from old list");
int n = scn.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
List ll = new ArrayList(filterList(list, n, i));
System.out.println(ll);
}

}

public static List filterList(List l, int newListNumber, int number) {
List newList = new ArrayList();
int newListSize = l.size() / newListNumber;
int firstIndex;
int lastIndex;
if (number == 0) {
firstIndex = 0;
} else {
firstIndex = (newListSize * number) + 1;
}
if (number == 0) {
lastIndex = (newListSize * 1);
} else {
lastIndex = (firstIndex + newListSize) - 1;
}

for (int i = 0; i < l.size(); i++) {
if (i >= firstIndex && i <= lastIndex) {
newList.add(l.get(i));
}
}
return newList;

}

}

Tuesday, October 17, 2017

How to make Map Unmodifiable in java collection

Collections class provide a method unmodifiableMap to make our Map unchangeable.
After applying unmodifiableMap() to map if try to put a new value it will throw UnsupportedOperationException.



public class UnmodifiableMapTest {

    public static void main(String[] args) {
      

        Map map=new HashMap();
        map.put(1, "value1");
        map.put(2, "value2");
        Map m=Collections.unmodifiableMap(map);
       
        m.put(3, "value3");
        System.out.println(map);
   
    }

}


Output : -

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableMap.put(Collections.java:1457)
    at Basic.Animals.main(Animals.java:31)

Object class In Java -all Methods With Complete Signature

Object class In Java -all  Methods With Complete Signature

public native int hashCode();

public boolean equals(Object obj) {
        return (this == obj);
    }

protected native Object clone() throws CloneNotSupportedException;

public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }


public final native void notify();

public final native void notifyAll();

public final native void wait(long timeout) throws InterruptedException;

public final void wait(long timeout, int nanos) throws InterruptedException {
        if (timeout < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (nanos < 0 || nanos > 999999) {
            throw new IllegalArgumentException(
                                "nanosecond timeout value out of range");
        }

        if (nanos > 0) {
            timeout++;
        }

        wait(timeout);
    }




public final void wait() throws InterruptedException {
        wait(0);
    }


protected void finalize() throws Throwable { }