Java Articles

Page 164 of 450

Retrieving Elements from Collection in Java- EnumerationIterator

AmitDiwan
AmitDiwan
Updated on 14-Sep-2020 267 Views

EnumerationIterator doesn’t have the option of eliminating elements from the collection, whereas an iterator has this facility. An additional disadvantage of using EnumerationIterator is that the name of methods associated with EnumerationIterator is difficult to remember.ExampleFollowing is an example − Live Demoimport java.util.Vector; import java.util.Enumeration; public class Demo {    public static void main(String args[]) {       Vector day_name = new Vector();       day_name.add("Tuesday");       day_name.add("Thursday");       day_name.add("Saturday");       day_name.add("Sunday");       Enumeration my_days = day_name.elements();       System.out.println("The values are ");       while (my_days.hasMoreElements())     ...

Read More

Retrieving Elements from Collection in Java- Iterator

AmitDiwan
AmitDiwan
Updated on 14-Sep-2020 422 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
AmitDiwan
Updated on 14-Sep-2020 360 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

Semaphore in Java

AmitDiwan
AmitDiwan
Updated on 14-Sep-2020 790 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
AmitDiwan
Updated on 14-Sep-2020 353 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
AmitDiwan
Updated on 14-Sep-2020 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

MultiMap in Java

AmitDiwan
AmitDiwan
Updated on 14-Sep-2020 351 Views

Multimap is a general method to bind the keys with random multiple values. The Multimap framework in Guava has methods that help in dealing with mapping keys to multiple values. Multimap can be visualized as a framework that −Is a collection of mapping from one key to one specific valueIs a collection of mapping from unique key to multiple values, i.e. collection of values.It can be implemented in places that use Map.Advantages of MultimapAn empty collection need not be populated before added a key value pair with the help of the function ‘put’.The ‘get’ method doesn’t return a null, except ...

Read More

Differences between String and StringBuffer

Himanshu shriv
Himanshu shriv
Updated on 09-Sep-2020 14K+ Views

String is an immutable class and its object can’t be modified after it is created but definitely reference other objects. They are very useful in multithreading environment because multiple threads can’t change the state of the object so immutable objects are thread safe.String buffer is mutable classes which can be used to do operation on string object such as reverse of string, concating string and etc. We can modify string without creating new object of the string. String buffer is also thread safe.Also, string concat + operator internally uses StringBuffer or StringBuilder class. Below are the differences.Sr. No.KeyStringStringBuffer1BasicString is an ...

Read More

Difference between CountDownLatch and CyclicBarrier in Java Concurrency

Himanshu shriv
Himanshu shriv
Updated on 09-Sep-2020 2K+ Views

CountDownLatch and CyclicBarrier both used in multithreading environment and they both are part of.As per Java Doc −CountDownLatch − A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.CyclicBarrier − A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point.Sr. No.KeyCyclicBarrierCountDownLatch1BasicA synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point.A synchronization aid that allows one or more threads to wait until a set of operations being ...

Read More

Difference between Apache Kafka and JMS.

Himanshu shriv
Himanshu shriv
Updated on 09-Sep-2020 878 Views

Kafka and JMS both are messaging system. Java message service is an api which are provided by Java. It is used for implementing messaging system in your application. JMS supports queue and publisher /subscriber(topic) messaging system . With queues, when first consumer consumes a message, message gets deleted from the queue and others cannot take it anymore. With topics, multiple consumers receive each message but it is much harder to scale.Kafka is a generalization of these two concepts - it allows scaling between members of the same consumer group, but it also allows broadcasting the same message between many different ...

Read More
Showing 1631–1640 of 4,496 articles
« Prev 1 162 163 164 165 166 450 Next »
Advertisements