import com.fasterxml.jackson.annotation.JsonAnySetter; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) throws JsonProcessingException { String json = "{\n" + " \"customObject\": {\n" + " \"value\": {\n" + " \"config-1\": {\n" + " \"configurationName\": {\n" + " \"de\": \"Das ist ein Text\",\n" + " \"en\": \"Hospitalization Cover\"\n" + " },\n" + " \"key\": \"hospitalization-cover\",\n" + " \"mandatory\": false,\n" + " \"type\": \"options\",\n" + " \"options\": {\n" + " \"1Lakh\": {\n" + " \"label\": \"1 Lakh\",\n" + " \"priceType\": \"fixed\",\n" + " \"price\": \"200\",\n" + " \"default\": \"true\"\n" + " },\n" + " \"2Lakh\": {\n" + " \"label\": \"2 Lakh\",\n" + " \"priceType\": \"fixed\",\n" + " \"price\": \"400\"\n" + " },\n" + " \"3Lakh\": {\n" + " \"label\": \"3 Lakh\",\n" + " \"priceType\": \"fixed\",\n" + " \"price\": \"450\"\n" + " },\n" + " \"4Lakh\": {\n" + " \"label\": \"4 Lakh\",\n" + " \"priceType\": \"fixed\",\n" + " \"price\": \"500\"\n" + " },\n" + " \"5Lakh\": {\n" + " \"label\": \"5 Lakh\",\n" + " \"priceType\": \"fixed\",\n" + " \"price\": \"550\"\n" + " },\n" + " \"6Lakh\": {\n" + " \"label\": \"6 Lakh\",\n" + " \"priceType\": \"fixed\",\n" + " \"price\": \"650\"\n" + " },\n" + " \"7Lakh\": {\n" + " \"label\": \"7 Lakh\",\n" + " \"priceType\": \"fixed\",\n" + " \"price\": \"780\"\n" + " }\n" + " }\n" + " },\n" + " \"config-2\": {\n" + " \"configurationName\": {\n" + " \"de\": \"Das ist ein Text\",\n" + " \"en\": \"OPD Cover\"\n" + " },\n" + " \"key\": \"opd-cover\",\n" + " \"mandatory\": false,\n" + " \"type\": \"number\",\n" + " \"options\": {\n" + " \"OPDCover10k\": {\n" + " \"label\": \"10,000\",\n" + " \"priceType\": \"calculated\",\n" + " \"priceFactor\": 0.2,\n" + " \"default\": \"true\"\n" + " },\n" + " \"OPDCover15k\": {\n" + " \"label\": \"15,000\",\n" + " \"priceType\": \"calculated\",\n" + " \"priceFactor\": 0.27\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}"; ObjectMapper mapper = new ObjectMapper(); GqCustomObject res = mapper.readValue(json, GqCustomObject.class); System.out.println(res); } static class GqCustomObject { private GqConfigCustomPOValue customObject; public GqConfigCustomPOValue getCustomObject() { return customObject; } public void setCustomObject(GqConfigCustomPOValue customObject) { this.customObject = customObject; } @Override public String toString() { return "GqCustomObject{" + "customObject=" + customObject + '}'; } } static class GqConfigCustomPOValue { public GqCustomPOCAList value; public GqCustomPOCAList getValue() { return value; } public void setValue(GqCustomPOCAList value) { this.value = value; } @Override public String toString() { return "GqConfigCustomPOValue{" + "value=" + value + '}'; } } static class GqCustomPOCAList { // 1. Do not use a List to parse an unknown number of configs, // use instead the @JsonAnyGetter annotation and a Map to add every config with their name as the key (config-1, config-2, etc.) public Map<String, GqCustomPOConfigAttributes> config; public GqCustomPOCAList(){ config = new HashMap<>(); // 2. initializing the Map so that it can be used by the method marked with @JsonAnySetter } // 3. Method to add a generic config to the Map @JsonAnySetter public void addConfig(String cofigName, GqCustomPOConfigAttributes value){ config.put(cofigName, value); } public Map<String, GqCustomPOConfigAttributes> getConfig() { return config; } public void setConfig(Map<String, GqCustomPOConfigAttributes> config) { this.config = config; } @Override public String toString() { return "GqCustomPOCAList{" + "config=" + config + '}'; } } static class GqCustomPOConfigAttributes { // 4. configurationName appears to be a Map<String, String> within your Json, not a String private Map<String, String> configurationName; private String key; private boolean mandatory; private String type; // 5. Do not use a generic Object to represent your option, define a class Option with the possible fields. // 6. Again, if the possible options are unknown, use a Map<String, Option> and the @JsonAnySetter annotation private Map<String, Option> options; public GqCustomPOConfigAttributes(){ configurationName = new HashMap<>(); // 7. initializing the Map so that it can be used by the method marked with @JsonAnySetter } // 8. Method to add a generic option to the Map @JsonAnySetter public void addOption(String optionName, Option option){ options.put(optionName, option); } public Map<String, String> getConfigurationName() { return configurationName; } public void setConfigurationName(Map<String, String> configurationName) { this.configurationName = configurationName; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public boolean isMandatory() { return mandatory; } public void setMandatory(boolean mandatory) { this.mandatory = mandatory; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Map<String, Option> getOptions() { return options; } public void setOptions(Map<String, Option> options) { this.options = options; } @Override public String toString() { return "GqCustomPOConfigAttributes{" + "configurationName=" + configurationName + ", key='" + key + '\'' + ", mandatory=" + mandatory + ", type='" + type + '\'' + ", options=" + options + '}'; } } static class Option { private String label; private String priceType; private BigDecimal price; private BigDecimal priceFactor; //9. the 'default' property must be renamed as default is a java keyword, // use @JsonProperty to bind it to the field 'default' within the Json @JsonProperty("default") private boolean isDefault; public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public String getPriceType() { return priceType; } public void setPriceType(String priceType) { this.priceType = priceType; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } public BigDecimal getPriceFactor() { return priceFactor; } public void setPriceFactor(BigDecimal priceFactor) { this.priceFactor = priceFactor; } public boolean isDefault() { return isDefault; } public void setDefault(boolean aDefault) { isDefault = aDefault; } @Override public String toString() { return "Option{" + "label='" + label + '\'' + ", priceType='" + priceType + '\'' + ", price=" + price + ", priceFactor=" + priceFactor + ", isDefault=" + isDefault + '}'; } } }
Write, Run & Share Java code online using OneCompiler's Java online compiler for free. It's one of the robust, feature-rich online compilers for Java language, running the Java LTS version 17. Getting started with the OneCompiler's Java editor is easy and fast. The editor shows sample boilerplate code when you choose language as Java and start coding.
OneCompiler's Java online editor supports stdin and users can give inputs to the programs using the STDIN textbox under the I/O tab. Using Scanner class in Java program, you can read the inputs. Following is a sample program that shows reading STDIN ( A string in this case ).
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your name: ");
String inp = input.next();
System.out.println("Hello, " + inp);
}
}
OneCompiler supports Gradle for dependency management. Users can add dependencies in the build.gradle
file and use them in their programs. When you add the dependencies for the first time, the first run might be a little slow as we download the dependencies, but the subsequent runs will be faster. Following sample Gradle configuration shows how to add dependencies
apply plugin:'application'
mainClassName = 'HelloWorld'
run { standardInput = System.in }
sourceSets { main { java { srcDir './' } } }
repositories {
jcenter()
}
dependencies {
// add dependencies here as below
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
}
Java is a very popular general-purpose programming language, it is class-based and object-oriented. Java was developed by James Gosling at Sun Microsystems ( later acquired by Oracle) the initial release of Java was in 1995. Java 17 is the latest long-term supported version (LTS). As of today, Java is the world's number one server programming language with a 12 million developer community, 5 million students studying worldwide and it's #1 choice for the cloud development.
short x = 999; // -32768 to 32767
int x = 99999; // -2147483648 to 2147483647
long x = 99999999999L; // -9223372036854775808 to 9223372036854775807
float x = 1.2;
double x = 99.99d;
byte x = 99; // -128 to 127
char x = 'A';
boolean x = true;
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
// code
} else {
// code
}
Example:
int i = 10;
if(i % 2 == 0) {
System.out.println("i is even number");
} else {
System.out.println("i is odd number");
}
Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.
switch(<conditional-expression>) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
//code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition. Usually for loop is preferred when number of iterations is known in advance.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(<condition>){
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (<condition>);
Class is the blueprint of an object, which is also referred as user-defined data type with variables and functions. Object is a basic unit in OOP, and is an instance of the class.
class
keyword is required to create a class.
class Mobile {
public: // access specifier which specifies that accessibility of class members
string name; // string variable (attribute)
int price; // int variable (attribute)
};
Mobile m1 = new Mobile();
public class Greeting {
static void hello() {
System.out.println("Hello.. Happy learning!");
}
public static void main(String[] args) {
hello();
}
}
Collection is a group of objects which can be represented as a single unit. Collections are introduced to bring a unified common interface to all the objects.
Collection Framework was introduced since JDK 1.2 which is used to represent and manage Collections and it contains:
This framework also defines map interfaces and several classes in addition to Collections.
Collection | Description |
---|---|
Set | Set is a collection of elements which can not contain duplicate values. Set is implemented in HashSets, LinkedHashSets, TreeSet etc |
List | List is a ordered collection of elements which can have duplicates. Lists are classified into ArrayList, LinkedList, Vectors |
Queue | FIFO approach, while instantiating Queue interface you can either choose LinkedList or PriorityQueue. |
Deque | Deque(Double Ended Queue) is used to add or remove elements from both the ends of the Queue(both head and tail) |
Map | Map contains key-values pairs which don't have any duplicates. Map is implemented in HashMap, TreeMap etc. |