We can use json-simple library parse a JSON string to Java object and vice versa. The jar can be downloaded from : https://code.google.com/archive/p/json-simple/ Here are some examples: Example : Convert Java Object to JSON String package com.topjavatutorial.json; import org.json.simple.JSONObject; public class JsonParsingExample { public static void main(String[] args) { JSONObject jsonObj = new JSONObject(); […]
Category: Java Programs
Java forEach examples
This article lists some examples of forEach loop with Collections. forEach example with Array String[] countries = { "india", "usa", "china", "russia" }; System.out.println("array contents : "); for (String str : countries) { System.out.println(str); } forEach example with List List<String> countries = new ArrayList<String>(); countries.add("india"); countries.add("usa"); countries.add("china"); countries.add("russia"); for (String str : countries) […]
How to sort a map in Java 8
Prior to Java 8, a Map could be sorted as explained in this article : How to sort a Map in Java The below examples demonstrate how we can sort a map in Java 8 using lambda expression and streams. These examples use a Map where country name is key and the name of its […]
Sort an ArrayList using Collections.sort() method
ArrayList stores elements in the order they are added. We can sort an ArrayList using Collections.sort() method. Example : Sort ArrayList using Collections.sort() package com.techkatak.app; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ArrayListDemo { public static void main(String[] args) { List<Integer> numList = new ArrayList<Integer>(); numList.add(15); numList.add(10); numList.add(55); numList.add(20); numList.add(25); System.out.println("Original […]
Read Write PDF file using Java
In this article, we will see some examples for creating and reading a PDF file using Apache PDFBox. We can create a Java project using an IDE like Eclipse or use a build tool like Maven to create a java project. Here is the maven archetype we will be using : mvn archetype:generate –DgroupId=com.mycompany.app […]