Skip to main content

Posts

Spring Reactive Stack

Compress a String

package com.sbs.java8.praticse; public class StringCompression { public StringCompression() { // TODO Auto-generated constructor stub } public static void main(String[] args) { System.out.println(compressString("aaaabbbbbccccAAAAccccccccdefg")); } public static String compressString(String str) { //String str = "aaaabbbbbcccc"; char[] charArray = str.toCharArray(); String compressedString = ""; int i = 0; while (i charArray.length)? str: compressedString; return output; } }

Basic Sortings (Bubble, Selection and Insertion Sorts)

public class BasicSortings { public static void main(String[] args) { int temp; int iterationCount = 0; int array[] = { 2, 33, 29, 30, 21, 98}; //Bubble sort or Simple sort for (int i = 0; i array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } iterationCount++; } } System.out.println("Bubble Sort Big 0(n) --> " + iterationCount); for (int s = 0; s //Selection Sort iterationCount =0; int sortPointer=0; for (int i = sortPointer; i array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } iterationCount++; } sortPointer++; } System.out.println("Selection Sort Big 0(n) --> " + iterationCount); for (int s = 0; s //Insertion Sort iterationCount=0; for (int i = 1; i 0;j--) // Inner loop travers back wards { if(array[j-1]>array[j]) { temp = array[j-1]; array[j-1] ...

Finding Second Highest element in an Array

package com.sbs.java8.praticse; public class SecondHightestClass { public static void main(String[] args) { final Integer[] intArray = { 10, 2, 36, 7, 29, 30, 100, 20, 90, 83, 87 }; final int n = intArray.length; int sortPointer = intArray.length; for (int m = n; m > (n - sortPointer); m--) { for (int i = 0; i < n - 1; i++) { int j = i + 1; if (intArray[i] > intArray[j]) { int temp = intArray[i]; intArray[i] = intArray[j]; intArray[j] = temp; } } sortPointer--; } System.out.println(intArray[n - 2]); } }

Anogram Java Program

package com.sbs.java8.praticse; import java.util.Arrays; public class AnagramClass { public static void main(String[] args) { String firstString = "1MASS1111124"; String secondString = "1SAMS1111124"; boolean status = false; if (validateString(firstString,secondString)) { char array1[] = firstString.toLowerCase().toCharArray(); char array2[] = secondString.toLowerCase().toCharArray(); Arrays.sort(array1); Arrays.sort(array2); status = Arrays.equals(array1, array2); } System.out.println("Anogram Status " + status); } private static boolean validateString(String string,String string2) { if (string == null || string.isEmpty()) { return false; } if (string2 == null || string2.isEmpty()) { return false; } if(string.length() != string2.length()){ return false; } return true; } }

NO SQL Types and vendors in market

NO SQL KEY - VALUE Store  - Similar to a  MAP.    e.g. Dynamo DB or REDIS DOCUMENT Sore   - Similar to KEY-VALUE, value can be JSON/XML and key will be unqiuely idnetified this document.    e.g. Coutch DB or mongoDB. Column Store - Multi timentional table, identifying data with row and column numbers.   e.g.  Cassandra or Apache HBase Graph Store - Store relation between nodes (record entities) . It has better transaction management e.g. Neo4J and Orient DB.

Maven Build Life Cyles

Following are the maven build life cycles. Clean - Removes the generated files in 'target' folder. command : mvn clean Validate - It validates your project and verify all the necessary information is there in project or not. mvn  valiate Compile - It compiles your project code. mvn compile Package - It takes the compiled code and packaged to JAR/WAR/EAR. It also compiles your code if your code is not compiled. mvn package Install -    It takes the packaged code and puts in your local maven repository (M2_HOME). It also does the compile and package if these are not already been done. mvn install Deploy - It takes the package and puts in remote repository for sharing accross team members or for other projects. mvn deploy FYI - Above commands can be used as variety of combinations. e.g. mvn clean install mvn clean package mvn clean compile package install To skip the tests to run  mvn clean install -DskipTests

Regular Expression in java

1 [abc] a, b, or c (simple class) 2 [^abc] Any character except a, b, or c (negation) 3 [a-zA-Z] a through z or A through Z, inclusive (range) 4 [a-d[m-p]] a through d, or m through p: [a-dm-p] (union) 5 [a-z&&[def]] d, e, or f (intersection) 6 [a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction) 7 [a-z&&[^m-p]] a through z, and not m through p: [a-lq-z](subtraction) X?  X, once or not at all X*  X, zero or more times X+  X, one or more times X{n}    X, exactly n times X{n,}   X, at least n times X{n,m}  X, at least n but not more than m times Reluctant quantifiers X?? X, once or not at all X*? X, zero or more times X+? X, one or more times X{n}?   X, exactly n times X{n,}?  X, at least n times X{n,m}? X, at least n but not more than m times Possessive quantifiers X?+ X, once or not at all X*+ X, zero or more times X++ X, one or more times X{n}+   X, exactly...

Most Common mistakes in Java Coding.

1. Copy & Paste Its one of the most powerful tool and technique in the typical programmer. But at the same time its too dangerous that you ought to forgot the code according to you requirements. So always be cautious about what piece of code you are copying. Read at least two to three times to avoid the tricky for fishy things while your unit testing. 2. Exception Catch Block. Always give priority to write some thing useful and meaning full statements in the exception catch block.It really worth when you troubling shooting the issues. Never leave this block with empty or sop's. And always throw the exception to the calling method. 3. String operations Since String is immutable object, when you do any operation on string that results a new object. So after you do any modifications on String and do assign the string some object and use it. 4. Null pointers Don't perform the operations on objects without checking the Object null condition. And while checking the...

CRON Expressions

  CRON expression.   Cron-Expressions are used to configure instances of CronTrigger. Cron-Expressions are strings that are actually made up of seven sub-expressions, that describe individual details of the schedule. These sub-expression are separated with white-space, and represent: Seconds Minutes Hours Day-of-Month Month Day-of-Week Year (optional field)       +-------------------- second ( 0 - 59 ) | +----------------- minute ( 0 - 59 ) | | +-------------- hour ( 0 - 23 ) | | | +----------- day of month ( 1 - 31 ) | | | | +-------- month ( 1 - 12 ) | | | | | +----- day of week ( 0 - 6 ) ( Sunday = 0 or 7 ) | | | | | | +-- year [ optional ] | | | | | | | * * * * * * * command to be executed      Examples.   1. For every 10 seconds    0/10 * * 1/1 * ?     2. For every Sunday midnight at 23:00...

Programming Guide Lines

1. Modularization Do not write the whole logic code in one block/function instead delegate the each and individual responsibility to each function. For example if you are converting one Date time format to another write this logic in Utils and use it. It might be useful in other place as well. And if you come across any common piece which is repeated in you logic put it in a function call where ever needed. This is basic fundamental rule. 2. Single Responsibility. Always delegate single responsibility to one block/function of code. Don't mess multiple operations in a single block/function. And make sure block/function of name should ideally match to what it actually does . 3. Name Justification Always declare the class, method or variable names based on its purpose. And don't use any shorter names. The one who reads your code should get idea what actually it does.

RESTful Service Java Implentaions

Jax RS API and its implementation Clarification The API does not provide any implementation code (concrete classes that do the actual processing). The API only specifies interfaces, annotations, exceptions. There can be concrete classes with some very basic, core behavior. The idea of standard APIs, like JAX-RS, is to give developers something to code against regardless of the underlying platform and implementation. Jersey, RestEasy and CXF are the implementations - it includes many concrete classes that actually handle the business  promised  by the API according to JSR  (Java Specification Request) 311. In JAX-RS case - it handles requests and responses. Apache CXF - CXF is the implementation from Apache Group. Jersey - the JAX-RS Reference Implementation from Sun. We're using Jersey as its packed full of features (e.g. WADL, implicit views, XML/JSON/Atom support) has a large and vibrant developer community behind it and has great spring integration...

Spring Boot Advantages

1. With Spring Boot you can focus more on business features and less on infrastructure. 2. With Spring Boot, project gets added with required libraries and configurations based on reasonable assumptions that Spring boot makes. Assumptions will be made based on the class path. 3. Spring Boot doesn’t generate code or make edits to your files. Instead, when you start up your application, Spring Boot dynamically wires up beans and settings and applies them to your application context.  Spring Boot = Spring Framework + Embded HTTP Servers (Tomcat, Jetty etc..) - XM Configurations For more information visit http://projects.spring.io/spring-boot/

JDK Vs JRE

JDK = JRE + Java Development tools JRE = JVM + Java inbuilt packages + Run time libraries Most of the people always has the doubt of why JDK is again having JRE inside ? Though it got pro vided out of the JDK. Standard JRE doesn't have tools like javac, javaDB and tool.jar So make sure you always point JDK JRE in your project build path. Except the above difference rest of the things are same with Standard JRE's and JRE inside JDK.

Project or Build Version Numbers

In Software world, every project/product release has one version number associated with it to identify on which version of code base it has. Usually every project/product release has its version as follows. (Major).(Minor).(Maintenance/Enhancements).(Build Number) Major - If the release has Major changes in Project/Product then we need to increment this number by 1 Minor -   If the release has Minor changes in Project/Product then we need to increment this number by 1 Maintenance - If the release has Bug fixes or Small Enhancements then we need to increase this number by 1. Build Number :  Every time when we deliver build (latest code) to the QA to test, then we need to increase by 1. Partial Builds: If only some of the modules in the project modified and if those modules only delivered to QA means, it is called as partial builds. And this will be noted as build number along with alphabets. E.G of Build Numbers. Current Version of Project is 4.0.0.0 (4 Major ...

Linux Useful Commands with examples

To know the IP address ----------- hostname -i (ip addres) hostname -s (short name) hostname -d (domain name) hostname -v (host name with domain name) To know the port number and running processes netstat -ano | findstr 80 If the file is huge in size and to see the file content we can use less <file name> e.g. less service.log Searching  matching pattern in file grep '<matching text>' <file name> e.g. grep 'Exception' service.log To grep the text from .gz files gzgrep '<matching text>' <gz file name> e.g. gzgrep 'Exception' service.log.gz ps -eaf | grep java ps aus | grep java tail -f <file Name> (auto updated last few lines) tail -100 logfile.log  (last 100 lines) ls -a  (hidden files) ls -lrt (Sort the files by time stamp) kill -9 <Process ID>   (get Process ID using ps -eaf | grep <process name>) e.g. kill -9 30102

ClassNotFoundException vs NoClassDefFoundError

Both ClassNotFoundException and NoClassDefFoundError errors will be occurred  when the required Class not present in the Class path.     ClassNotFoundException     ClassNotFoundException is an Exception got arized when trying load class using forName() method in class Class. findSystemClass method in class ClassLoader . loadClass method in class ClassLoader. e.g. Class.forName("oracle.jdbc.driver.OracleDriver") if the OracleDriver class file is not present in the Class path, then we will end up with this ClassNotFounException NoClassDefFoundError NoClassDefFoundError is an Error thrown at Runtime, when the Class is present during the compile time and not present during Runtime of the Code, then we will end up with this NoClassDefFoundError. 1. When required Class is not available at Runtime available only in compile time. 2.Since NoClassDefFoundError error is an Subclass of LinkageError, it will occur when one of the     Dependent ...

Content Extracting From 'CLOB' type Columns in SQL Querries

Extracting XML node value from CLOB  column in data base. Syntax ExtractValue(xml_fragment, xpath_expression) Query Syntax Select Extractvalue(XMLtype('<column name>'),'<XML node path>') From <table list> Where <conditions> Example Select Extractvalue(Xmltype(emp_xml), '/employees/empployee/emp_number') From employee  where  Status='A'; Updating a XML node value from CLOB  column in data base. Syntax UpdateXML(xml_target, xpath_expr, new_xml) Query Syntax Update table_name Set column_name=Updatexml(Xmltype('<column name>'),'<XML node path>','<Replacing XMLvalue'>).getclobval From <table list> Where <conditions>; Example Initial value /employees/empployee/emp_name/text() = Bhargava Update employe Set emp_name = Updatexml(Xmltype(emp_xml), '/employees/empployee/emp_name/text()','Bhargava Surimenu') where  emp_no=1207;