How to create directory in Java

How to create directory in Java

In this post, we will see how to create directory in Java.

We will be covering following options to understand how to create directory in Java

  1. Create a directory using file I/O introduced in Java 7 (java.nio package).
  2.  Apache Commons IO

 

1. Using Java 7

Java 7 Introduced java.nio.file system which provided 2 methods for creating a directory in Java.

public static Path createDirectory(Path dir, FileAttribute<?>... attrs)

public static Path createDirectories(Path dir,FileAttribute<?>... attrs)


createDirectory method is used to create a directory but will throw an exception in case parent directory does not exist into the system. Use this method only when you parent directory already exists in the file system.

createDirectories method is useful when we want to create all nonexistence nonexistent parent directories first.

1.1 Use CreteDirectory Method

 

public class CreateDirectory {

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

        Path path = Paths.get("/Users/personal/tutorials/directory");
        Path newPath= Files.createDirectory(path);
    }
}

Above code will create new directory with name "directory" under tutorials.

Above code will throw NoSuchFileException in case hierarchy does not exist.If we will change hierarchy to /Users/personal/tutorials1/directory (note tutorials1 does not exist in the underlying file system)

Output


Exception in thread "main" java.nio.file.NoSuchFileException: /Users/personal/tutorials1/directory
	at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
	at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
	at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
	at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:384)
	at java.nio.file.Files.createDirectory(Files.java:674)
	at com.umeshawasthi.tutorials.corejava.io.directory.CreateDirectory.main(CreateDirectory.java:16)

It will throw FileAlreadyExistsException in the case of directory structure already exists.Note that for createDirectory, check for the existence of the file and the creation of the directory if it does not exist are a single operation

1.2 Use CreteDirectories Method

For creating all nonexistent parent directories first, createDirectories method should be used

 


public class CreateDirectories {

    public static void main(String[] args) throws IOException {
        Path path = Paths.get("/Users/umesh/personal/tutorials_new/newDirectory");
        Files.createDirectories(path);
    }
} 

The method will not throw an exception if the directory could not be created because it already exists.In case it fails, it may do so after creating some, but not all, of the parent directories.

Both these methods will delegate work to FileSystemProvider.

FileAttribute attrs... is an optional parameter which can be used to set file attributes while creating file /directory.

For most of the case, FileAttribute will be set automatically.

 

1. Using Apache Commons IO

Apache Commons FileUtils#forceMkdir provide a convenient and easy way to create a directory in Java.


public class CreateDirectoryByFileUtils {

    public static void main(String[] args) throws IOException {
        FileUtils.forceMkdir(new java.io.File("/Users/umesh/personal/tutorials/directory"));
    }
}

FileUtils class provides two methods to create directory structure

public static void forceMkdir(File directory)
public static void forceMkdirParent(File file)

In this post, we checked different options how to create a directory in Java. Java 7 NIO package got a good enhancement to the file I/O package.

All the code of this article is available Over on Github. This is a Maven-based project.

References

  1. CreateDirectory
  2. CreateDirectories
  3. FileUtils.html#forceMkdir
  4. FileUtils#forceMkdirParent

Java String to Inputstream

Java String to Inputstream

In this post, we will go through different options at how to convert  Java String to InputStream using Java and Apache Commons IO library

1. Convert Using Plain Java

Use ByteArrayInputStream to convert Java String to InputStream

 

public class StringToIOJava {

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

        String inputString = "This is a String to demo as how to convert it to input stream using Core Java API";

        //String to input stream
        try (InputStream inputStream = new ByteArrayInputStream(inputString.getBytes(StandardCharsets.UTF_8));
             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))
        ) {
            String content;

            while ((content = bufferedReader.readLine()) != null) {
                //work with file
            }

        }
    }
}

 

 It’s always recommended to pass explicit Charset to the method getBytes() to avoid any unwanted behaviour.Java will pick platform’s default charset if not passed to the method.

 

1. Convert Using Apache Commons IO

Apache Commons IO provides short and concise solution for convert String to InputStream

 

public class StringToIOApache {

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

        String inputString = "This is a String to demo as how to convert it to input stream using Apache Commons IO";

        InputStream inputStream = IOUtils.toInputStream(inputString, StandardCharsets.UTF_8);

    }
}

