Month: April 2021

Learn Java by Example: Java Program to Sort ArrayList of Custom Objects By Property

Java Program to Sort ArrayList of Custom Objects By Property In this program, you’ll learn to sort an arraylist of custom object by their given property in Java.   Example: Sort ArrayList of Custom Objects By Property import java.util.*; public class CustomObject{ private String customProperty; public CustomObject(String property){ this.customProperty = property; } public String getCustomProperty(){ …

Learn Java by Example: Java Program to Sort a Map By Values

Java Program to Sort a Map By Values In this program, you’ll learn to sort a given map by values in Java.   Example: Sort a map by values import java.util.*; public class SortMap{ public static void main(String[] args){ LinkedHashMap<String, String> capitals = new LinkedHashMap<>(); capitals.put(“Nepal”, “Kathmandu”); capitals.put(“India”, “New Delhi”); capitals.put(“United States”, “Washington”); capitals.put(“England”, “London”); …

Learn Java by Example: Java Program to Compare Strings

Java Program to Compare Strings In this program, you’ll learn to compare two strings in Java.   Example 1: Compare two strings public class CompareStrings{ public static void main(String[] args){ String style = “Bold”; String style2 = “Bold”; if(style == style2) System.out.println(“Equal”); else System.out.println(“Not Equal”); } } Output Equal In the above program, we’ve two …

Learn Java by Example: Java Program to Lookup enum by String value

Java Program to Lookup enum by String value In this program, you’ll learn to convert a string value to a enum in Java using enum’s valueOf() method. Example: Lookup enum by string value public class EnumString{ public enum TextStyle { BOLD, ITALICS, UNDERLINE, STRIKETHROUGH } public static void main(String[] args){ String style = “Bold”; TextStyle …

Learn Java by Example: Java Program to Convert OutputStream to String

Java Program to Convert OutputStream to String In this program, you’ll learn to convert outputstream to a string using String initializer in Java.   Example: Convert OutputStream to String import java.io.*; public class OutputStreamString{ public static void main(String[] args) throws IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream(); String line = “Hello there!”; stream.write(line.getBytes()); String finalString …

Learn Java by Example: Java Program to Convert InputStream to String

Java Program to Convert InputStream to String In this program, you’ll learn to convert input stream to a string using InputStreamReader in Java.   Example: Convert InputStream to String import java.io.*; public class InputStreamString{ public static void main(String[] args) throws IOException { InputStream stream = new ByteArrayInputStream(“Hello there!”.getBytes()); StringBuilder sb = new StringBuilder(); String line; …

Learn Java by Example: Java Program to Convert File to byte array and Vice-Versa

Java Program to Convert File to byte array and Vice-Versa In this program, you’ll learn to convert a File object to byte[] and vice-versa in Java.   Before we convert a file to byte array and vice-versa, we assume we have a file named test.txt in our src folder. Here’s the content of test.txt This is a Test file. Example …

Learn Java by Example: Java Program to Convert a Stack Trace to a String

Java Program to Convert a Stack Trace to a String In this program, you’ll learn to convert a stack trace to a string in Java.   Example: Convert stack trace to a string import java.io.PrintWriter; import java.io.StringWriter; public class PrintStackTrace{ public static void main(String[] args){ try { int division = 0 / 0; } catch …

Learn Java by Example: Java Program to Append Text to an Existing File

Java Program to Append Text to an Existing File In this program, you’ll learn different techniques to append text to an existing file in Java.   Before we append text to an existing file, we assume we have a file named test.txt in our src folder. Here’s the content of test.txt This is a Test file. Example 1: Append text …