Posts

Skipping Junit Tests in maven build

When a project is build, Maven runs all the junit tests written for this project by default. But sometime few of them gets failed. But still developer wants to build the project.

For this purpose junit tests can be skipped with -DskipTests command on command line as below.

D:\Project>mvn install -DskipTests

Or -Dmaven.test.skip=true can be used as below on command line.

D:\Project>mvn install -Dmaven.test.skip=true

The difference in both the commands is that skipTests complies the tests and skips running. While maven.test.skip stops both compile and run.

Java Program for CountDownLatch

package com.java.thread.concurrent;importjava.util.concurrent.CountDownLatch;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;classProcessorimplements Runnable {private CountDownLatch latch; Processor(CountDownLatch latch){this.latch= latch;}@Overridepublicvoidrun(){ System.out.println("Started.");try{ Thread.sleep(3000);}catch(InterruptedException e){ e.printStackTrace();} latch.countDown(); System.out.println("Completed.");}}publicclassCountDownThread{publicstaticvoidmain(String[] args){ CountDownLatch downLatch =new CountDownLatch(3); ExecutorService executorService=Executors.newFixedThreadPool(3);for(int a=0; a<3; a++){ executorService.submit(new Processor(downLatch));}try{ downLatch.await();}catch(InterruptedException e){ e.printStackTrace();} System.out.println("Finished.&quo…

Java program for Fixed Thread Pool

package com.java.thread.executor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class Processor implements Runnable {

    private int id;

    public Processor(int id) {
        super();
        this.id = id;
    }

    @Override
    public void run() {
        System.out.println("Starting.... " + id + "by thread " + Thread.currentThread().getName());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Completed.... " + id +  "by thread " + Thread.currentThread().getName());
    }

}

public class ProcessorExecutor {

    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(2);

        for(int a=0; a<5; a++){
            service.submit(new Processor(a));
        }

        service.shutdown();

        System.out.print…

Java program to find duplicate element in array

package com.java;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

public class FindDuplicateElementInArray {

    public static void main(String[] args) {

        int[] intArr = new int[] { 1, 2, 1, 2, 1, 3, 4, 6, 2, 8 };

        Map<Integer, Integer> map = new LinkedHashMap<Integer, Integer>();

        for (int i = 0; i < intArr.length; i++) {
            // take first element and then matched complete array
            int temp = intArr[i];
            int count = 0;

            for (int j = 0; j < intArr.length; j++) {
                if (temp == intArr[j]) {
                    // element matched -- break
                    count++;
                }
            }
            map.put(temp, count);
        }

        Set<Integer> duplicate = new LinkedHashSet<Integer>();
        Set<Integer> noDuplicate = new LinkedHashSet<Integer>();

        for (int i = 0…

BigInteger to String conversion problem in Java

Issue when converting the BigInteger to String.

Above code was creating an issue as , when the value of macObject.getMACHINECODE() is coming as null,
Then valueOf() method of String was converting its value as null String as "null".

So when machinecode variable is being checked for null, it is getting passed through as the converted value,
was String.

And code was not working as expected.


String machinecode = String.valueOf(macObject.getMACHINECODE());
if (machinecode != null) {
// some code
}

So the solution is as below,

BigInteger machineCode = macObject.getMACHINECODE());
if(machineCode != null){
machineCode .toString();
// some code
}

Java Program for Anagrams

Problem : A text file is given which contains many anagrams line by line. All anagrams should be in groups after processing of this file.
package com;
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map;
publicclass FindAnagram {
publicstaticvoid main(String[] args) {               String aFile = "F:\\anagram\\anagramInput.txt";

Writing text to file and reading text from files in Java

Writing text to file and reading text from files.

publicstaticvoid writeToFile(String logFilePath, String sb) {               File file = new File(logFilePath); try {             FileWriter fstream = new FileWriter(file, false);            try {                 fstream.write(sb);                 fstream.flush();             } catch (IOException ioException) { LOGGER.error(ioException.getMessage()+ "]-");             } finally { if (fstream != null) {                     fstream.close();                 }             }         } catch (IOException ioException) {