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(){ …
Day: April 27, 2021
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”); …
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 …
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 …
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 …
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; …
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 …
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 …
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 …
Java Program to Create String from Contents of a File In this program, you’ll learn different techniques to create a string from concents of a given file in Java. Before we create a string from a 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. …