In this post, we saw two easy and concise way to convert simple String to InputStream.

Please read Convert InputStream to String in Java to check how to Convert InputStream to String in Java.

 

All the code of this article is available Over on Github. This is a Maven-based project.

References

  1. ByteArrayInputStream
  2. Apache Commons IO

Convert Stack Trace to String in Java

Convert Stack Trace to String in Java

Handling exceptions in Java require an exception to storing in a file or might need it for some additional analysis than simply logging it. In this post, we will cover how to convert stack trace to string in Java.

1 Using Core Java API

Core Java API provides easy and efficient way to convert stack trace to string using StringWriter and PrintWriter

 

 
StringWriter writer = new StringWriter();
PrintWriter printWriter= new PrintWriter(writer);
exception.printStackTrace(printWriter);

 

 
public class StackTraceToStringJava {

    public static void main(String[] args) {

        try {
            throwDummyException();
        } catch (FileNotFoundException exception) {

            StringWriter writer = new StringWriter();
            PrintWriter printWriter= new PrintWriter(writer);
            exception.printStackTrace(printWriter);
            System.out.println("Exception in String is :: " + writer.toString());
        }
    }

    public static void throwDummyException() throws FileNotFoundException {
        throw new FileNotFoundException("Throwing file not found exception for demo purpose");
    }
}

 

Output

 
Exception in String is :: java.io.FileNotFoundException: Throwing file not found exception for demo purpose at com.umeshawasthi.tutorials.corejava.exception.StackTraceToStringJava.throwDummyException(StackTraceToStringJava.java:26)
at com.umeshawasthi.tutorials.corejava.exception.StackTraceToStringJava.main(StackTraceToStringJava.java:15)

Calling writer.toString() will provide stack trace in String format.

 

2 Using Apache Commons Lang API

Apache Commons Lang API ExceptionUtils class can be used to convert stack trace to String with a simple one line.

 


public class StackTraceToStringApache {

    public static void main(String[] args) {

        try {
            throwDummyException();
        } catch (FileNotFoundException exception) {
            String stackTraceAsString= ExceptionUtils.getStackTrace(exception);
            System.out.println("Strack Trace using Apache Commons :" + stackTraceAsString);
        }
    }

    public static void throwDummyException() throws FileNotFoundException {
        throw new FileNotFoundException("Throwing file not found exception for demo purpose Apache Commons Lang");
    }
}

Output


Strack Trace using Apache Commons :java.io.FileNotFoundException: Throwing file not found exception for demo purpose Apache Commons Lang
	at com.umeshawasthi.tutorials.corejava.exception.StackTraceToStringApache.throwDummyException(StackTraceToStringApache.java:23)
	at com.umeshawasthi.tutorials.corejava.exception.StackTraceToStringApache.main(StackTraceToStringApache.java:15)

To Apache Commons Lang API, we need to add it to classpath, We will use Maven for this (Simply add to lib folder if not using Maven)

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.5</version>
</dependency>

3 Using Guava’s Throwable Class

Google Guava class provide Throwable utility class to convert stack trace to String

 String stackTrace = Throwables.getStackTraceAsString ( exception ) ;

We saw there are a number of different ways to Convert stack trace to String in Java using Java core API and third party API available.

Java 9 will be introducing StackWalker API which will provide more easy and efficient way to perform these operations.

 

All the code of this article is available Over on Github. This is a Maven-based project.

References

  1. Apache Commons Lang
  2. Guava Class Throwables
  3. Java 9 StackWalker

Java try with resources

Java try with resources

In Java, we open a file in a try block and close it in finally block to avoid any potential memory leak. Java try with resources was introduced in Java 7.

This new feature of Java try with resources statement ensure that resources will be closed after execution of the program. Resources declared under try with java resources must implement java.lang.AutoCloseable interface.

Here are few of the advantages of using Java try with resources statement.

  1. You don’t have to close resource explicitly, close() is automatically called by JVM, if it throws an IOException, it will be suppressed. (Call it automatic resource management)
  2. More clean and concise code.
  3. Ability to open multiple resources and manage them automatically.

 

Before Java 7, We will handle resource in following way


try{
      // Open resource (e.g FileStream)
    }
    catch(IOException exception){
        //exception handling
    }
    finally{
        //block to close resoucres opened in try block
    }

