Skip to main content

Posts

Showing posts from March, 2020

Jackson – Convert JSON array string to List

1.JSON array string

[{"name":"knowledgefactory1", "salaray":1000}, {"name":"knowledgefactory2", "salaray":1200}]

2. Create an object to map the above JSON fields.
package com.knf;

public class Employee {

String name;
Double salaray;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Double getSalaray() {
return salaray;
}

public void setSalaray(Double salaray) {
this.salaray = salaray;
}

@Override
public String toString() {
return "Employee [name=" + name + ", salaray=" + salaray + "]";
}

} 3.Convert the JSON array string to a List
package com.knf;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class JacksonArrayExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
String json = "[{\"name\&q…

Java Program to find Largest Number in an Array

We can find the largest number in an array in java by sorting the array and returning the largest number. Let's see the full example to find the largest number in java array.


package com.knf;

public class LargestInArrayExample1 {

public static void main(String args[]) {
int a[] = { 2, 1, 2, 5, 6, 3, 2, 9 };
int b[] = { 144, 666, 99, 677, 433, 422, 255 };
System.out.println("Largest: " + getLargest(a));
System.out.println("Largest: " + getLargest(b));
}

public static int getLargest(int[] a) {
int temp;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[a.length - 1];
}
}
Output:

Largest: 9
Largest: 677

Example 2 :Using Arrays
Let's see another example to get largest element in java array using Arrays.

package com.knf;

import java.util.Arrays;

public class LargestInArrayExample2 {

public static void main(String args[]) {
int a[] = { 2, 1, 2, 5, 6, 3, 2, 9 };
int b[] = { 144, 666, …

Java - Generics

In any nontrivial software project, bugs are simply a fact of life. Careful planning, programming, and testing can help reduce their pervasiveness, but somehow, somewhere, they'll always find a way to creep into your code. This becomes especially apparent as new features are introduced and your code base grows in size and complexity.

Fortunately, some bugs are easier to detect than others. Compile-time bugs, for example, can be detected early on; you can use the compiler's error messages to figure out what the problem is and fix it, right then and there. Runtime bugs, however, can be much more problematic; they don't always surface immediately, and when they do, it may be at a point in the program that is far removed from the actual cause of the problem.

Generics add stability to your code by making more of your bugs detectable at compile time.

Generic Methods
You can write a single generic method declaration that can be called with arguments of different types. Based on th…