Maruthi Krishna has Published 951 Articles

Remove Leading Zeroes from a String in Java using regular expressions

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 12:51:11

6K+ Views

The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String.Following is the regular expression to match the leading zeros of a string −The ^0+(?!$)";To remove the leading zeros from a string pass this as ... Read More

Can an interface in Java extend multiple interfaces?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 12:48:00

757 Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static. Just like classes you can extend one interface from another using the extends keyword as shown below:interface ArithmeticCalculations {    public abstract int addition(int a, int b);    public ... Read More

What is ArrayStoreException in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 10:44:49

451 Views

When you have created an array of a particular data type with fixed size and populate it if you store a value other than its datatype an ArrayStoreException is thrown at the run time.ExampleIn the following Java program, we are creating an Integer array and trying to store a double ... Read More

How to avoid ConcurrentModificationException while iterating a collection in java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 10:35:53

297 Views

When you are working with collection objects, while one thread is iterating over a particular collection object, if you try to add or remove elements from it, a ConcurrentModificationException will be thrown.Not only that, If you are iterating a collection object, add or remove elements to it and try to ... Read More

What happens when we try to add a duplicate key into a HashMap object in java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 10:32:33

3K+ Views

The HashMap is a class that implements the Map interface. It is based on the Hash table. It allows null values and null keys.You can store key-value pairs in the HashMap object. Once you do so you can retrieve the values of the respective keys but, the values we use ... Read More

How to insert an object in an ArrayList at a specific position in java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 10:29:34

795 Views

The add() method of the ArrayList class helps you to add elements to an array list. It has two variants −add(E e) − This method accepts an object/elements as a parameter and adds the given element at the end of the list.public void add(int index, E element) − This method ... Read More

How to search for a string in an ArrayList in java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 10:26:22

20K+ Views

The contains a () method of the String class accepts Sting value as a parameter, verifies whether the current String object contains the specified string and returns true if it does (else false).Therefore, to for a string in an ArrayList −Get the array list.Using the for-each loop get each element ... Read More

Difference between ArrayList.clear() and ArrayList.removeAll() in java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 10:21:26

770 Views

The ArrayList class in Java is a Resizable-array implementation of the List interface. It allows null values.The clear() method this class removes all the elements from the current List object.Example Live Demoimport java.util.ArrayList; public class ClearExample {    public static void main(String[] args){       //Instantiating an ArrayList object   ... Read More

Difference between next() and hasNext() in java collections?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 10:18:38

985 Views

Java provides Iterator and ListIterator classes to retrieve the elements of the collection objects.The hasNext() methodThe hasNext() method of these interfaces returns true if the collection object has the next element else it returns false.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class hasNextExample{    public static void main(String[] args){     ... Read More

How to remove the redundant elements from an ArrayList object in java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 10:15:45

376 Views

The interface set does not allow duplicate elements. The add() method of this interface accepts elements and adds to the Set object, if the addition is successful it returns true if you try to add an existing element using this method, the addition operations fails to return false.Therefore, to remove ... Read More

Advertisements