Java 7  try with resources introduced a more easy way to handle it.


try(//open your resource(s) here ){
        // Open resource (e.g FileStream)
    }
    // no need of finally, JVM will handle it for us

 

Pay close attention to try(//open your resource(s) here ) which is the new and main block for Java try with resource feature.

1. Resource Handling Pre Java 7

public class FIleIOPreJDK7 {

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

        String fileName = "/tutorials/fileread/SampleFile.txt";
        String fileContent;
        BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
        try {

            while ((fileContent = bufferedReader.readLine()) != null) {
                //business logic for file processing
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();   // we need to close it to avoid memory leak.
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        }

    }
}

2. With Java 7

public class FileIOWithJava7 {

    public static void main(String[] args) {

        String fileName = "/tutorials/fileread/SampleFile.txt";

        try( BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName))){
            String content;
            while((content = bufferedReader.readLine() )!=null){
                //you logic here
            }
        }
        catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }
}

In the above example,  the resource declared in the try-with-resources statement is a BufferedReader.Resource declaration appears within parentheses immediately after the try keyword.

You must have noticed that there is no finally in the code, try with resource statement will automatically close BufferReader once program complete try block.

In this section, we will cover other features of  Java try with resources.

3. Try with resources for multiple Resources

try with resources can efficiently be used for multiple resources

 

public class MultipleResouces {

    public static void main(String[] args) {

        String fileName = "/tutorials/fileread/SampleFile.txt";
        String fileName1 = "/tutorials/fileread/SampleFile1.txt";

        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
             Scanner scanner = new Scanner(new File(fileName1))) {
            String content;
            while ((content = bufferedReader.readLine()) != null) {
                //you logic here
            }
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }
}

Multiple resources can be declared within try block by using

; (try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
             Scanner scanner = new Scanner(new File(fileName1))))

4. Custom Resource

To create custom resource, you need to implement AutoCloseable interface

MyCustomResource impliments AutoCloseable{
  @Override 
  public void close() throws Exception{
     //you need to impliment close method
  }
}

5. Improvement with JDK9

As per of the Milling Project Coin in JDK 9, there is further improvement in the Java try with resources statement which is more natural” way of writing.

If you already have resource as a final or a final variable, you can easily use this variable in try statement without declaring any a new variable

Before JDK9

// BufferedReader is declared outside try() block
BufferedReader br = new BufferedReader(new FileReader("file-path"));

try (BufferedReader inBr = br) {
    // ...
} catch (IOException e) {
    // ...
}

With JDK9

// JDK9
BufferedReader br = new BufferedReader(new FileReader("file-path"));

try (br) {
    // use the reader
} catch (IOException e) {
    // ignoring exceptions because that is how I roll
}

All the code of this article is available Over on Github. This is a Maven-based project.

Reference

  1. try-with-resources
  2. AutoCloseable
  3. Closeable

How to Read Large File in Java

How to Read Large File in Java

In our last article, we cover How to read file in Java.This post will cover how to read large file in Java efficiently.

Reading the large file in Java efficiently is always a challenge, with new enhancements coming to Java IO package, it is becoming more and more efficient.

We have used sample file with size 1GB for all these. Reading such a large file in memory is not a good option, we will covering various methods outlining How to read large file in Java line by line.

 

1 Using Java API

We will cover various options how to read file in Java efficiently using plain Java API.

 

 1.1 Using Java BufferReader


public class ReadLargeFileByBufferReader {

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

        String fileName = "/tutorials/fileread/file.txt"; //this path is on my local
        try (BufferedReader fileBufferReader = new BufferedReader(new FileReader(fileName))) {
            String fileLineContent;
            while ((fileLineContent = fileBufferReader.readLine()) != null) {
                // process the line.
            }
        }
    }
}

Output


Max Memory Used : 258MB
Time Take : 100 Seconds

 1.2 Using Java 8 Stream API

public class ReadLargeFIleUsingStream {

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

        String fileName = "/tutorials/fileread/file.txt"; //this path is on my local
        // lines(Path path, Charset cs)
        try (Stream inputStream = Files.lines(Paths.get(fileName), StandardCharsets.UTF8)) {
            inputStream.forEach(System.out::println);
        }
    }
}

 

Output

