In this post we will see examples to convert ArrayList to HashSet in java. HashSet and ArrayList are both Collection objects. Hence we can pass ArrayList to the construtor of the HashSet and get it
How to convert ArrayList to Array
In this post we will see examples on how to convert ArrayList to Array in java. To convert ArrayList to Array we will use the toArray method from ArrayList.
1 |
public <T> T[] toArray(T[] a) |
How to create Sublist from ArrayList
To create Sublist from ArrayList in java, we need to use the subList(int fromIndex,int toIndex) method provided by ArrayList. We will see the example on how to use the subList method. [crayon-66de5
ArrayList ensureCapacity example
If you have an ArrayList that already has lots of elements and you are trying to add more elements to the ArrayList, this may degrade the performance of the system. To improve the performance, you
How to remove elements from ArrayList
In this post we will see various ways to remove elements from ArrayList in java. ArrayList provides following methods to remove elements. 1. E remove(int index) Removes an element fr
How to add elements in ArrayList
In this post we will see various ways to add elements in ArrayList in java. ArrayList provides following methods to add elements. 1. boolean add(E e) Adds an element at the end of
How to Iterate ArrayList in java
In this post we will see how to iterate ArrayList in java. We can iterate ArrayList either using 1. For loop 2. Iterator. 3. While loop We will see examples for all these methods. 1. Iterate ArrayL
How to initialize ArrayList in java
We have many ways to initialize ArrayList in java. ArrayList can be initialized in one line, Using constructors, Using Arrays. 1. Initialize ArrayList using Constructors [crayon-66de50b4c998d60992
String trim example
String trim method is used to remove the leading and trailing white spaces in a String.
1 |
public String trim() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kscodes.sampleproject; public class StringTrimExample { public static void main(String[] args) { String testStr1 = " Make me Slim trim "; System.out.println("Original String ::" + testStr1); System.out.println("Original String length ::" + testStr1.length()); System.out.println("----------------------------------"); System.out.println("After trimming ::"+ testStr1.trim()); System.out.println("After trimming String length ::"+ testStr1.trim().length()); } } |
String concat example
Concat method of String in java is used to add a new String at the end of the original String.
1 |
public String concat(String str) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.kscodes.sampleproject; public class StringConcatExample { public static void main(String[] args) { String testStr1 = "This is test String"; String testStr2 = " Used for Concat Example."; System.out.println("After 2 String concat::" + testStr1.concat(testStr2)); System.out.println("Adding 1 more to concat::" + testStr1.concat(testStr2).concat("Added more to this String")); } } |