Java

Reading/Writing Files using FileChannel

Posted on

6db3a-download

There are multiple ways to reading/writing a file in Java. Usually, we used java.lang.IO package to read/write a file but as my last experience with copying a large file size (>256 MB ) into some other file location or on a network.

 

As we also tried to used java.lang.io package to get this job done but we were unable to copy the file in a Network location, there were performance issues.

http://stackoverflow.com/questions/1605332/java-nio-filechannel-versus-fileoutputstream-performance-usefulness

And finally, I got an alternative way to copy a file into a Network location  i.e FileChannel . its provide a better performance than InputStream/OutputStream.

Some interesting points about java.nio or FileChannel:-

  1. it’s a buffer-oriented, means- same Buffer is used to reading / writing so you can move back or forth in the Buffer as you need to.  usee flip() method to get the pointer to next position.
  2. It’s a non-blocking, i.e means a Thread request to reading data from Channel then only get what is currently available in a buffer, if data is not available then rather than blocking until data get available in buffer, Thread goes on and do something else in meantime. The same is true for non-blocking writing. A thread can request that some data be written to a channel, but not wait for it to be fully written. The thread can then go on and do something else in the mean time.
  3. Concurrent processing can be done in FileChannel which is safe for use.
  4. Channels can be read and written asynchronously.

