Found 2616 Articles for Java

How to ignore a field of JSON object using the Jackson library in Java?

raja
Updated on 06-Jul-2020 13:12:32

7K+ Views

The Jackson @JsonIgnore annotation can be used to ignore a certain property or field of a Java object. The property can be ignored both when reading JSON into Java objects and when writing Java objects into JSON. We can use the readValue() and writeValueAsString() methods of ObjectMapper class to read a JSON to Java Object and to write a Java object to JSON.Syntax@Target(value={ANNOTATION_TYPE, METHOD, CONSTRUCTOR, FIELD}) @Retention(value=RUNTIME) public @interface JsonIgnoreExampleimport java.io.*; import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; public class JsonIgnoreTest {    public static void main(String[] args) throws IOException {       Customer customer = new Customer("110", "Surya Kiran", "Chennai");       System.out.println(customer); ... Read More

How to deserialize a JSON string using @JsonCreator annotation in Java?

raja
Updated on 17-Feb-2020 08:18:20

793 Views

The @JsonProperty annotation can be used to indicate the property name in JSON. This annotation can be used for a constructor or factory method. The @JsonCreator annotation is useful in situations where the @JsonSetter annotation cannot be used. For instance, immutable objects do not have any setter methods, so they need their initial values injected into the constructor.@JsonProperty - ConstructorExampleimport com.fasterxml.jackson.annotation.*; import java.io.IOException; import com.fasterxml.jackson.databind.*; public class JsonCreatorTest1 {    public static void main(String[] args) throws IOException {       ObjectMapper om = new ObjectMapper();       String jsonString = "{\"id\":\"101\", \"fullname\":\"Ravi Chandra\", \"location\":\"Pune\"}";       System.out.println("JSON: " + jsonString);   ... Read More

Algorithm to generate positive rational numbers in Java

sudhir sharma
Updated on 16-Oct-2019 06:47:04

670 Views

Rational Numbers − A number that is expressed in the form of p/q. Given the condition that p and q both should be integers and q should not be equal to 0.Positive rational numbers are those numbers whose final values are positive. For this, either p and q both should be positive or p and q both should be negative.In this problem to generate positive random numbers up to a given number. We have to generate a finite number of positive rational numbers to n i.e. we will find rational numbers between 1 to n. For this algorithm, we will ... Read More

How to auto-increment the property of a JSONObject in Java?

raja
Updated on 06-Jul-2020 13:03:45

1K+ Views

A JSONObject is an unordered collection of name/value pairs and parses text from a String to produce a map-like object. However, we can auto-increment the property of a JSONObject using the increment() method of JSONObject class. If there is no such property, create one with a value of 1. If there is such a property and if it is an Integer, Long, Double or Float then add one to it.Syntaxpublic JSONObject increment(java.lang.String key) throws JSONExceptionExampleimport org.json.JSONException; import org.json.JSONObject; public class IncrementJSONObjectTest {    public static void main(String[] args) throws JSONException {       JSONObject jsonObj = new JSONObject();       jsonObj.put("year", 2019);     ... Read More

Convert JSONObject to/from a cookie in Java?

raja
Updated on 06-Jul-2020 13:05:15

1K+ Views

The JSON is one of the widely used data-interchange formats and is a lightweight and language independent. We can convert a JSONObject to cookie using the toString() method and convert a cookie to JSONObject using the toJSONObject() method of org.json.Cookie class.Convert JSONObject to cookieSyntaxpublic static java.lang.String toString(JSONObject jo) throws JSONExceptionExampleimport org.json.Cookie; import org.json.JSONObject; public class JSONObjectToCookieTest {    public static void main(String args[]) {       JSONObject jsonObject = new JSONObject();       jsonObject.put("path", "/");       jsonObject.put("expires", "Thu, 07 May 2020 12:00:00 UTC");       jsonObject.put("name", "username");       jsonObject.put("value", "Adithya");       String cookie = Cookie.toString(jsonObject); ... Read More

What is meant by re-throwing exceptions in Java?

Maruthi Krishna
Updated on 01-Nov-2019 10:22:40

4K+ Views

When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects).While re-throwing exceptions you can throw the same exception as it is without adjusting it as −try {    int result = (arr[a])/(arr[b]);    System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result); } catch(ArithmeticException e) {    throw e; }Or, wrap it within a new exception and throw it. When you wrap a cached exception within another exception and throw it, it is known as exception chaining or, exception wrapping, by doing this you can adjust your exception, throwing a ... Read More

How to avoid ConcurrentModificationException while iterating a collection in java?

Maruthi Krishna
Updated on 15-Oct-2019 10:35:53

298 Views

When you are working with collection objects, while one thread is iterating over a particular collection object, if you try to add or remove elements from it, a ConcurrentModificationException will be thrown.Not only that, If you are iterating a collection object, add or remove elements to it and try to iterate its contents again it is considered that you are trying to access the collection object using multiple threads and ConcurrentModificationException is thrown.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class OccurenceOfElements {    public static void main(String args[]) {       ArrayList list = new ArrayList();       ... Read More

What happens when we try to add a duplicate key into a HashMap object in java?

Maruthi Krishna
Updated on 15-Oct-2019 10:32:33

3K+ Views

The HashMap is a class that implements the Map interface. It is based on the Hash table. It allows null values and null keys.You can store key-value pairs in the HashMap object. Once you do so you can retrieve the values of the respective keys but, the values we use for keys should be uniqueDuplicate valuesThe put command associates the value with the specified key. i.e. if we add a key-value pair where the key exists already, this method replaces the existing value of the key with the new value, Example Live Demoimport java.util.HashMap; import java.util.Iterator; import java.util.Map; public class DuplicatesInHashMap ... Read More

How to insert an object in an ArrayList at a specific position in java?

Maruthi Krishna
Updated on 15-Oct-2019 10:29:34

797 Views

The add() method of the ArrayList class helps you to add elements to an array list. It has two variants −add(E e) − This method accepts an object/elements as a parameter and adds the given element at the end of the list.public void add(int index, E element) − This method accepts an element and an integer value representing the position at which we need to insert it and inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).Therefore ... Read More

How to search for a string in an ArrayList in java?

Maruthi Krishna
Updated on 15-Oct-2019 10:26:22

20K+ Views

The contains a () method of the String class accepts Sting value as a parameter, verifies whether the current String object contains the specified string and returns true if it does (else false).Therefore, to for a string in an ArrayList −Get the array list.Using the for-each loop get each element of the ArrayList object.Verify whether each element in the array list contains the required string.If so print the elements.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class FindingString{    public static void main(String[] args){       ArrayList list = new ArrayList();       //Instantiating an ArrayList object     ... Read More

Advertisements