Found 33676 Articles for Programming

Matcher.pattern() method in Java Regular Expressions

Vikyath Ram
Updated on 30-Jun-2020 08:05:47

164 Views

The method java.time.Matcher.pattern() returns the pattern that is matched upon by the Matcher. This method accepts no parameters.A program that demonstrates the method Matcher.pattern() in Java regular expressions is given as follows −Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       String regex = "Apple";       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher("AppleIsAFruit");       System.out.println("Pattern: " + m.pattern());    } }The output of the above program is as follows −Pattern: AppleNow let us understand the above program.The pattern that is matched upon ... Read More

Class with a constructor to initialize instance variables in Java

Aishwarya Naglot
Updated on 08-Aug-2025 15:09:08

12K+ Views

Constructors are special methods in Java that are invoked when an object is instantiated. They are typically used to initialize instance variables of a class. In this article, we will explore how to create a class with a constructor to initialize instance variables in Java. Creating a Class with a Constructor To create a class with a constructor, you define the class and then create a constructor that has the same name as the class. The constructor accepts parameters to initialize instance variables. Example In the example below, we have a class named 'Car' with three instance variables: 'model', 'year', ... Read More

Append all elements of another Collection to a Vector in Java

Jai Janardhan
Updated on 30-Jun-2020 08:08:14

219 Views

The elements of a Collection can be appended at the end of the Vector using the method java.util.Vector.addAll(). This method takes a single parameter i.e. the Collection whose elements are added to the Vector and it returns true if the Vector is changed.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Vector; public class Demo {    public static void main(String args[]) {       Vector vec1 = new Vector();       vec1.add(7);       vec1.add(3);       vec1.add(5);       vec1.add(9);       vec1.add(2);       System.out.println("The Vector vec1 elements ... Read More

Add elements at the middle of a Vector in Java

Aishwarya Naglot
Updated on 21-Jul-2025 12:21:00

540 Views

In Java, Vector is a part of the Java Collections Framework that implements a growable array of objects, which changes its size. It is thread-safe, so it can be useful in multi-threaded environments. Add elements at the middle of a Vector in Java We can add elements in the middle of a Vector in Java in the following ways: Using add() method Using insertElementAt() method Using add() Method The add() method of the java.util.Vector class accepts an integer value representing an index and an element as parameters and adds the ... Read More

Add elements to a Vector in Java

Aishwarya Naglot
Updated on 17-Jul-2025 17:54:23

1K+ Views

The java.util.Vector class is a part of the Java Collections Framework. It implements the List interface and is used for storing the elements in a dynamic array. It is similar to an ArrayList but is synchronized (thread-safe). The size of a vector grows dynamically as we add more elements to it and shrinks when we remove elements. It can also contain duplicate elements and null values. We can add elements to a vector in the following ways: Using add() Method Using add(int index, E element) Method Using addElement() Method ... Read More

Get Size of Java LinkedHashSet

Aishwarya Naglot
Updated on 29-Jul-2025 18:49:52

393 Views

LinkedHashSet is a collection in Java that maintains the insertion order of elements. It is part of the Java Collections Framework and extends the HashSet class. It stores unique elements and allows null values, but only one null element. It is similar to HashSet; the main difference is that a LinkedHashSet maintains a linked list holding of the entries of the current object, allowing it to maintain the order of elements. Let's learn how to get the size of a LinkedHashSet in Java. The following are some example scenarios: Scenario 1 Input : set = {1, 2, 3, 4, ... Read More

Iterate through elements of Java LinkedHashSet

Aishwarya Naglot
Updated on 28-Jul-2025 14:58:13

429 Views

LinkedHashSet is a part of Java's Collection Framework. It is a type of Set that keeps the order of elements the same as they were added. It uses a combination of a hash table and a linked list to store elements.Our task is to traverse/iterate through the elements of a LinkedHashSet in Java. Let's look at some scenarios to understand the problem better: Scenario 1 Input: {1, 2, 3, 4, 5} Output: 1 2 3 4 5 Explanation: The LinkedHashSet contains the elements 1, 2, 3, 4, and 5. When we iterate through it, we get the elements in the ... Read More

Create a new ArrayList from another collection in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

403 Views

An ArrayList can be created from another collection using the java.util.Arrays.asList() method. A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Demo {    public static void main(String args[]) throws Exception {       String str[] = { "John", "Macy", "Peter", "Susan", "Lucy" };       List aList = new ArrayList(Arrays.asList(str));       System.out.println("The ArrayList elements are: " + aList);    } }OutputThe ArrayList elements are: [John, Macy, Peter, Susan, Lucy]Now let us understand the above program.The string array str[] is defined. Then an ArrayList is created using ... Read More

Fill elements in a Java int array in a specified range

Samual Sam
Updated on 30-Jul-2019 22:30:24

762 Views

Elements can be filled in a Java int array in a specified range using the java.util.Arrays.fill() method. This method assigns the required int value in the specified range to the int array in Java.The parameters required for the Arrays.fill() method are the array name, the index of the first element to be filled(inclusive), the index of the last element to be filled(exclusive) and the value that is to be stored in the array elements.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] argv) throws Exception {     ... Read More

How to use headSet() method of Java NavigableSet Class

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

121 Views

The headset() method returns elements up to a limit defined as a parameter.First, create a NavigableSet and add elements −NavigableSet set = new TreeSet(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85); set.add(100);Now, use the headset() method −set.headSet(55));The following is an example −Example Live Demoimport java.util.NavigableSet; import java.util.TreeSet; public class Demo {    public static void main(String[] args) {       NavigableSet set = new TreeSet();       set.add(10);       set.add(25);       set.add(40);       set.add(55);       set.add(70);       set.add(85);       set.add(100);       System.out.println("Returned Value = " + ... Read More

Advertisements