Found 346 Articles for Java Programming

Difference between HashMap and ConcurrentHashMap in Java

Arushi
Updated on 21-Jun-2020 12:18:12

4K+ Views

Following are the notable differences between HashMap and ConcurrentHashMap classes in Java. HashMapConcurrentHashMapSynchronizedHashMap is not synchronized.ConcurrentHashMap is synchronized.Thread SafeHashMap is not thread safe.ConcurrentHashMap is thread safe.Iterator typeHashMap iterator is fail-fast and ArrayList throws ConcurrentModificationException if concurrent modification happens during iteration.ConcurrentHashMap is fail-safe and it will never throw ConcurrentModificationException during iteration.Null valuesHashMap allows key and value to be null.ConcurrentHashMap does not allow null key/value. It will throw NullPointerException.PerformanceHashMap is faster.ConcurrentHashMap is slower than HashMap.Since Java Version1.21.5Exampleimport java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; public class Tester {    public static void main(String[] args) {       List arrayList = new ArrayList(); ... Read More

Difference between ArrayList and CopyOnWriteArrayList in Java

Vikyath Ram
Updated on 21-Jun-2020 12:23:44

2K+ Views

Following are the notable differences between ArrayList and CopyOnWriteArrayList classes in Java. ArrayListCopyOnWriteArrayListSynchronizedArrayList is not synchronized.CopyOnWriteArrayList is synchronized.Thread SafeArrayList is not thread safe.CopyOnWriteArrayList is thread safe.Iterator typeArrayList iterator is fail-fast and ArrayList throws ConcurrentModificationException if concurrent modification happens during iteration.CopyOnWriteArrayList is fail-safe and it will never throw ConcurrentModificationException during iteration. The reason behind the it that CopyOnWriteArrayList creates a new arraylist every time it is modified.Remove OpearationArrayList iterator supports removal of element during iteration.CopyOnWriteArrayList.remove() method throws exception if elements are tried to be removed during iteration.PerformanceArrayList is faster.CopyOnWriteArrayList is slower than ArrayList.Since Java Version1.21.5Exampleimport java.util.Iterator; import java.util.concurrent.CopyOnWriteArrayList; public class Tester { ... Read More

Deque interface in Java

Fendadis John
Updated on 21-Jun-2020 12:26:34

305 Views

java.util.Deque interface is a subtype of java.util.Queue interface which supports insertion and removal of elements at both ends.Interface Declarationpublic interface Deque extends QueueArrayDeque ClassThe java.util.ArrayDeque class provides resizable-array and implements the Deque interface. Following are the important points about Array Deques −Array deques have no capacity restrictions so they grow as necessary to support usage.They are not thread-safe; in the absence of external synchronization.They do not support concurrent access by multiple threads.Null elements are prohibited in the array deques.They are faster than Stack and LinkedList.This class and its iterator implement all of the optional methods of the Collection and Iterator ... Read More

CopyOnWriteArrayList Class in Java

karthikeya Boyini
Updated on 19-Jun-2020 14:41:11

4K+ Views

Class declarationpublic class CopyOnWriteArrayList    extends Object implements List, RandomAccess, Cloneable, SerializableCopyOnWriteArrayList is a thread-safe variant of ArrayList where operations which can change the ArrayList (add, update, set methods) creates a clone of the underlying array.CopyOnWriteArrayList is to be used in a Thread based environment where read operations are very frequent and update operations are rare.Iterator of CopyOnWriteArrayList will never throw ConcurrentModificationException.Any type of modification to CopyOnWriteArrayList will not reflect during iteration since the iterator was created.List modification methods like remove, set and add are not supported in the iteration. This method will throw UnsupportedOperationException.null can be added to the ... Read More

CopyOnWriteArraySet Class in Java

Samual Sam
Updated on 19-Jun-2020 14:45:11

420 Views

Class declarationpublic class CopyOnWriteArraySet    extends AbstractSet implements SerializableCopyOnWriteArraySet class uses CopyOnWriteArrayList internally for all of its operations and thus possesses the basic properties of CopyOnWriteArrayList.CopyOnWriteArraySet is a thread-safe.CopyOnWriteArraySet is to be used in Thread based environment where read operations are very frequent and update operations are rare.Iterator of CopyOnWriteArraySet will never throw ConcurrentModificationException.Any type of modification to CopyOnWriteArraySet will not reflect during iteration since the iterator was created.Set modification methods like remove, set and add are not supported in the iteration. This method will throw UnsupportedOperationException.CopyOnWriteArraySet MethodsFollowing is the list of important methods available in the CopyOnWriteArraySet class.Sr.No.Method & ... Read More

Counting number of characters in text file using java

karthikeya Boyini
Updated on 19-Jun-2020 13:10:12

5K+ Views

We can read characters in a file using BufferedReader class of Java. See the example below −ExampleConsider the following text file in the classpath.test.txtThis is Line 1 This is Line 2 This is Line 3 This is Line 4 This is Line 5 This is Line 6 This is Line 7 This is Line 8 This is Line 9 This is Line 10Tester.javaimport java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; public class Tester {    private static final String FILE_PATH = "data.txt";    public static void main(String args[]) throws IOException {       ... Read More

Counting number of paragraphs in text file using java

karthikeya Boyini
Updated on 19-Jun-2020 13:13:30

480 Views

We can read paragraphs in a file by reading it in a string and then spilting based on "\r" pattern. See the example below −ExampleConsider the following text file in the classpath.test.txtThis is Line 1 This is Line 2 This is Line 3 This is Line 4 This is Line 5 This is Line 6 This is Line 7 This is Line 8 This is Line 9 This is Line 10Tester.javaimport java.io.File; import java.io.FileInputStream; import java.io.IOException; public class Tester {    public static void main(String args[]) throws IOException {       FileUtil fileUtil = ... Read More

Coupling in Java

karthikeya Boyini
Updated on 19-Jun-2020 13:22:22

4K+ Views

Coupling refers to the usage of an object by another object. It can also be termed as collaboration. This dependency of one object on another object to get some task done can be classified into the following two types −Tight coupling - When an object creates the object to be used, then it is a tight coupling situation. As the main object creates the object itself, this object can not be changed from outside world easily marked it as tightly coupled objects.Loose coupling - When an object gets the object to be used from the outside, then it is a loose coupling ... Read More

Database operations in Java

karthikeya Boyini
Updated on 19-Jun-2020 13:27:08

1K+ Views

This article provides an example of how to create a simple JDBC application. This will show you how to open a database connection, execute a SQL query, and display the results.Creating JDBC ApplicationThere are following six steps involved in building a JDBC application −Import the packages: Requires that you include the packages containing the JDBC classes needed for database programming. Most often, using import java.sql.* will suffice.Register the JDBC driver: Requires that you initialize a driver so you can open a communication channel with the database.Open a connection: Requires using the DriverManager.getConnection() method to create a Connection object, which represents ... Read More

Date Formatting Using SimpleDateFormat

karthikeya Boyini
Updated on 19-Jun-2020 12:43:03

693 Views

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.ExampleLive Demoimport java.util.*; import java.text.*; public class DateDemo {    public static void main(String args[]) {       Date dNow = new Date( );       SimpleDateFormat ft =               new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");       System.out.println("Current Date: " + ft.format(dNow));    } }This will produce the following result −OutputCurrent Date: Sun 2004.07.18 at 04:14:09 PM PDTSimple DateFormat ... Read More

Previous 1 ... 7 8 9 10 11 ... 35 Next
Advertisements