Max Memory Used : 390MB
Time Take : 60 Seconds


 1.3 Using Java Scanner

Java Scanner API also provides a way to read large file line by line.


  public class ReadLargeFileByScanner {

    public static void main(String[] args) throws FileNotFoundException {
        String fileName = "/Users/umesh/personal/tutorials/fileread/file.txt"; //this path is on my local
        InputStream inputStream = new FileInputStream(fileName);
       
        try(Scanner fileScanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())){
            while (fileScanner.hasNextLine()){
                System.out.println(fileScanner.nextLine());
            }
        }
    }
}

Output


Max Memory Used : 460MB
Time Take : 60 Seconds

2 Streaming File Using Apache Commons IO

This can also be achieved by using Apache Commons IO  FileUtils.lineIterator () Method


public class ReadLargeFileUsingApacheCommonIO {

    public static void main(String[] args) throws IOException {
        String fileName = "/Users/umesh/personal/tutorials/fileread/file.txt"; //this path is on my local

        LineIterator fileContents= FileUtils.lineIterator(new File(fileName), StandardCharsets.UTF_8.name());
        while(fileContents.hasNext()){
            System.out.println(fileContents.nextLine());   
        }
    }
}

Output


Max Memory Used : 400MB
Time Take : 60 Seconds

As we saw how to read large file in Java efficiently. Few things which you need to pay close attention

  1. Reading the large file in one go will not be a good option (You will get OutOfMemoryError ).
  2. We Adapted technique to read large file line by line to keep memory footprint low.

I used VisualVM to monitoring Memory, CPU and Threadpool information while running these programmes.

based on our test, BufferReader has the lowest memory footprint, though the overall execution was slow.

All the code of this article is available Over on Github. This is a Maven-based project.

References

  1. Apache Commons IO

How to read file in Java

How to read file in Java

In this post, We will cover as  How to read file in Java. This post will cover different options available in Java to see how to read file in Java.

1.  Reading File Using JDK

JDK provide number of option to read file in Java, we will explore few of the options to read file

  1.1 Read File using Java 8 Stream API

Java 8 Provides an efficient way to read a file using Stream API. You can use Files.line to read it as a  stream

File Content

This Show How
To Read File
Using Java 8
public class ReadFileByStream {