Below example demonstrated how to write into a file using FileChannel.


 import java.io.File;
        import java.io.FileNotFoundException;
        import java.io.IOException;
        import java.io.RandomAccessFile;
        import java.nio.ByteBuffer;
        import java.nio.channels.FileChannel;
        import java.nio.channels.WritableByteChannel;
        
        /**
         * Created by kumajye on 10/01/2017.
         */
        public class FileChannelTest {
            // This is a Filer location where write operation to be done.
            private static final String FILER_LOCATION = "C:\\documents\\test";
            // This is a text message that to be written in filer location file.
            private static final String MESSAGE_WRITE_ON_FILER = "Operation has been committed.";
        
            public static void main(String[] args) throws FileNotFoundException {
                // Initialized the File and File Channel
                RandomAccessFile randomAccessFileOutputFile = null;
                FileChannel outputFileChannel = null;
                try {
                    // Create a random access file with 'rw' permission..
                    randomAccessFileOutputFile = new RandomAccessFile(FILER_LOCATION + File.separator + "readme.txt", "rw");
                    outputFileChannel = randomAccessFileOutputFile.getChannel();
                    //Read line of code one by one and converted it into byte array to write into FileChannel.
                    final byte[] bytes = (MESSAGE_WRITE_ON_FILER + System.lineSeparator()).getBytes();
                    // Defined a new buffer capacity.
                    ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
                    // Put byte array into butter array.
                    buffer.put(bytes);
                    // its flip the buffer and set the position to zero for next write operation.
                    buffer.flip();
                    /**
                     * Writes a sequence of bytes to this channel from the given buffer.
                     */
                    outputFileChannel.write(buffer);
                    System.out.println("File Write Operation is done!!");
        
                } catch (IOException ex) {
                    System.out.println("Oops Unable to proceed file write Operation due to ->" + ex.getMessage());
                } finally {
                    try {
                        outputFileChannel.close();
                        randomAccessFileOutputFile.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
        
                }
        
            }
        
        }


How to Schedule Future Task Using java.util.Timer

Posted on Updated on

java.util.Timer

6db3a-downloadA java.util.Timer class provides a facility to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals. Timer class uses java.util.TaskQueue to add tasks at given regular interval and at any time there can be only one thread running the TimerTask.

TimerTask :-

java.util.TimerTask is an abstract class that implements Runnable interface and we need to extend this class to create our own TimerTask that can be scheduled using java Timer class.

Key points :-

  • java.util.Timer is a thread safe – means, multiple threads can share a single Timer object without the need for external synchronization.
  • This class schedules tasks for one-time execution, or for repeated execution at regular intervals.
  • All constructors start a timer thread.
  • This class does not offer real-time guarantees: it schedules tasks using the Object.wait(long) method.

Class Constructors :-

  • Timer() – Create a new Timer
  • Timer(boolean isDaemon) – Creates a new timer whose associated thread may be specified to run as a daemon.
  • Timer(String name) – Creates a new timer whose associated thread has the specified name.
  • Timer(String name, boolean isDaemon) – Creates a new timer whose associated thread has the specified name, and may be specified to run as a daemon.

Class Methods :-

  • cancel() – Terminates this timer, discarding any currently scheduled tasks.
  • purge() – Removes all cancelled tasks from this timer’s task queue.
  • schedule(TimerTask task,Date time) –  Schedules the specified task for execution at the specified time.
  • schedule(TimerTask task,Date firstTime,long period) – Schedules the specified task for repeated fixed-delay execution, beginning at the specified time.
  • schedule(TimerTask task,long delay) – Schedules the specified task for execution after the specified delay.
  • schedule(TimerTask task,long delay,long period) – Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
  • scheduleAtFixedRate(TimerTask task, Date firstTime, long period) – Schedules the specified task for repeated fixed-rate execution, beginning at the specified time.
  • scheduleAtFixedRate(TimerTask task, long delay, long period) – Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.

Example :-

Here, we scheduled a non-repeated future task that will be print on a Console after the delay of 10 min. TimerTask is a task that can be scheduled for one-time or repeated execution by a Timer and it is a sub-class of Runnable Interface.


import java.util.Timer;
import java.util.TimerTask;
/**
 * Created by MYPC on 10/8/2016.
 */
public class FutureExecutionExample {
    public static void main(String[] args) {
        Timer t = new Timer();
        t.schedule(new TimerTask() {
                  @Override
                  public void run() {
                  // Execute given scheduled task after 10 min.
                  System.out.println(" Run specific task at given time." + System.currentTimeMillis());
               }
             }
         , 10 * 60 * 1000);  // 10 mins
    }
}


Let assume, we create a Timer to run every 2 seconds interval but the execution of Thread takes 3 seconds then Timer keeping adding a schedule task in queue and as soon as Thread execution get finished then, it’s notify to queue to execute next one.


Timer t = new Timer();
t.schedule(new TimerTask() {
       @Override
       public void run() {
           System.out.println(" Run specific task at given time." + System.currentTimeMillis());
           try {
               Thread.sleep(3000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
   }

, 1000,2000);  // After 1 second given task will be executed and every 2 second given task execution repeat.
}


Java 8 – Consumer Example

Posted on Updated on

Java 8 – Consumer Example 

Consumjava-8-lambdaer<T> introduced in Java 8.  Although it is a Functional Interface that  accepts only one argument and  operation to be performed on that argument but do not return anything.

Since It has only one method i.e

accept( T t);  //Performs this operation on the given argument.

and another method which is a default method– As you know that since Java 8, An Interface can be  a default method implementation which is denoted as default keyword.

default Consumer<T> andThen(Consumer<? super T> after)

Basically, this default method performs a sequence of composed consumers operation that followed by the after operation.

Below is some example that’s build up you understanding of Consumer<T> concept :-

 


package com.example1;

import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by MYPC on 9/19/2016.
 */
public class ConsumerTest {

    public static void main(String[] args) {

        /**
         * Example 1
         */
        List integerList = Stream.of(new Integer(1), new Integer(10), new Integer(200), new Integer(101), new Integer(-10), new Integer(0))
                .collect(Collectors.toList());
        forEach(integerList, (number) -> {
            System.out.println(" Number is " + number);
        });

        /**
         * Example 2
         */
        Consumer consumer = (number) -> {
            number += number;
            System.out.println(number);
        };

        /**
         * Example 3
         */
        consumer.accept(2000);
        getConsumer().accept(1000);

        /**
         * Example 4
         */
        integerList.forEach((num -> {
            System.out.println("Java 8 forEach Loop" + num);
        }));


    }

    /**
     * Return consumer that to be applied on accept(T t) parameter.
     *
     * @return
     */
    public static Consumer getConsumer() {
        return (number) -> System.out.println("Method Invoked here " + number);
    }

    /**
     * Iterate over the Collection and to be consumed by console or other system.
     *
     * @param collections
     * @param consumer
     * @param 
     */
    public static  void forEach(Collection collections, Consumer consumer) {
        collections.forEach(consumer);
    }
}


Let’s move to some other example:-

In the example, we have tried to create a generic forEach loop that’s iterating over the generated Query result and consume by some other object.

ExecutorContext.Java – Object consume data producted by getQuery().


import java.util.List;
import java.util.function.Consumer;

interface ExecutorContext {
    List getQuery();
    Consumer getConsumer();
}


StubsUtils.Java : – Utility class


class StubsUtils {

    public static void forEach(ExecutorContext executorContext) {
        executorContext.getQuery().forEach(executorContext.getConsumer());
    }
}
 

Java8ConsumerExample.Java :- This is  a Consumer client that invoked forEach loop and override  ExecutorConext interface to produce & consume the query set elements.

Here, getQuery() method returns a Collection  but it could be any Result set query that will be produced a result set by JdbcTemplate. Other method  getConsumer() will be applied the desired operation over the each element of Collection returned by getQuery() method means printing on consoles and adding into a list.

Inside the getConsumer(), you can probably apply your logic to perform the operation over the Input argument.


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Java8ConsumerExample {

    private static List QUERY_RESULTSET_INTEGER = Arrays.asList(new Integer(1), new Integer(10), new Integer(200), new Integer(101), new Integer(-10), new Integer(0));
    private static List QUERY_RESULTSET_STRING = Stream.of("A", "B", "C", "D", "E", "F").collect(Collectors.toList());

    public static void main(String[] args) {

        // EXAMPLE : 1

        /**
         * Iterator over the Query generated integer list and print on console.
         */
        StubsUtils.forEach(new ExecutorContext() {
            List consumerList = new ArrayList();

            /**
             * Let's assume that our query returns set of Integer that would be consume by some caller class or print on console.
             * @return
             */
            @Override
            public List getQuery() {
                return QUERY_RESULTSET_INTEGER;
            }

            @Override
            public Consumer getConsumer() {
                return x -> {
                    System.out.println(x);
                    consumerList.add(x);
                };
            }
        });

        // EXAMPLE : 2
        /**
         * Iterator over the Query generated String list and print on console.
         */
        StubsUtils.forEach(new ExecutorContext() {
            List consumerList = new ArrayList();

            /**
             * Let's assume that our query returns set of Integer that would be consume by some caller class or print on console.
             * @return
             */
            @Override
            public List getQuery() {
                return QUERY_RESULTSET_STRING;
            }

            @Override
            public Consumer getConsumer() {
                return x -> {
                    System.out.println(x);
                    consumerList.add(x);
                };
            }
        });

    }
}


Output:-

1 10 200 101 -10 0
A B C D E F

How to read/write CSV file to Map in java

Posted on Updated on

As you see in last article, how to read a CSV file into Java object with the help of Jackson API. In this article, we will see how to write a list of Map ( List<Map<String,String>> ) into a CSV file in your machine and again read it through with Jackson API.

Add this dependency in your pom.xml file to used Jackson API in your application.

<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-csv</artifactId>
  <version>2.7.0</version>
</dependency>

 

Below are a sample example of reading or writing a CSV file with the help of Jackson API, which makes it very easy to write or read the CSV file into Java Object.

 


package com.example;

import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

import java.io.*;
import java.util.*;

/**
 * Created by MYPC on 8/1/2016.
 */
public class ConvertMapToCSVTest {
    public static void main(String[] args) throws IOException {

        List<HashMap<String, String>> myArrList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;

        /*** Rows 1 ***/
        map = new HashMap<String, String>();
        map.put("CustomerID", "C001");
        map.put("Name", "Ajay Kumar");
        map.put("Email", "ajay@gmail.com");
        map.put("CountryCode", "TH");
        map.put("Budget", "1000000");
        map.put("Used", "600000");
        myArrList.add(map);

        /*** Rows 2 ***/
        map = new HashMap<String, String>();
        map.put("CustomerID", "C002");
        map.put("Name", "Rahul Kumar");
        map.put("Email", "Rahul.kumar@gmail.com");
        map.put("CountryCode", "UK");
        map.put("Budget", "2000000");
        map.put("Used", "800000");
        myArrList.add(map);

        /*** Rows 3 ***/
        map = new HashMap<String, String>();
        map.put("CustomerID", "C003");
        map.put("Name", "Wasim Akram");
        map.put("Email", "waism.atnet@rediff.com");
        map.put("CountryCode", "US");
        map.put("Budget", "3000000");
        map.put("Used", "600000");
        myArrList.add(map);

        /*** Rows 4 ***/
        map = new HashMap<String, String>();
        map.put("CustomerID", "C004");
        map.put("Name", "Santosh Kumar");
        map.put("Email", "santosh.ku@yahoo.com");
        map.put("CountryCode", "US");
        map.put("Budget", "4000000");
        map.put("Used", "100000");
        myArrList.add(map);

        File file = new File("d:\\sample.csv");
        // Create a File and append if it already exists.
        Writer writer = new FileWriter(file, true);
        Reader reader = new FileReader(file);
        //Copy List of Map Object into CSV format at specified File location.
        csvWriter(myArrList, writer);
        //Read CSV format from specified File location and print on console..
        csvReader(reader);
    }

    /**
     * @param listOfMap
     * @param writer
     * @throws IOException
     */
    public static void csvWriter(List<HashMap<String, String>> listOfMap, Writer writer) throws IOException {
        CsvSchema schema = null;
        CsvSchema.Builder schemaBuilder = CsvSchema.builder();
        if (listOfMap != null && !listOfMap.isEmpty()) {
            for (String col : listOfMap.get(0).keySet()) {
                schemaBuilder.addColumn(col);
            }
            schema = schemaBuilder.build().withLineSeparator("\r").withHeader();
        }
        CsvMapper mapper = new CsvMapper();
        mapper.writer(schema).writeValues(writer).writeAll(listOfMap);
        writer.flush();
    }

    /**
     * 
     * @param collection
     * @param writer
     * @param 
     * @throws IOException
     */
    public static  void csvWriter(Collection collection, Writer writer) throws IOException {
        if (collection != null && collection.size() > 0) {
            CsvMapper mapper = new CsvMapper();
            Object[] objects = collection.toArray();
            Class type = objects[0].getClass();
            CsvSchema schema = mapper.schemaFor(type).withHeader();
            mapper.writer(schema).writeValues(writer).write(objects);
        } else {
            writer.write("No Data");
        }
        writer.flush();
    }


    /**
     * @param reader
     * @throws IOException
     */

    public static void csvReader(Reader reader) throws IOException {
        Iterator<Map<String, String>> iterator = new CsvMapper()
                .readerFor(Map.class)
                .with(CsvSchema.emptySchema().withHeader())
                .readValues(reader);
        while (iterator.hasNext()) {
            Map<String, String> keyVals = iterator.next();
            System.out.println(keyVals);
        }
    }
}


A CSV file is created at specified location i.e d:\\sample.csv in file system.

Email,Used,Budget,CustomerID,CountryCode,Name
ajay@gmail.com,600000,1000000,C001,TH,"Ajay Kumar"
Rahul.kumar@gmail.com,800000,2000000,C002,UK,"Rahul Kumar"
waism.atnet@rediff.com,600000,3000000,C003,US,"Wasim Akram"
santosh.ku@yahoo.com,100000,4000000,C004,US,"Santosh Kumar"

Reading sample.csv file from file system.

{Email=ajay@gmail.com, Used=600000, Budget=1000000, CustomerID=C001, CountryCode=TH, Name=Ajay Kumar}
{Email=Rahul.kumar@gmail.com, Used=800000, Budget=2000000, CustomerID=C002, CountryCode=UK, Name=Rahul Kumar}
{Email=waism.atnet@rediff.com, Used=600000, Budget=3000000, CustomerID=C003, CountryCode=US, Name=Wasim Akram}
{Email=santosh.ku@yahoo.com, Used=100000, Budget=4000000, CustomerID=C004, CountryCode=US, Name=Santosh Kumar}

Hoping, It would be helpful !! Thanks

Jackson JSON Java Parser Example

Posted on

Jackson JSON Parser API provides easy way to convert JSON to POJO Object and supports easy conversion to Map from JSON data. Jackson supports generics too and directly converts them from JSON to object.

we can add below dependency in your pom.xml.

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.8.0</version>
</dependency>

jackson-databind jar depends on jackson-core and jackson-annotations libraries, so if you are adding them directly to build path, make sure you add all three otherwise you will get a runtime error.

Below is the Utilities class that uses Jackson API to transform String to Json to Map object or any Java type object or vice-versa.   ThreadLocal is used here because ObjectMapper is not Thread’s safe in Multithreading app otherwise simply create a static variable of ObjectMapper.


package com.example.utilities;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.IOException;
import java.util.Map;

/**
 * Created by MYPC on 7/18/2016.
 */
public class JSONConverter {

    protected static final ThreadLocal OBJECT_MAPPER_THREAD_LOCAL = ThreadLocal.withInitial(() -> new ObjectMapper());

    /**
     * Convert String as JSON to JsonNode.
     *
     * @param payload
     * @return
     */
    public static JsonNode getJsonFromString(String payload) {
        try {
            return OBJECT_MAPPER_THREAD_LOCAL.get().readTree(payload);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Convert JsonNode Object to Map
     *
     * @param payload
     * @return
     */
    public static Map getMapFromJson(JsonNode payload) {
        try {
            return OBJECT_MAPPER_THREAD_LOCAL.get().readerFor(Map.class).readValue(payload);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Convert Map Object to JsonNode.
     *
     * @param payload
     * @return
     */
    public static JsonNode getJsonFromMap(Map payload) {
        try {
            return getJsonFromString(OBJECT_MAPPER_THREAD_LOCAL.get().writeValueAsString(payload));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Create a Empty Object Node. i.e {}
     *
     * @return
     */
    public static ObjectNode createObjectNode() {
        return OBJECT_MAPPER_THREAD_LOCAL.get().createObjectNode();
    }

    /**
     * Create a Empty Array Json Object i.e [{},{}]
     *
     * @return
     */
    public static ArrayNode createArrayNode() {
        return OBJECT_MAPPER_THREAD_LOCAL.get().createArrayNode();
    }

    /**
     * Remove particular key elements from JsonNode.
     *
     * @param payload
     * @param key
     * @return
     */
    public static JsonNode remove(JsonNode payload, String key) {
        ((ObjectNode) payload).remove(key);
        return payload;
    }

    public static void main(String[] args) {
        String payload = "{\"id\":\"1001\", \"amount\":\"10000045.999\", \"currency\":\"EUR\"}";
        System.out.println(" Payload String  >> " + payload);
        JsonNode jsonNode = getJsonFromString(payload);
        System.out.println(" Convert String Payload to JsonNode  >> " + payload);
        Map map = getMapFromJson(getJsonFromString(payload));
        System.out.println(" GET Map Object from Payload String >> " + map);
        System.out.println(" Before Removing 'amount' from payload >> " + jsonNode);
        remove(jsonNode, "amount");
        System.out.println(" After Removing 'amount' from payload >> " + jsonNode);
        System.out.println(" Convert Map to JSON Node Object  >>" + getJsonFromMap(map));

        System.out.println(" Employee JsonNode >>" + createEmployeeJson());
        System.out.println(" Convert Employee Json to Map Object >>" + getMapFromJson(createEmployeeJson()));
    }

    /**
     * Testing on some real data.
     * employee.json
     * {
     * "id":"123",
     * "name":"Rahul Kumar",
     * "address":{
     *      "street":"Kalkaji",
     *      "city":"Delhi",
     *      "zipcode":"110020"
     * },
     * "phoneNumbers":[
     *      "9923456469",
     *      "9886786786"
     * ],
     * "role":"Developer"
     * }
     *
     * @return
     */
    private static JsonNode createEmployeeJson() {
        ObjectNode employeeJsonNode = createObjectNode();
        ObjectNode addressNode = createObjectNode();
        ArrayNode phoneList = createArrayNode();
        addressNode.put("street", "Kalkaji");
        addressNode.put("city", "Delhi");
        addressNode.put("zipcode", "110020");

        phoneList.add("9923456469");
        phoneList.add("9886786786");

        employeeJsonNode.put("id", "123");
        employeeJsonNode.put("name", "Rahul Kumar");
        employeeJsonNode.set("address", addressNode);
        employeeJsonNode.set("phoneNumbers", phoneList);
        employeeJsonNode.put("role", "Developer");
        return employeeJsonNode;
    }
}


output:-

Payload String >> {"id":"1001", "amount":"10000045.999", "currency":"EUR"}
 Convert String Payload to JsonNode >> {"id":"1001", "amount":"10000045.999", "currency":"EUR"}
 GET Map Object from Payload String >> {id=1001, amount=10000045.999, currency=EUR}
 Before Removing 'amount' from payload >> {"id":"1001","amount":"10000045.999","currency":"EUR"}
 After Removing 'amount' from payload >> {"id":"1001","currency":"EUR"}
 Convert Map to JSON Node Object >>{"id":"1001","amount":"10000045.999","currency":"EUR"}
 Employee JsonNode >>{"id":"123","name":"Rahul Kumar","address":{"street":"Kalkaji","city":"Delhi","zipcode":"110020"},"phoneNumbers":["9923456469","9886786786"],"role":"Developer"}
 Convert Employee Json to Map Object >>{id=123, name=Rahul Kumar, address={street=Kalkaji, city=Delhi, zipcode=110020}, phoneNumbers=[9923456469, 9886786786], role=Developer}

How to read CSV file in Java

Posted on

Jackson data format module for reading and writing CSV-encoded data, either as “raw” data (sequence of String arrays ) , or via data binding to/from Java Objects (POJOs).

To use this extension on Maven-based projects, use following dependency:

<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-csv</artifactId>
  <version>2.7.0</version>
</dependency>

Read CSV file based on Java Class.

AS below schema Java Object have explicitly defined Order and properties type to ensured that reading or writing the data in given order.


package com.example.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonRootName;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * Created by MYPC on 7/18/2016.
 */
@JsonPropertyOrder(value = {"id", "amount", "currency"})
@JsonRootName("transaction")
public class Transaction implements Serializable {
    public enum Currency {
        INR, HKD, EUR, USD
    }
    @JsonProperty
    private String id;
    @JsonProperty
    @JsonFormat(shape = JsonFormat.Shape.NUMBER)
    private BigDecimal amount;
    @JsonProperty
    private Currency currency;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public double getAmount() {
        return amount.doubleValue();
    }
    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }
    public Currency getCurrency() {
        return currency;
    }
    public void setCurrency(Currency currency) {
        this.currency = currency;
    }
    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Transaction{");
        sb.append("id='").append(id).append('\'');
        sb.append(", amount=").append(amount);
        sb.append(", currency=").append(currency);
        sb.append('}');
        return sb.toString();
    }
}


Trade.csv  file reside in project “/src/main/resources” folder.

id,amount,currency
"1001",10000045.999,"EUR"
"1002",17777045.999,"HKD"
"1003",10000045.999,"USD"
"1004",1234045.999,"EUR"
"1005",100875.999,"HKD"
"1006",165780045.999,"EUR"
"1007",32452345.999,"INR"
"1008",7777777.999,"EUR"

 

Reading CSV file to Java Object.


package com.example;

import com.example.model.Transaction;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

import java.io.IOException;
import java.util.List;
/**
 * Created by MYPC on 7/18/2016.
 */
public class JSONSchemaReaderApp {

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

        CsvMapper csvMapper = new CsvMapper();
        CsvSchema csvSchema = csvMapper.typedSchemaFor(Transaction.class).withHeader();
        List list = new CsvMapper().readerFor(Transaction.class)
                .with(csvSchema.withColumnSeparator(CsvSchema.DEFAULT_COLUMN_SEPARATOR))
                .readValues(JSONSchemaReaderApp.class.getClassLoader().getResource("trade.csv"))
                .readAll();
        for (int i = 0; i < list.size(); i++) {
            System.out.printf(" Transaction Row [%d] : %s \n", i + 1, list.get(i));
        }
    }
}


Output:-

Transaction Row [1] : Transaction{id='1001', amount=10000045.999, currency=EUR}
Transaction Row [2] : Transaction{id='1002', amount=17777045.999, currency=HKD}
Transaction Row [3] : Transaction{id='1003', amount=10000045.999, currency=USD}
Transaction Row [4] : Transaction{id='1004', amount=1234045.999, currency=EUR}
Transaction Row [5] : Transaction{id='1005', amount=100875.999, currency=HKD}
Transaction Row [6] : Transaction{id='1006', amount=165780045.999, currency=EUR}
Transaction Row [7] : Transaction{id='1007', amount=32452345.999, currency=INR}
Transaction Row [8] : Transaction{id='1008', amount=7777777.999, currency=EUR}

Reading CSV file into Map


public static void main(String[] args) throws IOException {
        CsvMapper csvMapper = new CsvMapper();

        CsvSchema csvSchema = csvMapper.typedSchemaFor(Map.class).withHeader();
        MappingIterator<Map<String, String>> it = csvMapper.readerFor(Map.class).with(csvSchema.withColumnSeparator(CsvSchema.DEFAULT_COLUMN_SEPARATOR))
                .readValues(JSONSchemaReaderApp.class.getClassLoader().getResource("trade.csv"));
        List<Map<String, String>> listOfMapSchema = it.readAll();

        for (Map<String, String> map : listOfMapSchema) {
            System.out.println(map);
        }

    }

output:-

{id=1001, amount=10000045.999, currency=EUR}
{id=1002, amount=17777045.999, currency=HKD}
{id=1003, amount=10000045.999, currency=USD}
{id=1004, amount=1234045.999, currency=EUR}
{id=1005, amount=100875.999, currency=HKD}
{id=1006, amount=165780045.999, currency=EUR}
{id=1007, amount=32452345.999, currency=INR}
{id=1008, amount=7777777.999, currency=EUR}

Java 8 – Sorted Map By Key & Value

Posted on

Java-8

 

 

 

Below example will show, how to sort Collection & Map element by Key & Value using Lambda Expression in Java 8.

Example 1

In this example, we will see how to sort  element of Collection using Lambda expression.


        List listOfString = new ArrayList<>();
        listOfString.add("Z");
        listOfString.add("Q");
        listOfString.add("D");
        listOfString.add("A");
        listOfString.add("H");
        listOfString.add("T");

        listOfString.stream().sorted((e1,e2)->e1.compareTo(e2)).forEach(e->{
            System.out.println(" sorted list of String "+e);
        });
        // return new sorted list.
        /*        
        listOfString = listOfString.stream()
                                   .sorted((e1,e2)->e1.compareTo(e2))
                                   .collect(Collectors.toList());
        listOfString.forEach(e->{
            System.out.println(" sorted list of String "+e);
        });*/

Output:-
sorted list of String A
sorted list of String D
sorted list of String H
sorted list of String Q
sorted list of String T
sorted list of String Z

Example 2 : See how to sort a List’s Person by Name.

 


        List list = new ArrayList();

        list.add(new Person(6, "FF", 13, Person.SEX.MALE, "GOA"));
        list.add(new Person(7, "GG", 31, Person.SEX.FEMALE, "Delhi"));
        list.add(new Person(8, "HH", 30, Person.SEX.MALE, "Mumbai"));
        list.add(new Person(9, "II", 22, Person.SEX.FEMALE, "Delhi"));
        list.add(new Person(1, "AA", 12, Person.SEX.FEMALE, "Delhi"));
        list.add(new Person(2, "BB", 22, Person.SEX.FEMALE, "MP"));
        list.add(new Person(3, "CC", 44, Person.SEX.FEMALE,"UP"));
        list.add(new Person(4, "DD", 25, Person.SEX.MALE, "Gurgaon"));
        list.add(new Person(5, "EE", 51, Person.SEX.FEMALE, "GOA"));

         // sorted Person by name
        List sortedList = list.stream()
                .sorted((p1,p2)->p1.getName().compareTo(p2.getName()))
                .collect(Collectors.toList());
        sortedList.forEach(p->System.out.println("sorted by name :: "+p.toString()));


output :-
sorted by name :: Person{id=1, name=’AA’, age=12, gender=FEMALE, location=’Delhi’}
sorted by name :: Person{id=2, name=’BB’, age=22, gender=FEMALE, location=’MP’}
sorted by name :: Person{id=3, name=’CC’, age=44, gender=FEMALE, location=’UP’}
sorted by name :: Person{id=4, name=’DD’, age=25, gender=MALE, location=’Gurgaon’}
sorted by name :: Person{id=5, name=’EE’, age=51, gender=FEMALE, location=’GOA’}
sorted by name :: Person{id=6, name=’FF’, age=13, gender=MALE, location=’GOA’}
sorted by name :: Person{id=7, name=’GG’, age=31, gender=FEMALE, location=’Delhi’}
sorted by name :: Person{id=8, name=’HH’, age=30, gender=MALE, location=’Mumbai’}
sorted by name :: Person{id=9, name=’II’, age=22, gender=FEMALE, location=’Delhi’}

Example 3:- See how to sort a Map by value element


        //Sort a Map by their Value.
        Map<Integer, String> random = new HashMap<Integer, String>();
       
        random.put(1,"z");
        random.put(6,"k");
        random.put(5,"a");
        random.put(3,"f");
        random.put(9,"c");

        Map<Integer, String> sortedMap =
                random.entrySet().stream()
                        .sorted(Map.Entry.comparingByValue())
                        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                                (e1, e2) -> e2, LinkedHashMap::new));
        System.out.println("Sorted Map: " + Arrays.toString(sortedMap.entrySet().toArray()));

Output :-
Sorted Map: [5=a, 9=c, 3=f, 6=k, 1=z]

Example 4:- See how to sort a Map by Key element


        //Sort a Map by their Key.
        Map<Integer, String> random = new HashMap<Integer, String>();
       
        random.put(1,"z");
        random.put(6,"k");
        random.put(5,"a");
        random.put(3,"f");
        random.put(9,"c");

        Map<Integer, String> sortedMap =
                random.entrySet().stream()
                        .sorted(Map.Entry.comparingByKey())
                        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                                (e1, e2) -> e2, LinkedHashMap::new));
        System.out.println("Sorted Map: " + Arrays.toString(sortedMap.entrySet().toArray()));

Output :-
Sorted Map: [1=z, 3=f, 5=a, 6=k, 9=c]

Example 5:-

Here, A Map<String,Person> is created where String & Person object are taken as a Key & Value elements. First we will sort by Key elements then sort by Value elements in this below example.

Sorted by Map’s Key –


        Map<String,Person> map = new HashMap<>();
        map.put("g",new Person(5, "EE", 51, Person.SEX.FEMALE, "A"));
        map.put("a",new Person(4, "DD", 25, Person.SEX.MALE, "D"));
        map.put("e",new Person(3, "CC", 44, Person.SEX.FEMALE,"B"));

        Map<String,Person> sortedNewMap = map.entrySet().stream()
                .sorted((e1,e2)->e1.getKey().compareTo(e2.getKey()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (e1, e2) -> e1, LinkedHashMap::new));
        sortedNewMap.forEach((key,val)->{
            System.out.println(key+ " = "+ val.toString());
        });

Output:- 

a = Person{id=4, name=’DD’, age=25, gender=MALE, location=’D’}
e = Person{id=3, name=’CC’, age=44, gender=FEMALE, location=’B’}
g = Person{id=5, name=’EE’, age=51, gender=FEMALE, location=’A’}

Final output give us a Map which values are sorted location field –


        Map<String,Person> map = new HashMap<>();
        map.put("g",new Person(5, "EE", 51, Person.SEX.FEMALE, "A"));
        map.put("a",new Person(4, "DD", 25, Person.SEX.MALE, "D"));
        map.put("e",new Person(3, "CC", 44, Person.SEX.FEMALE,"B"));

        Map<String,Person> sortedNewMap = map.entrySet().stream().sorted((e1,e2)->
                e1.getValue().getLocation().compareTo(e2.getValue().getLocation()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (e1, e2) -> e1, LinkedHashMap::new));
        sortedNewMap.forEach((key,val)->{
            System.out.println(key+ " = "+ val.toString());
        });

Output:- 
g = Person{id=5, name=’EE’, age=51, gender=FEMALE, location=’A’}
e = Person{id=3, name=’CC’, age=44, gender=FEMALE, location=’B’}
a = Person{id=4, name=’DD’, age=25, gender=MALE, location=’D’}

 

There are lots of ways to do this work depends on the requirement. Hope you enjoy this post.
Thanks
——–