Sort user defined objects/POJO in array (java/example/Comparator interface)

Given an array of user defined objects/POJO in java. We would like to sort user defined objects on certain properties/data members. We will use Arrays.sort method to sort objects. 1. Example – Sort POJO or user defined object using comparator interface. Given a Array containing Vehicle objects. Vehicle class has following data members. public String model public… Read More »

Copy properties from one bean into another bean/object (java/ example)

Given a source bean or object in java. We would like to copy properties from source bean into destination bean in java. i.e. We would like to convert one object into another object. 1. Example: Convert one object into another object in java (copy properties) Given an EmployeeDB object containing information about employees, present in… Read More »

Display currency code/ symbol from Locale in java (example)

Given Locale for given language & country code in java. We would like to display currency code & symbol for given locale. 1. Example: Display currency code/ symbol from Locale in java Suppose we would like to display currency code for country=US and Language=English. We would use default locale class to get currency information. We should… Read More »

CopyOnWriteArrayList concurrent collection in java (example)

CopyOnWriteArrayList is thread-safe variant of ArrayList. New copy of underlying array is created when any add, set (or modification) operations performed on CopyOnWriteArrayList. CopyOnWriteArrayList can be visualize as combination of words, COPY + On Write + ArrayList. New copy of underlying array is created, when any write/update/remove operation is performed on CopyOnWriteArrayList. i.e. When any… Read More »

Read contents from console or command line (Scanner/ Java/ Example)

Scanner class is a text parser, which is used to parse primitives & strings using regular expression. Scanner split the input into token using delimiter pattern. Default delimiter pattern is a whitespace. We will read the contents from console or command line using Scanner class. Scanner class extends object & implements Closeable & Iterable interface. 1.… Read More »

Java Scanner example – read & write contents to/ from file (example)

Scanner is text parser which used to parse primitives & strings using regular expression. Scanner split the input into token using delimiter pattern. Default pattern delimiter is whitespace. We will write content to a file using FileWriter class. Then, we will read content from as file using Scanner class. Scanner class extends object & implements… Read More »

Page 2 of 4212345...102030...Last »