Programming Articles

Page 2536 of 2547

Checking for Null or Empty or White Space Only String in Java.

Anjana
Anjana
Updated on 30-Jul-2019 2K+ Views

We can verify whether the given string is empty using the isEmpty() method of the String class. This method returns true only if length() is 0. Example import java.lang.*; public class StringDemo {     public static void main(String[] args) {         String str = "tutorialspoint";         // prints length of string         System.out.println("length of string = " + str.length());         // checks if the string is empty or not         System.out.println("is this string empty? = " + str.isEmpty());     } } Output length of string = 14 is this string empty? = false

Read More

What does the method add(E element) do in java?

Nikitha N
Nikitha N
Updated on 30-Jul-2019 527 Views

The add(E e) method of the java.util.ArrayList class appends the specified element E to the end of the list.Example:import java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add(15);       arrlist.add(20);       arrlist.add(25);       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }    } }Output:Number = 15 Number = 20 Number = 25

Read More

When to use LinkedList over ArrayList in Java

Paul Richard
Paul Richard
Updated on 30-Jul-2019 3K+ Views

LinkedList should be used where modifications to a collection are frequent like addition/deletion operations. LinkedList is much faster as compare to ArrayList in such cases. In case of read-only collections or collections which are rarely modified, ArrayList is suitable.

Read More

Which one is faster Array or List in Java

Moumita
Moumita
Updated on 30-Jul-2019 948 Views

The array is faster in case of access to an element while List is faster in case of adding/deleting an element from the collection.

Read More

remove null value from a String array in Java

Jai Janardhan
Jai Janardhan
Updated on 30-Jul-2019 4K+ Views

Following program creates an array with null values. Convert it a list with not-null values only and then get the array of that list.Exampleimport java.util.ArrayList; import java.util.List; public class Tester { public static void main(String[] args) { String[] array = {"I", null, "love", null, "Java" }; List values = new ArrayList(); for(String data: array) { if(data != null) { values.add(data); } } String[] target = values.toArray(new String[values.size()]); for(String data: target) { System.out.println(data + " "); } } }OutputI love Java

Read More

How to synchronize an ArrayList in Java?

Nikitha N
Nikitha N
Updated on 30-Jul-2019 314 Views

The synchronizedList(List list) method of the Collections class accepts a List object and returns a synchronized list backed by the specified list.Example:import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; public class ArrayListSample {    public static void main(String[] args){       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       Set set = new LinkedHashSet(list);       Collections.synchronizedList(list);       System.out.println(list);    } }Output:[JavaFx, Java, WebGL, OpenCV]

Read More

How to sort an ArrayList in Java in ascending order?

Priya Pallavi
Priya Pallavi
Updated on 30-Jul-2019 733 Views

You can sort an ArrayList using the sort() method of the Collections class this method accepts a list object as a parameter and sorts the contents of it in ascending order.Example:import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; public class ArrayListSample {    public static void main(String[] args) {       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       Set set = new LinkedHashSet(list);       Collections.sort(list);       System.out.println(list);    } }Output:[Java, JavaFx, OpenCV, WebGL]

Read More

How to sort an ArrayList in Java in descending order?

V Jyothi
V Jyothi
Updated on 30-Jul-2019 7K+ Views

To sort the contents of an ArrayList in descending orderCreate an ArrayList.Sort the contents of the ArrayList using the sort() method of the Collections class.Then, reverse array list using the reverse() method of the Collections class.Example:import java.util.ArrayList; import java.util.Collections; public class ArrayListSample {    public static void main(String[] args) {       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       Collections.sort(list);       System.out.println(list);       Collections.reverse(list);       System.out.println(list);    } }Output:[Java, JavaFx, OpenCV, WebGL] [WebGL, OpenCV, JavaFx, Java]

Read More

Constructors of StringBuilder class in Java.

Manikanth Mani
Manikanth Mani
Updated on 30-Jul-2019 435 Views

The StringBuilder class of the java.lang package is a mutable sequence of characters. This provides an API compatible with StringBuffer, but with no guarantee of synchronization. Following are the list of constructors provided by the StringBuilder class.S.N.Constructor & Description1StringBuilder()This constructs a string builder with no characters in it and an initial capacity of 16 characters.2StringBuilder(CharSequence seq)This constructs a string builder that contains the same characters as the specified CharSequence.3StringBuilder(int capacity)This constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.4StringBuilder(String str)This constructs a string builder initialized to the contents of the specified string. ...

Read More

Difference between StringBuffer and StringBuilder.

Akshaya Akki
Akshaya Akki
Updated on 30-Jul-2019 1K+ Views

The StringBuffer and StringBuilder classes are used when there is a necessity to make a lot of modifications to Strings of characters.Unlike Strings, objects of type StringBuffer and String builder can be modified over and over again without leaving behind a lot of new unused objects.The StringBuilder class was introduced as of Java 5 and the main difference between the StringBuffer and StringBuilder is that StringBuilder’s methods are not thread safe (not synchronized).It is recommended to use StringBuilder whenever possible because it is faster than StringBuffer. However, if the thread safety is necessary, the best option is StringBuffer objects.

Read More
Showing 25351–25360 of 25,467 articles
Advertisements