Found 2619 Articles for Java

Regular Expression "re*" Metacharacter in Java

Maruthi Krishna
Updated on 18-Nov-2019 11:59:46

89 Views

The subexpression/metacharacter “re*” matches 0 or more occurrences of the preceding expression.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "aabc*";       String input = "aabcabcaabcabbcaabcbcaabc";       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count++;       }       System.out.println("Number of matches: "+count);    } }OutputNumber of matches: 4Example 2import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchAllCharacters ... Read More

When to use @ConstructorProperties annotation with Jackson in Java?

raja
Updated on 09-Jul-2020 07:19:56

1K+ Views

The @ConstructorProperties annotation is from java.beans package, used to deserialize JSON to java object via the annotated constructor. This annotation supports from Jackson 2.7 version onwards. The way this annotation works very simple, rather than annotating each parameter in the constructor, we can provide an array with the properties names for each of the constructor parameters.Syntax@Documented @Target(value=CONSTRUCTOR) @Retention(value=RUNTIME) public @interface ConstructorPropertiesExampleimport com.fasterxml.jackson.databind.ObjectMapper; import java.beans.ConstructorProperties; public class ConstructorPropertiesAnnotationTest {    public static void main(String args[]) throws Exception {       ObjectMapper mapper = new ObjectMapper();       Employee emp = new Employee(115, "Raja");       String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp);   ... Read More

Explain Regular Expression "A" Metacharacter in Java

Maruthi Krishna
Updated on 18-Nov-2019 11:57:12

161 Views

The subexpression/metacharacter “\A” matches the beginning of the entire string.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "\AHi";       String input = "Hi how are you welcome to Tutorialspoint";       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count++;       }       System.out.println("Number of matches: "+count);    } }OutputNumber of matches: 1Example2Following Java program accepts a string ... Read More

Regular Expression "." (dot) Metacharacter in Java

Maruthi Krishna
Updated on 18-Nov-2019 10:29:25

770 Views

The subexpression/metacharacter “.” matches any single character except a newline.Example1import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchesAll {    public static void main( String args[] ) {       String regex = ".";       String input = "Hi how are you welcome to Tutorialspoint";       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count++;       }       System.out.println("Number of matches: "+count);    } }OutputNumber of matches: 40Example 2The following Java program accepts 5 ... Read More

Regular Expression "$" (dollar) Metacharacter in Java

Maruthi Krishna
Updated on 18-Nov-2019 10:17:40

769 Views

The subexpression/metacharacter “$” matches the end of a line.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class EndWith {    public static void main( String args[] ) {       String regex = "Tutorialspoint$";       String input = "Hi how are you welcome to Tutorialspoint";       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count++;          System.out.println("Number of matches: "+count);       }    } }OutputNumber of matches: 1Example 2The following Java program accepts ... Read More

Regular Expression "^" (caret) Metacharacter in Java

Maruthi Krishna
Updated on 18-Nov-2019 10:12:52

432 Views

The subexpression/metacharacter “^” matches the beginning of a line. If you use this in a regular expression, it matches the sentence succeeding it in the input string.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "^Hi how are you";       String input = "Hi how are you welcome to Tutorialspoint";       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count++;     ... Read More

How to add/insert additional property to JSON string using Gson in Java?

raja
Updated on 19-Feb-2020 10:28:54

7K+ Views

The com.google.gson.JSonElement class represents an element of Json. We can use the toJsonTree() method of Gson class to serialize an object's representation as a tree of JsonElements. We can add/ insert an additional property to JSON string by using the getAsJsonObject() method of JSonElement. This method returns to get the element as JsonObject.Syntaxpublic JsonObject getAsJsonObject()Exampleimport com.google.gson.*; public class AddPropertyGsonTest {    public static void main(String[] args) {       Gson gson = new GsonBuilder().setPrettyPrinting().create(); // pretty print JSON       Student student = new Student("Adithya");       String jsonStr = gson.toJson(student, Student.class);       System.out.println("JSON String: " + jsonStr);       ... Read More

Difference between fail-fast and fail safe in Java

Mahesh Parahar
Updated on 18-Nov-2019 07:31:59

2K+ Views

Sr. No.KeyFail-FastFail-Safe1ExceptionAny changes in the collection, such as adding, removing and updating collection during a thread are iterating collection then Fail fast throw concurrent modification exception. The fail-safe collection doesn't throw exception. 2.Type of collectionArrayList and hashmap collection are the examples of fail-fast iterator CopyOnWrite and concurrent modification are the examples of a fail-safe iterator 3.Performance and MemoryIt's work on actual collection instead. So, this iterator doesn't require extra memory and time It's working on a clone of the collection instead of actual collection. It is overhead in terms of time and memory4.Modifications Iterators don't allow modifications of a collection while iterating over it.Fail-Safe iterators allow ... Read More

Difference between volatile and transient in java

Mahesh Parahar
Updated on 18-Nov-2019 07:24:40

8K+ Views

A volatile keyword is used in a multithreading environment where two threads reading and writing the same variable simultaneously. The volatile keyword flushes the changes directly to the main memory instead of the CPU cache. On the other hand, the transient keyword is used during serialization. Fields that are marked as transient can not be part of the serialization and deserialization. We don't want to save the value of any variable then we use transient keyword with that variable. Sr. No.KeyVolatileTransient1Basic Volatile keyword is used to flush changes directly to the main memoryThe transient keyword is used to exclude variable during serialization 2.Default value Volatile ... Read More

Difference between List and Set in Java

Mahesh Parahar
Updated on 05-Dec-2023 09:48:36

2K+ Views

List and Set both interface belongs to the Collection framework. Both interfaces extend the Collection interface. They both are used to store a collection of objects as a single unit. Before jdk1.2, we used to use Arrays, Vectors, and Hashtable for grouping objects as a single unit. Sr. No. Key List Set 1 Positional Access The list provides positional access of the elements in the collection. Set doesn't provide positional access to the elements in the collection. 2 Implementation Implementation of List are ArrayList, LinkedList, Vector ,Stack. Implementation of a set interface is HashSet and LinkedHashSet. 3 Duplicate We can store the duplicate elements in the list. We can’t store duplicate elements in Set. 4 Ordering List maintains ... Read More

Advertisements