    public static void main(String[] args) {

       String fileName = "/tutorials/fileread/SampleFile.txt"; //this path is on my local
        try {
            List<String> readStream=Files.lines(Paths.get(fileName)).collect(Collectors.toList());
            readStream.forEach(System.out::println);
        } 
catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output

This Show How
To Read File
Using Java 8

Java 8 Stream API is really clean and provide an elegant way to read the file.We have seen too many open files in a system exception being reported.

 

1.2 Read File using BufferReader and StringBuilder

BufferReader with StringBuilder is an efficient way to read small file in Java

public class ReadFileByBufferReader {

    public static void main(String[] args){

        String fileName = "/tutorials/fileread/SampleFile.txt";

        try(BufferedReader bufferReader = new BufferedReader(new FileReader(fileName))) {
            StringBuilder fileContent = new StringBuilder();
            String line = bufferReader.readLine();

            while (line != null) {
                fileContent.append(line);
                fileContent.append(System.lineSeparator());
                line = bufferReader.readLine();
            }

            String fileInformation = fileContent.toString();
            System.out.println(fileInformation);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output

This Show How
To Read File
Using Buffer Reader

Above Example used try-with-resources feature to autoclose stream.

1.3 Read File using Scanner

package com.umeshawasthi.tutorials.corejava.io.readfile;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 * Created by umesh on 5/2/17.
 */
public class ReadFileByScanner {

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

        String fileName = "/tutorials/fileread/SampleFile.txt";
        File inputFile = new File(fileName);

        useScannerByLoop(inputFile);
        useScannerWithoutLoop(inputFile);

    }

    private static void useScannerByLoop(final File file) throws FileNotFoundException {
        Scanner fileScanner = new Scanner(file);
        while(fileScanner.hasNextLine()){
            System.out.println(fileScanner.nextLine());
        }
        fileScanner.close();
    }

    private static void useScannerWithoutLoop(final File file) throws FileNotFoundException {
        Scanner fileScanner = new Scanner(file);
        fileScanner.useDelimiter("\\Z");
        System.out.println(fileScanner.next());

    }
}

Output (For both cases)

This Show How
To Read File
Using Scanner
With Loop

Make sure to check next line when using Scanner else it can throw NoSuchElementException exception

 

1.4  Apache Commons IO Utils

Apache Commons provide the convenient method to work with files.

Here is a clean and efficient way to as how to read file in Java using Apache Commons IO Utils.

public class ReadFileByApacheIO {

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

        String fileName = "/tutorials/fileread/SampleFile.txt";
        try(FileInputStream inputStream = new FileInputStream(fileName)){

            String fileContent = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
            System.out.println(fileContent);
        }
    }
}

Output

This Show How
To Read File
Using Apache
IO

We have used Apache Commons IOUtils using Java 7  try-with-resources feature.

Apache FileUtils

public class ReadFileByApacheFileUtils {

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

        String fileName = "/Users/umesh/personal/tutorials/fileread/SampleFile.txt";
        List<String> fileContent= FileUtils.readLines(new File(fileName), StandardCharsets.UTF_8);

        for(String line : fileContent){
            System.out.println(line);
        }
    }
}

Output

This Show How
To Read File
Using Apache
IO

There are multiple ways as How to Read file in Java. All options have their own positive and negative.

Our recommendation is

  1. Apache Commons
  2. Buffer Reader
  3. Others

If you are using Google Guava, you can use Guava in a very similar way Apache Commons IO.

Please refer to our post on how to convert InputStream to String for a basic understanding of the process.

All the code of this article is available Over on Github. This is a Maven-based project.

References

  1. Apache Commons IOUtils
  2. Apache Commons IO FileUtils

Convert inputstream to string in Java

Convert InputStream to string in Java

Converting InputStream to String in Java is very common operation, in this post, We will cover how to Convert InputStream to String in Java using

  1. Apache Commons IOUtils
  2. Java

 

1. Apache Common

One of the clean ways to Convert InputStream to String in Java is using Apache Commons IOUtils.

public class InputStreamStringCommonIO {

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

        String sampleTestString = "Converting input stream to String using Apache Common IO";
        InputStream inputStream = new ByteArrayInputStream(sampleTestString.getBytes());

        convertUsingCopy(inputStream, StandardCharsets.UTF_8);
        convertUsingToString(inputStream, StandardCharsets.UTF_8);
    }

    public static void convertUsingCopy(final InputStream inputStream, final Charset inputEncoding) throws IOException {
        StringWriter writer = new StringWriter();
        IOUtils.copy(inputStream, writer, inputEncoding);
        System.out.println(writer.toString());
    }

    public static void convertUsingToString(final InputStream inputStream, final Charset inputEncoding) throws IOException {
        String output = IOUtils.toString(inputStream, inputEncoding);
        System.out.println(output);
    }
}

Output

Converting input stream to String using Apache Common IO

Just note that IOUtils.toString()  method does not close inputStream, you can use IOUtils.closeQuietly()  to close it.

Most of the modern project use  Apache Commons as defacto API, if you are using Apache Commons IO, this is your best option.

 

2. Java Way

If you do not have the option to use Apache Commons IO, there are multiple ways to Convert InputStream to String in Java.

    2.1 Using InputStreamReader and StringBuilder

public class ConvertByInputStreamReader {

    public static void main(String[] args) {
        String sampleTestString = "Converting input stream to String using JDK Input Stream Reader";
        InputStream inputStream = new ByteArrayInputStream(sampleTestString.getBytes());
        System.out.println(inputStream);

    }

    private static String getStringByInputStreamReader(final InputStream inputStream) throws IOException {
        final int bufferSize = 1024;
        final char[] buffer = new char[bufferSize];
        final StringBuilder output = new StringBuilder();

        Reader streamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
        int character = 0;
        while ((character = streamReader.read(buffer, 0, buffer.length)) > 0) {
            output.append(buffer, 0, character);
        }
        return output.toString();
    }
}

 

    2.2 Using BufferReader and StringBuilder

public class ReadUsingBufferReader {

    public static void main(String[] args) {
        String sampleTestString = "Converting input stream to String using JDK Buffer Reader";

        InputStream inputStream = new ByteArrayInputStream(sampleTestString.getBytes());
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder outStream = new StringBuilder();
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                outStream.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println(outStream.toString());
    }
}

Output

Converting input stream to String using JDK Buffer Reader

 

