Menu

Void elements in HTML

There are six categories of HTML elements. Void element is one among the six categories that have the following HTML elements. Void elements or tags do not need the closing tag and always be closed in the same opening tag, that's why we also refer to these elements as self-closing elements. Void elements can't have child elements or content.

  1. area
  2. base
  3. br
  4. col
  5. embed
  6. hr
  7. img
  8. input
  9. link
  10. meta
  11. param
  12. source
  13. track
  14. wbr

How to iterate on a JSON objects using Java

Today we are going to see how to iterate on a JSON object or read a JSON file and fetch key and value from that JSON file using Java.

Below is the example code snippet.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package rashidjorvee;

import java.io.FileReader;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class JsonReader {
 /* JSON data file
  * [
    {
      "componentName": "comp1""compKey": [
        {
          "keyname": "json_key1",
          "props": [
            {
              "fieldLabel": "Jorvee",
              "name": "./productProperty",
              "id": "productID",
              "sling:resourceType": "granite/ui/components/foundation/form/textfield"
            }
          ]
        }
      ]
    },
    {
      "componentName": "comp2""compKey": [
        {
          "keyname": "json_key2",
          "props": [
            {
              "fieldLabel": "Rashid",
              "name": "./productProperty",
              "id": "productID",
              "sling:resourceType": "foundation/components/parsys"
            }
          ]
        }
      ]
    }
  ]
  */
 public static void main(String[] args) {
  try{
   JSONParser parser=new JSONParser();
   JSONArray a = (JSONArray) parser.parse(new FileReader("D:\\json/jsonFile.json"));
   for (Object comp:a) {
    JSONObject key1 = (JSONObject) comp;
    String componentName = (String) key1.get("componentName");
    System.out.println("componentName: "+componentName);
    JSONArray compkeys = (JSONArray) key1.get("compKey");
    for (Object o : compkeys)
    {
     JSONObject key = (JSONObject) o;
     String keyName = (String) key.get("keyname");
     System.out.println("KeyName: "+keyName);
     
     JSONArray keys = (JSONArray) key.get("props");
     for (Object prop : keys) {
      JSONObject properties = (JSONObject) prop;
      
      String name = (String) properties.get("name");
      System.out.println("Name: " +name);
      
      String fieldLabel = (String) properties.get("fieldLabel");
      System.out.println("City: "+fieldLabel);
      
      String job = (String) properties.get("sling:resourceType");
      System.out.println("resourceType: "+job);
      
      String id = (String) properties.get("id");
      System.out.println("ID: "+id);
      
     }
     System.out.println("\n");
    }
   }
  } catch(Exception e) {}

 }

}

How to store multiple values for a key into Map using JAVA

Today we will practice a program to assign or set multiple values or list of values or ArrayList for a key in Map. And also how to iterate on that map and find the value from the list of values.

Declare a Map, with a string or integer key and list or ArrayList type of value; as given in below code in line 10. In this Map, you can store a single value as a key and a list of values you could store into that key in form of a list or array list.
Line 10: Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
In line 12 we are creating an ArrayList in which we will store the list of values and we will put this list into Map.
Line 12: ArrayList<String> list = new ArrayList<String>();
Later in line 13 and 14, we are adding values in ArrayList.
Line 13 and 14: list.add("A"+i);
                          list.add("B"+i);
On top of this ArrayList we have created a for loop which will help us to create the pair of 10 unique values of key and value and mapped and put those values into the map.

Line 15 of the code is to put a pair of key and value into a map.
Line 15: map.put("index"+i, list);
We have completed the creation of map. Now we will see how to iterate through the map and pull a particular key or value. or how to find a value into map? or how to find a matching value from a list stored into a map?
To iterate on map first we have to create an object of Entry class with the help of Map and entrySet(). Below is the foreach loop using that we can iterate on all keys of the map.  
Line 19: for(Map.Entry<String, ArrayList<String>> entry : map.entrySet())
Now using the entry object we can pull the key and values of the current iteration index. Below are the codes to getKey() and getValue() from the map. Line 20 of the code will return the value stored as a key, and line 21 will return a list of values stored into the value of the map of the current index.
Line 20: String key = entry.getKey();Line 21: ArrayList<String> value = entry.getValue();
To find a value from map or the list of values stored into the map we will use the matches() method and using regex we could find the matching values at the specified index. In line 23 of the code, we are searching a value B5 which we have stored in the list of values. If we have a list of values then we have to specify the index number using get(index) in which method matches() will search for the requested value.
Line 23: if(entry.getValue().get(1).matches("B5")) 
To search a value from simple key-value pairs of map then we could simply write the above line without get() method. e.g. if(entry.getValue().matches("B5")) and in similar way you could find a key as well. e.g. if(entry.getKey().matches("index7"))

Below is the complete example code which you could directly copy and paste in your IDE and practice this exercise.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package rashidjorvee;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MultipleValuesInMap {

 public static void main(String[] args) {
  Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
  for(int i=1; i<=10; i++) {
   ArrayList<String> list = new ArrayList<String>();
   list.add("A"+i);
   list.add("B"+i);
   map.put("index"+i, list);
  }

  System.out.println(map);
  for(Map.Entry<String, ArrayList<String>> entry : map.entrySet()) {
   String key = entry.getKey();
   ArrayList<String> value = entry.getValue();
   //System.out.println(value.get(1).matches("(?i)b2|B3|B4"));
   if(entry.getValue().get(1).matches("B5")) {
    System.out.println(entry.getKey() +" " +entry.getValue().get(0) +" " +entry.getValue().get(1));
   }
  }
  System.out.println(map.size());

 }

}