Implement Numbering in MySQL Group Concat

AmitDiwan
Updated on 08-Jul-2020 11:58:17

538 Views

Let us first create a table −mysql> create table DemoTable1627     -> (     -> FirstName varchar(20),     -> LastName varchar(20)     -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command.mysql> insert into DemoTable1627 values('John', 'Smith'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1627 values('John', 'Doe'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1627 values('Adam', 'Smith'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1627 values('Carol', 'Taylor'); Query OK, 1 row affected (0.08 sec)Display all records from the table using ... Read More

Find Reminder of Array Multiplication Divided by N in Java

AmitDiwan
Updated on 08-Jul-2020 11:58:16

234 Views

To find reminder of array multiplication divided by n, the Java code is as follows −Example Live Demoimport java.util.*; import java.lang.*; public class Demo{    public static int remainder(int my_arr[], int arr_len, int val){       int mul_val = 1;       for (int i = 0; i < arr_len; i++)       mul_val = (mul_val * (my_arr[i] % val)) % val;       return mul_val % val;    }    public static void main(String argc[]){       int[] my_arr = new int []{ 35, 100, 69, 99, 27, 88, 12, 25 }; ... Read More

Expand a String If Range Is Given in Java

AmitDiwan
Updated on 08-Jul-2020 11:53:10

337 Views

To expand a String if range is given, the Java code is as follows −Example Live Demopublic class Demo {    public static void expand_range(String word) {       StringBuilder my_sb = new StringBuilder();       String[] str_arr = word.split(", ");       for (int i = 0; i < str_arr.length; i++){          String[] split_str = str_arr[i].split("-");          if (split_str.length == 2){             int low = Integer.parseInt(split_str[0]);             int high = Integer.parseInt(split_str[split_str.length - 1]);             while (low

Count Trailing Zeroes in Factorial of a Number in Java

AmitDiwan
Updated on 08-Jul-2020 11:47:03

826 Views

To count trailing zeroes in factorial of a number, the Java code is as follows −Example Live Demoimport java.io.*; public class Demo{    static int trailing_zero(int num){       int count = 0;       for (int i = 5; num / i >= 1; i *= 5){          count += num / i;       }       return count;    }    public static void main (String[] args){       int num = 1000000;       System.out.println("The number of trailing zeroes in " + num +" factorial is " + ... Read More

Count Characters in Each Word of a Given Sentence in Java

AmitDiwan
Updated on 08-Jul-2020 11:43:04

506 Views

To count the characters in each word in a given sentence, the Java code is as follows −Example Live Demoimport java.util.*; public class Demo{    static final int max_chars = 256;    static void char_occurence(String my_str){       int count[] = new int[max_chars];       int str_len = my_str.length();       for (int i = 0; i < str_len; i++)       count[my_str.charAt(i)]++;       char ch[] = new char[my_str.length()];       for (int i = 0; i < str_len; i++){          ch[i] = my_str.charAt(i);          int find = 0;          for (int j = 0; j

Convert Iterator to Spliterator in Java

AmitDiwan
Updated on 08-Jul-2020 11:39:44

138 Views

To convert Iterator to Spliterator, the Java code is as follows −Example Live Demoimport java.util.*; public class Demo{    public static Spliterator getspiliter(Iterator iterator){       return Spliterators.spliteratorUnknownSize(iterator, 0);    }    public static void main(String[] args){       Iterator my_iter = Arrays.asList(56, 78, 99, 32, 100, 234).iterator();       Spliterator my_spliter = getspiliter(my_iter);       System.out.println("The values in the spliterator are : ");       my_spliter.forEachRemaining(System.out::println);    } }OutputThe values in the spliterator are : 56 78 99 32 100 234A class named Demo contains a function named ‘getspiliter’ that returns a spliterator. In ... Read More

Check if Array Digits Can Form a Divisible by 3 Number in Java

AmitDiwan
Updated on 08-Jul-2020 11:37:16

232 Views

To check whether it is possible to make a divisible by 3 number using all digits in an array, the Java code is as follows −Example Live Demoimport java.io.*; import java.util.*; public class Demo{    public static boolean division_possible(int my_arr[], int n_val){       int rem = 0;       for (int i = 0; i < n_val; i++)       rem = (rem + my_arr[i]) % 3;       return (rem == 0);    }    public static void main(String[] args){       int my_arr[] = { 66, 90, 87, 33, 123}; ... Read More

When to Use @JsonManagedReference and @JsonBackReference Annotations in Jackson

raja
Updated on 08-Jul-2020 11:36:22

5K+ Views

The @JsonManagedReference and @JsonBackReference annotations can be used to create a JSON structure in a bidirectional way. The @JsonManagedReference annotation is a forward reference that includes during the serialization process whereas @JsonBackReference annotation is a backreference that omits during the serialization process.In the below example, we can implement @JsonManagedReference and @JsonBackReference annotations.Exampleimport java.util.*; import com.fasterxml.jackson.annotation.JsonManagedReference; import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.JsonProcessingException; public class ManagedReferenceBackReferenceTest {    public static void main(String args[]) throws JsonProcessingException {       BackReferenceBeanTest testBean = new BackReferenceBeanTest(110, "Sai Chaitanya");       ManagedReferenceBeanTest bean = new ManagedReferenceBeanTest(135, "Adithya Ram", testBean);       testBean.addEmployees(bean);       ObjectMapper mapper = ... Read More

Check If Count of Divisors Is Even or Odd in Java

AmitDiwan
Updated on 08-Jul-2020 11:35:48

211 Views

To check if the count of divisors is even or odd, the Java code is as follows −Example Live Demoimport java.io.*; import java.math.*; public class Demo{    static void divisor_count(int n_val){       int root_val = (int)(Math.sqrt(n_val));       if (root_val * root_val == n_val){          System.out.println("The number of divisors is an odd number");       }else{          System.out.println("The number of divisors is an even number");       }    }    public static void main(String args[]) throws IOException{       divisor_count(25);    } }OutputThe number of divisors is an ... Read More

Convert JSON Object to Bean Using JSON-lib API in Java

raja
Updated on 08-Jul-2020 11:34:52

3K+ Views

The JSONObject class is a collection of name/value pairs (unordered) where the bean is a class with setter and getter methods for its member fields. We can convert a JSON object to a bean using the toBean() method of JSONObject class.Syntaxpublic static Object toBean(JSONObject jsonObject, Class beanClass)Exampleimport net.sf.json.JSONObject; public class ConvertJSONObjToBeanTest {    public static void main(String[] args) {       mployee emp = new Employee("Sai", "Ram", 30, "Bangalore");       JSONObject jsonObj = JSONObject.fromObject(emp);       System.out.println(jsonObj.toString(3)); // pretty print JSON       emp = (Employee)JSONObject.toBean(jsonObj, Employee.class);       System.out.println(emp.toString());    }    // Employee class    public static ... Read More

Advertisements