Java useful Code Snippets


1.Printing array in java using toString() method:

      We can use the Arrays.toString() static helper method as follows:
   
      String words[] = getInputArray();
 
      System.out.println(java.util.Arrays.toString(words));

2. How to Split a Sting[] array in java based on a delimter?

split() method syntax:

public String[] split(String regex)

String Str = new String("Welcome-to-Liferay-way-blogspot-com");

for (String retval: Str.split("-")){
         System.out.println(retval);
      }

Output:
Welcome
to
Liferay
way
blogspot
com

3. Find duplicate string from an Array of String:

We can add the String array to the HashSet, HashSet does not allow duplicates

Set h = new HashSet(Arrays.asList(new String[] { "a", "b" }));

this will get you unique String values. If necessary convert the HashSet back to array

String[] uniqueValues = h.toArray(new String[0]);

Search This Blog

All the rights are reserved to this blog is belongs to me only.. Powered by Blogger.