    2.3 Using Scanner

public class ReadUsingScanner {

    public static void main(String[] args) {

        String sampleTestString = "Converting input stream to String using JDK Using Scanner";
        InputStream inputStream = new ByteArrayInputStream(sampleTestString.getBytes());

        Scanner inputScanner = new Scanner(inputStream, StandardCharsets.UTF_8.name()).useDelimiter("\\A");
        String output = inputScanner.hasNext() ? inputScanner.next() : "";
        System.out.println(output);
        inputScanner.close();
    }
}

Output

Converting input stream to String using JDK Using Scanner

Google Guava provide a good alternate to Convert InputStream to String in Java.

As we saw, there are a couple of ways to convert InputStream to String in Java, it based on your preferences, personally, I prefer Apache Commons IO.

All the code of this article is available Over on Github. This is a Maven-based project.

References

  1. Apache Commons IO

Java Sorting Example(Comparable and Comparator)

Java Sorting Example(Comparable and Comparator)

In this article, we will cover Java sorting example (Comparable and Comparator). Java Collection API provides a way to sort Array and Lists but it expects input from the developer.This articles will try to give some example to use both java.lang.Comparable and java.util.Comparator to sort objects.

 

1. Sort an ArrayList

Collections API’s utility class Collections provide a handy way to sort an ArrayList in natural ordering provided all elements in the list must implement the Comparable interface.

public class SimpleSorting {

    public static void main(String[] args) {

        List<String> locationList = new ArrayList<String>();
        locationList.add("California");
        locationList.add("Texas");
        locationList.add("Seattle");
        locationList.add("New Delhi");

        Collections.sort(locationList);

        for (String location : locationList) {
            System.out.println("Location is: " + location);
        }
    }
}

Output

Location is: California
Location is: New Delhi
Location is: Seattle
Location is: Texas

Above example worked perfectly with Collections.Sort() as List defined in example was Lits<String> and java.lang.String implements the Comparable interface.

In the case of List contains custom objects, Collections.sort() will fail if the custom object does not implement the Comparable interface.

2. Sort Object using Comparable

public class Person implements Comparable<Person> {

    private String name;
    private int age;

    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int compareTo(Person person) {
        return this.age > person.age ? 1 : this.age < person.age ? -1 : 0;
    }
}

To sort it, we will use Collections.sort() method

public class ComparableDemo {

    public static void main(String[] args) {

        List<Person> persons = new ArrayList<Person>();

        persons.add(new Person("Umesh Awasthi", 35));
        persons.add(new Person("Robert Hickle", 55));
        persons.add(new Person("John Smith", 40));
        persons.add(new Person("David", 23));

        Collections.sort(persons);

        for (Person person : persons) {
            System.out.println("Person is: " + person.getName());
        }

    }
}

Output is

Person is: David
Person is: Umesh Awasthi
Person is: John Smith
Person is: Robert Hickle

Pay close attention to Person class declaration public class Person implements Comparable<Person>  which indicates that Person class is a comparable if you remove Comparable<Person> from your Person class and try Collection.sort(persons) , compile will give the following error

Error:(21, 20) java: no suitable method found for sort(java.util.List<com.umeshawasthi.tutorials.corejava.sorting.Person>)
    method java.util.Collections.<T>sort(java.util.List<T>) is not applicable
      (inferred type does not conform to upper bound(s)
        inferred: com.umeshawasthi.tutorials.corejava.sorting.Person
        upper bound(s): java.lang.Comparable<? super com.umeshawasthi.tutorials.corejava.sorting.Person>)
    method java.util.Collections.<T>sort(java.util.List<T>,java.util.Comparator<? super T>) is not applicable
      (cannot infer type-variable(s) T
        (actual and formal argument lists differ in length))

As a developer, it’s out responsibility to pass the comparable object to Collections.sort() method.

It’s up to you how you want to use the compareTo method and what will be your comparison logic, I have used a simple logic to compare the age of the person but you can use any custom logic to in compareTo()  method.

3. Sort Object using Comparator

java.lang.Comparable interface provides only one way to compare this object with another one, what are the options in case we need to compare 2 objects to sort them correctly.How about sorting Person with name or age?  What if we want to override the natural ordering. We need to create a Comparator for these requirements.

First and super short template is to provide a custom comparator while calling Collections.sort()  method

public class ComparatorDemo {

