Menu

trimToSize() in Java

trimToSize() method used to reduce the capacity of the ArrayList, StringBuffer and StringBuilder in Java. Trims the capacity of the instance(ArrayList, StringBuffer and StringBuilder) to be the list's current size. An application can use this operation to minimize the storage of those instances.
Let understand this method using a scenario, for example, we have created a StringBuffer and assign some value into it. suppose we have put "trim to size method in java" string into it. And when you will pull the length of the StringBuffer then it will return the actual size of the string, which, we have assign into it, in our case, it will return 27. But when you will fetch the capacity if the StringBuffer then it will give you output more than the length of StringBuffer, in our case it is returning 34. This means this Buffer has more capacity and could store more data into it. Since our text has the length of 27, and we don't want that extra capacity anymore in our instance then we could remove and free the extra capacity from that instance using trimToSize method. And this method will trim the capacity of an instance with the size or length of the instance.  

Here is an example code of trimToSize() method with StringBuffer

Output:
trim to size method in java, and length of StringBuffer copyText is:27
Capacity of copyText is::34
trim to size method in java, and length of StringBuffer copyText after trimToSize() is:27
Capacity of copyText after trimToSize() is::27

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) {}

 }

}