Found 7442 Articles for Java

Retrieving Elements from Collection in Java- ListIterator

AmitDiwan
Updated on 14-Sep-2020 09:39:05

234 Views

Following is an example to retrieve elements from Collection in Java-ListIterator −Example Live Demoimport java. util.* ; public class Demo {    public static void main(String args[]) {       Vector my_vect = new Vector();       my_vect.add(56);       my_vect.add(78);       my_vect.add(98);       my_vect.add(34);       ListIterator my_iter = my_vect.listIterator();       System.out.println("In forward direction:");       while (my_iter.hasNext())       System.out.print(my_iter.next()+" ") ;       System.out.print("In backward direction:") ;       while (my_iter.hasPrevious())          System.out.print(my_iter.previous()+" ");    } }OutputIn forward direction: 56 78 ... Read More

Retrieving Elements from Collection in Java- Iterator

AmitDiwan
Updated on 14-Sep-2020 09:36:16

359 Views

Following is an example to retrieve elements −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       HashSet my_hs = new HashSet() ;       my_hs.add("Joe");       my_hs.add ("Rob");       Iterator my_it = my_hs.iterator();       System.out.println("The elements are : ");       while (my_it.hasNext())          System.out.println(my_it.next());    } }OutputThe elements are : Joe RobA class named Demo contains the main function, which defines a HashSet collection. Elements are added to this collection using the ‘add’ function. An iterator is defined, and the ... Read More

Retrieving Elements from Collection in Java- For-each loop

AmitDiwan
Updated on 14-Sep-2020 09:34:04

319 Views

The ‘for-each’ loop is used to iterate over a set of elements that is stored in a data structure.Syntaxfor (element e: collection) {    System.out.println(e); }ExampleFollowing is an example − Live Demopublic class Demo {    public static void main(String[] args) {       int[] my_vals = {5, 67, 89, 31, -1, 2, 0};       int sum = 0;       for (int number: my_vals) {          sum += number;       }       System.out.println("The sum is " + sum);    } }OutputThe sum is 193A class named Demo contains the ... Read More

Reflection Array Class in Java

AmitDiwan
Updated on 14-Sep-2020 09:26:40

575 Views

The array class present in java.lang.reflect package belong to the Java reflection class. The Java Reflection class provides static methods, and these methods can be used to create and access Java arrays in a dynamic manner. This class is final, and this means it can’t be changed or even instantiated. The methods present inside this class can be used with the help of the class name itself.The methods present in java.util.Arrays.class can be used to work with arrays, and the java.lang.reflect.Array class contains methods that help in creating and working with Java arrays in a dynamic manner.The java.lang.reflect.Array class is ... Read More

Semaphore in Java

AmitDiwan
Updated on 14-Sep-2020 09:21:29

686 Views

A semaphore is used to control access to a shared resource when a process is being executed. This is done with the help of a counter. When this counter value is greater than 0, access to share resource is provided. On the other hand, if the value of counter is zero, then access to shared resources is denied. The counter basically keeps a count of the number of permissions it has given to the shared resource. This means, a semaphore provides access to a shared resource for a thread.Semaphores are present in the java.util.concurrent package. The concept of semaphore is ... Read More

Quantifiers in Java

AmitDiwan
Updated on 14-Sep-2020 09:19:31

297 Views

Quantifier is a concept that allows the programmer to specify the number of occurrences of a specific type of value in the regular expression. There are different types of quantifiers, some of them include ‘?’ (Reluctant quantifier), ‘+’ (Possessive quantifier). In this post, we will see how reluctant quantifier works.ExampleFollowing is an example − Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String[] args) {       Pattern my_pattern = Pattern.compile("sam+?");       Matcher my_match = my_pattern.matcher("samp");       while (my_match.find())       System.out.println("The pattern has been found - " + my_match.start() ... Read More

Parent and Child classes having same data member in Java

AmitDiwan
Updated on 14-Sep-2020 09:16:11

12K+ Views

The parent class can hold reference to both the parent and child objects. If a parent class variable holds reference of the child class, and the value is present in both the classes, in general, the reference belongs to the parent class variable. This is due to the run-time polymorphism characteristic in Java.ExampleFollowing is an example − Live Democlass Demo_base {    int value = 1000;    Demo_base() {       System.out.println("This is the base class constructor");    } } class Demo_inherits extends Demo_base {    int value = 10;    Demo_inherits() {       System.out.println("This is the inherited ... Read More

Parallel Data Processing in Java

Alshifa Hasnain
Updated on 19-Mar-2025 18:53:20

1K+ Views

In this article, we will learn about Parallel Data Processing in Java. Parallel processing of data is important to increase performance, particularly for large amounts of data. Java has its own built-in ways to accomplish things in the background, fully using multi-core processors. Different Approaches The following are the two different approaches for parallel data processing in Java − Using Java Streams API Using Arrays.parallelSort() Why Parallel Processing? Parallel data processing is essential in scenarios where − Processing large datasets is necessary to happen ... Read More

NavigableMap Interface in Java with Example

AmitDiwan
Updated on 14-Sep-2020 09:11:24

744 Views

NavigableMap is an extension of the SortedMap collection framework. It is used to arrange the elements in a uniform fashion. NavigableMap has different methods to iterate over the elements in the Map.ExampleFollowing is an example − Live Demoimport java.util.NavigableMap; import java.util.TreeMap; public class Demo {    public static void main(String[] args) {       NavigableMap my_map = new TreeMap();       my_map.put("A", 856);       my_map.put("M", 349);       my_map.put("Z", 567);       System.out.printf("The descending set is : %s%n", my_map.descendingKeySet());       System.out.printf("The floor entry is : %s%n", my_map.floorEntry("A"));       System.out.printf("The first key ... Read More

Non-generic Vs Generic Collection in Java

Alshifa Hasnain
Updated on 19-Mar-2025 18:53:35

2K+ Views

In this article, we will learn about the collections in Java. The collections are used to store and manipulate groups of objects. Java collections can be broadly classified into two types − Non-generic Collections (Before Java 5) Generic Collections (Introduced in Java 5) Non-generic collections allow storing objects of different types leading to runtime errors. On the other hand generic collections enforce type safety at compile-time, reducing the risk of type-related errors.  Non - Generic Collection When the data structure is non-generic, it causes issues when the data is tried ... Read More

Advertisements