    public static void main(String[] args) {
        List<Person> persons = new ArrayList<Person>();

        persons.add(new Person("Umesh Awasthi", 35));
        persons.add(new Person("Robert Hickle", 55));
        persons.add(new Person("John Smith", 40));
        persons.add(new Person("David", 23));
        persons.add(new Person("David", 63));

        Collections.sort(persons, new Comparator<Person>() {
            public int compare(Person person, Person person1) {
                int name = person.getName().compareTo(person1.getName());
                if(name == 0){
                    return name;
                }
                return person.getAge() > person1.getAge() ? 1 : person.getAge() < person1.getAge() ? -1 : 0;
            }
        });


        for (Person person : persons) {
            System.out.println("Person is: " + person.getName());
        }
    }
}

Output

Person is: David
Person is: Umesh Awasthi
Person is: John Smith
Person is: Robert Hickle
Person is: David

Check the output, the interesting part is David, which is first and last result of the output.

Another efficient way is to define the Comparators in the Contact itself to reuse them instead of recreating every time

public class ComparatorPerson {

    private String name;
    private int age;

    public ComparatorPerson(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static Comparator<ComparatorPerson> COMPARE_BY_NAME = new Comparator<ComparatorPerson>() {
        public int compare(ComparatorPerson one, ComparatorPerson other) {
            return one.name.compareTo(other.name);
        }
    };

    public static Comparator<ComparatorPerson> COMPARE_BY_AGE = new Comparator<ComparatorPerson>() {
        public int compare(ComparatorPerson one, ComparatorPerson other) {
            return one.age > other.age ? 1 : one.age < other.age ? -1 : 0;
        }
    };
}

Comparator with multi options

public class MultiComparatorDemo {
    public static void main(String[] args) {
        List<ComparatorPerson> persons = new ArrayList<ComparatorPerson>();

        persons.add(new ComparatorPerson("Umesh Awasthi", 35));
        persons.add(new ComparatorPerson("Robert Hickle", 55));
        persons.add(new ComparatorPerson("John Smith", 40));
        persons.add(new ComparatorPerson("David", 23));
        persons.add(new ComparatorPerson("David", 63));

        Collections.sort(persons,ComparatorPerson.COMPARE_BY_AGE );

        for (ComparatorPerson person : persons) {
            System.out.println("Person Name Sorted by Age is: " + person.getName());
        }

        System.out.println("######################################################");
        Collections.sort(persons,ComparatorPerson.COMPARE_BY_NAME );

        for (ComparatorPerson person : persons) {
            System.out.println("Person Name Sorted by Name  is: " + person.getName());
        }
    }
}

Output

Person Name Sorted by Age is: David
Person Name Sorted by Age is: Umesh Awasthi
Person Name Sorted by Age is: John Smith
Person Name Sorted by Age is: Robert Hickle
Person Name Sorted by Age is: David
######################################################
Person Name Sorted by Name  is: David
Person Name Sorted by Name  is: David
Person Name Sorted by Name  is: John Smith
Person Name Sorted by Name  is: Robert Hickle
Person Name Sorted by Name  is: Umesh Awasthi

 

4. Sort Object using Java8

Java 8 added a new way of making Comparators that reduces the amount of code you have to write Comparator.comparing. Java 8 streaming API provides neater approach. It’s always good to understand how compareTo()  and compare()  method work under the hood

public class Java8CompareExample {

    public static void main(String[] args) {

        List<Person> persons = new ArrayList<Person>();

        persons.add(new Person("Umesh Awasthi", 35));
        persons.add(new Person("Robert Hickle", 55));
        persons.add(new Person("John Smith", 40));
        persons.add(new Person("David", 23));
        persons.add(new Person("David", 63));

        persons.sort(Comparator.comparing(Person::getName).thenComparingInt(Person::getAge));

        for (Person person : persons) {
            System.out.println("Person Name is: " + person.getName());
        }

        persons.sort(Comparator.comparingInt(Person::getAge).thenComparing(Person::getName));

        for (Person person : persons) {
            System.out.println("Person Name is: " + person.getName());
        }
    }
}

In this article, we cover Java Sorting Example(Comparable and Comparator) provided by JDK, we will cover some other API which provides the same functionalities with additional features.

All the code of this article is available Over on Github. This is a Maven-based project.

As a bonus, have a look at Apache Common ComparatorUtils which provide some of the ready to use a feature for you.

Reference

