Core java
07 MayJava String indexOf example
String’s indexOf method is used to find out index of any character or sub string. indexOf method can take char or string as argument and also you can provide fromIndex(Index after which you want to use indexOf method) also. Methods: public int indexOf(int ch) returns index of first occurrence of character within String public int […]
06 MayJava String substring example
String’s substring method is used to fetch part of a String.It returns a new String which is substring of given String. substring is nothing but subset of given String. There are two overloaded methods for substring. Method Signature: public String substring(int startIndex) It returns a new String which is substring of given String from specified […]
06 MayJava String contains example
String’s contains method checks if sequence of characters can be found in String. It returns true if character sequence is present in String else return false. Method Signature: public boolean contains(CharSequence s) String contains Example: package org.arpit.java2blog; public class StringContainsMethodExample { public static void main(String[] args) { String str = "Hello world from java2blog.com"; if […]
06 MayJava String concatenate example
String concatenate is a way to join multiple Strings to create large String. There are many places where we use to do String concatenate. For example: When you override toString() method of object, you concatenate multiple attributes to create a big String that represent that object. 3 ways to concatenate String Using + operator Using String’s […]
06 MayHow to split a String in java
In this post, we will see how to split a String by delimiter in java. Sometimes, we need to split a String by delimiter for example: while reading a csv file, we need to split string by comma(,). We will use String class’s split method to split a String. This split(regex) takes a regex as […]
04 MayHow to convert Char Array to String in java
There are multiple ways to convert Char Array to String in java. Some of them are: Using String class’s constructor (By passing Char array to contructor) Using String class’s valueOf method Using iterating char array Example: package org.arpit.java2blog; public class CharArrToStringMain { /* * @ Author : Arpit Mandliya */ public static void main(String args[]) […]
04 MayHow to convert String to Char Array in java
Java String’s toCharArray() method can be used to convert String to char Array in java. It is simplest method to convert String to char Array. Example: package org.arpit.java2blog; public class StringToCharArrayMain { /* * @Author: Aprit Mandliya */ public static void main(String[] args) { String str="java2blog"; char[] charArr=str.toCharArray(); for (int i = 0; i < […]
01 MayHow to sort HashMap in java by keys and values
HashMap does not preserve order of element but what if you want to sort it by keys or values. In this post, we will see how to sort HashMap by keys or values. Java HashMap tutorial: HashMap in java HashMap internal working hash and indexfor method in HashMap hashcode and equals in java sort HashMap […]
01 MayLinkedHashSet in java
In this post, we will see about LinkedHashSet in java. LinkedHashSet is same as HashSet except that it maintains insertion order. Some points about LinkedHashSet LinkedHashSet implements Set interface and extends HashSet class. LinkedHashSet maintains insertion order, so when you will be able to access elements in the order they were inserted like ArrayList. Example: LinkedHashSetMain.java package org.arpit.java2blog; import java.util.LinkedHashSet; […]