How to remove only trailing spaces of a string in Java

by Chaitanya Singh July 23, 2014

In this tutorial we will learn how to trim trailing spaces from the string but not leading spaces. Here is the complete code: class TrimBlanksExample { public static void main(String[] args) { System.out.println("#"+trimTrailingBlanks(" How are you?? ")+"@"); System.out.println("#"+trimTrailingBlanks(" I'm Fine. ")+"@"); } public static String trimTrailingBlanks( String str) { if( str == null) return null; int len = str.length(); for( […]

Read the full article →

java – Left padding a String with Spaces and Zeros

by Chaitanya Singh July 23, 2014

In this tutorial we are gonna see how to left pad a string with spaces and zeros: 1) Left pad with spaces class LeftPaddingExample1 { public static void main(String[] args) { System.out.println("#" + padLeftSpaces("mystring", 10) + "@"); System.out.println("#" + padLeftSpaces("mystring", 15) + "@"); System.out.println("#" + padLeftSpaces("mystring", 20) + "@"); } public static String padLeftSpaces(String str, int n) { return String.format("%1$" […]

Read the full article →

java – Right padding a String with Spaces and Zeros

by Chaitanya Singh July 23, 2014

In this tutorial we are gonna see how to right pad a string with spaces and zeros: 1) Right pad with spaces public class PadRightExample1 { public static void main(String[] argv) { System.out.println("#" + rightPadding("mystring", 10) + "@"); System.out.println("#" + rightPadding("mystring", 15) + "@"); System.out.println("#" + rightPadding("mystring", 20) + "@"); } public static String rightPadding(String str, int num) { return […]

Read the full article →

How to get the size of TreeMap example – Java

by Chaitanya Singh July 22, 2014

In this tutorial we willl learn how to get the size of a TreeMap. We are using size() method of TreeMap class to get the number of key-value mappings of a TreeMap. Here is the complete code: Example import java.util.TreeMap; class TreeMapSize { public static void main(String args[]) { // Create a TreeMap TreeMap<String, String> treemap = new TreeMap<String, String>(); […]

Read the full article →

How to get the Sub Map from TreeMap example – Java

by Chaitanya Singh July 22, 2014

In this example we are gonna see how to get a sub map from TreeMap. We are using subMap() method of TreeMap class. Please refer the comments in the below program for more details. Example import java.util.*; class TreeMapDemo { public static void main(String args[]) { // Create a TreeMap TreeMap<String, String> treemap = new TreeMap<String, String>(); // Put elements […]

Read the full article →

How to iterate TreeMap in reverse order in Java

by Chaitanya Singh July 22, 2014

By default TreeMap elements are sorted in ascending order of keys. We can iterate the TreeMap in reverse order to display the elements in descending order of keys. Display TreeMap elements in reverse order: import java.util.*; class TreeMapDemo { public static void main(String args[]) { Map<String, String> treemap = new TreeMap<String, String>(Collections.reverseOrder()); // Put elements to the map treemap.put("Key1", "Jack"); […]

Read the full article →

How to sort a TreeMap by value in java

by Chaitanya Singh July 22, 2014

A TreeMap is always sorted based on its keys, however if you want to sort it based on its values then you can build a logic to do this using comparator. Below is a complete code of sorting a TreeMap by values. import java.util.*; class TreeMapDemo { //Method for sorting the TreeMap based on values public static <K, V extends […]

Read the full article →

Java program for binary to decimal conversion

by Chaitanya Singh July 20, 2014

There are two following ways to convert binary number to decimal number: 1) Using Integer.parseInt() method of Integer class. 2) Do conversion by writing your own logic without using any predefined methods. Method 1: Binary to Decimal conversion using Integer.parseInt() method import java.util.Scanner; class BinaryToDecimal { public static void main(String args[]){ Scanner input = new Scanner( System.in ); System.out.print("Enter a […]

Read the full article →

Java program to convert decimal to hexadecimal

by Chaitanya Singh July 20, 2014

There are two following ways to convert Decimal number to hexadecimal number: 1) Using toHexString() method of Integer class. 2) Do conversion by writing your own logic without using any predefined methods. Method 1: Decimal to hexadecimal Using toHexString() method import java.util.Scanner; class DecimalToHexExample { public static void main(String args[]) { Scanner input = new Scanner( System.in ); System.out.print("Enter a […]

Read the full article →

Java program for decimal to octal conversion

by Chaitanya Singh July 20, 2014

In this tutorial we will learn following two ways to convert a decimal number to equivalent octal number. 1) Using predefined method Integer.toOctalString(int num) 2) Writing our own logic for conversion import java.util.Scanner; class DecimalToOctalExample { public static void main(String args[]) { Scanner input = new Scanner( System.in ); System.out.print("Enter a decimal number : "); int num =input.nextInt(); /* Method […]

Read the full article →

TreeMap Iterator example – Java

by Chaitanya Singh July 20, 2014

In this example we are iterating a TreeMap using Iterator and Map.Entry. import java.util.TreeMap; import java.util.Set; import java.util.Map; import java.util.Iterator; public class TreeMapExample { public static void main(String[] args) { // Create a TreeMap TreeMap<String, String> treemap = new TreeMap<String, String>(); // Add key-value pairs to the TreeMap treemap.put("Key1","Item1"); treemap.put("Key2","Item2"); treemap.put("Key3","Item3"); treemap.put("Key4","Item4"); treemap.put("Key5","Item5"); // Get a set of the entries […]

Read the full article →

Our Friend's website link : FromDev.com