  1. Comparator
  2. Comparable
  3. Comparator.comparing

Convert String to int in Java

Convert String to int in Java

There are few ways to convert String to int in Java. Below are few of popular ways for converting String to int

  1. Integer.parseInt()
  2. Integer.valueOf()

1.  Integer.parseInt() Examples

Integer.ParseInt()  will return an int primitive value.If efficiency is your concern and you only need primitive value, then using Integer.ParseInt()  is the best option.

Here is an example to convert “50” to an int value

public class JavaParseInt {
    public static void main(String[] args) {
        String number ="50";
        int result = Integer.parseInt(number);
        System.out.println(result);
    }
}

Here is the output of the above code

50

2.  Integer.valueOf() Examples

You can also use Integer.valueOf() , this method will return Integer object

public class JavaIntegerValueOf {

    public static void main(String[] args) {
        String number ="60";
        Integer result = Integer.valueOf(number);
        System.out.println(result);
    }
}

Output

60

Actually, valueOf uses parseInt internally. If efficiency is your concern do not use this method to convert String to int or Integer.

3.  NumberFormat Exception

Both Integer.parseInt()  and Integer.valueOf()  can throw NumberFormatException if the string cannot be parsed as an integer or int

public class NumberFormatExceptionExample {

    public static void main(String[] args) {
        String invalidNumber = "abc";
        parseIntNFException(invalidNumber);

    }

    public static void parseIntNFException(String number) {
        Integer result = Integer.parseInt(number);
        System.out.println(result);
    }

    public static void valueOfNFException(String number) {
        Integer result = Integer.valueOf(number);
        System.out.println(result);
    }
}

Output

Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Integer.parseInt(Integer.java:615)
	at NumberFormatExceptionExample.parseIntNFException(NumberFormatExceptionExample.java:13)
	at NumberFormatExceptionExample.main(NumberFormatExceptionExample.java:8)

Note: PraseInt() return a primitive value while valueOf() will always return new Integer object, ideally, if you just want to convert String to an int, it’s always better to avoid creating new Object.

Furthur Reading

It’s always a good habit to read Javadoc for more inside details

  1. JavaDoc valueOf()
  2. JavaDoc parseInt()
  3. NumberFormatException

Download above examples from

  1. GitHub Repository

Java Comparable vs Comparator

Java Comparable vs Comparator

In this articles we will discuss Java Comparable vs Comparator, very often people have  lots of questions when it comes to Java Comparable vs Comparator

  1. What is Comparable and what is Comparator in Java
  2. How do we use them
  3. What is the difference between Comparable and Comparator?
  4. When we should use Comparable vs Comparator

This post does not cover basics about Java’s Comparable and Comparator ( Will cover that in another post ), but this post is more towards getting insight about Comparable vs Comparator.

 

Comparable

public interface Comparable<T>

When a class implement Comparable, the compareTo method of the class defines the “natural” ordering of that object. CompareTo method should follow some general contracts, few of the examples are

  1. Should always be returned 0  for objects when equal ()  comparisons return true.
  2. Comparable is useful when there’s a single natural default comparison.

Comparator

public Interface Comparator<T>

Comparator provides a way to compare 2 objects and can be used to compare objects in a way that might not align with the natural ordering.

One of the common examples is String, String is generally compared alphabetically, ” a”.compareTo(“b”)  would use alphabetical comparisons.If you want to compare based on the String length, writing custom Comparator is a preferred solution.

 

If you do not have control over the source code ( where you can not implement Comparable ), then use Comparator.Comparator is useful when you have multiple ways to compare an object ( e.g you can compare cars by brand and cc etc.).

Both seem to be different, but to summarise, there isn’t much difference.Both ends to similar means.In general implement comparable for natural order, and write a comparator for other sorting or comparison needs.

Please read Java Sorting Example(Comparable and Comparator) to get an understanding of using Comparable and Comparator for sorting objects in Java.