Posted In: Java Core, String

How to read and parse CSV file using String Pattern

Splitting a comma-separated string but ignoring commas in quotes Parsing CSV input with a RegEx in java

Read More

Tags:

by , on August 29th, 2017

Posted In: Java Core, String

How to read and parse CSV file using Open CSV

Splitting a comma-separated string but ignoring commas in quotes Maven opencsv Example csv (cm24AUG2017bhav.csv) Example csv commas in quotes (cm24AUG2017bhav1.csv) opencsv import With deprecated CSVReader. Use CSVReaderBuilder With deprecated CSVReader. Use CSVReaderBuilder. commas in quotes data With CSVReaderBuilder With CSVReaderBuilder. commas in quotes data With CsvToBean and ColumnPositionMappingStrategy. CsvToBean.parse is deprecated With header element name

Read More

Tags:

by , on August 29th, 2017

Posted In: Java Core, String

How to read and parse CSV file using Apache Commons CSV

Splitting a comma-separated string but ignoring commas in quotes Maven commons-csv Example csv Example csv commons csv import With element index With header element name With header name and with commas in quotes With header name RFC4180 format

Read More

Tags:

by , on August 29th, 2017

Posted In: IO, Java Core

How to create a temporary file/directory in Java

JDK 7 createTempDirectory and createTempFile method creates temp folder with given suffix. I have also added deleteOnExit to delete temp folder but dir.toFile().deleteOnExit() does not seem to be reliable on Windows. package com.example.java.core; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import org.junit.Test; public class TempFile { @Test public void create() throws Exception { Path dir = […]

Read More

by , on August 24th, 2017

Posted In: Date, Java Core

Convert java.util.Date to java.time.LocalDate, LocalDateTime

Date date1 = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”) .parse(“2017-08-13 12:10:30”); LocalDate localdate = date1.toInstant().atZone(ZoneId.systemDefault()) .toLocalDate(); LocalDateTime localdatetime = date1.toInstant().atZone(ZoneId.systemDefault()) .toLocalDateTime();

Read More

by , on August 24th, 2017

Posted In: Date, Java Core

Comparing two Dates without the time portion

Techniques used 1. JODA Time DateTimeComparator 2. Apache Commons DateUtils 3. JDK 8 LocalDate 4. Below JDK 8 DateFormat  

Read More

by , on August 24th, 2017

Posted In: Java Core

How to determine the size of an object in Java

JDK 5 provides Instrumentation interface which can be used find Object Size Instrumentation set up requires entry to manifest file Premain-Class: ObjectAgent and use public static void premain(String agentArgs, Istrumentation instrumentation). There is little easy way to do it. Use memory-measurer Run class with javaagent VM argument  

Read More

by , on August 24th, 2017

Posted In: Java Core, String

String vs StringBuilder vs StringBuffer Simple Benchmark test

Since String is immutable in java any changes to it creates a new object. StringBuffer and StringBuilder are mutable objects. They provide methods like append(), insert(), delete() and substring() to manipulate date.   This test clearly shows that always use StringBuilder. It will perform much better than String concatenation. Result

Read More

by , on August 24th, 2017

Posted In: Java Core, String

When to use StringBuilder vs String concatenation

Most of the discussions say that for few fields String concatenation is fine. If code is looping then do use StringBuilder. Use StringBuilder for following kind of code There is slight different issue with this may be approach. Applications which go through various static code analysis tools should always use StringBuilder append than String concatenation. […]

Read More

by , on August 24th, 2017

Posted In: Collections, Java Core

Randomly change order of elements in ArrayList

Shuffle array to change order To pick random object from ArrayList java.util.Collections shuffle code

Read More

by , on August 23rd, 2017

  • Categories