Found 272 Articles for Java8

Different ways for Integer to String conversion in Java

Fendadis John
Updated on 21-Jun-2020 12:40:05

125 Views

Following are the different ways to convert an Integer to String in Java.Using Integer.toString(int) − Convert an int to String using static toString() method of Integer class.String b = Integer.toString(125);Using String.valueOf(int) − Convert an int to String using static valueOf() method of String class.String b = String.valueOf(125);Using new Integer(int).toString() − Convert an int to String using toString() method of Integer object.String b = new Integer(125).toString();Using DecimalFormat(pattern).format(int) − Convert an int to String using DecimalFormat.format() method.String b = new DecimalFormat("#").format(125);Using StringBuilder().toString() − Convert an int to String using StringBuilder.toString() method.String b = new StringBuilder().append(125).toString();Using StringBuffer().toString() − Convert an int to String ... Read More

Difference between TreeMap, HashMap, and LinkedHashMap in Java

Paul Richard
Updated on 21-Jun-2020 12:35:10

10K+ Views

HashMap, TreeMap and LinkedHashMap all implements java.util.Map interface and following are their characteristics.HashMapHashMap has complexity of O(1) for insertion and lookup.HashMap allows one null key and multiple null values.HashMap does not maintain any order.TreeMapTreeMap has complexity of O(logN) for insertion and lookup.TreeMap does not allow null key but allow multiple null values.TreeMap maintains order. It stores keys in sorted and ascending order.LinkedHashMapLinkedHashMap has complexity of O(1) for insertion and lookup.LinkedHashMap allows one null key and multiple null values.LinkedHashMap maintains order in which key-value pairs are inserted.Exampleimport java.util.HashMap; import java.util.Hashtable; import java.util.LinkedHashMap; import java.util.Map; import java.util.TreeMap; public class Tester { ... Read More

Difference between HashTable and HashMap in Java

Rishi Raj
Updated on 21-Jun-2020 12:35:45

3K+ Views

Following are the notable differences between HashTable and HashMap classes in Java. HashTableHashMapSynchronizedHashTable is synchronized.HashMap is not synchronized.Thread SafeHashTable is thread safe.HashMap is not thread safe.Null objectsHashTable does not allows null keys or null values.HashMap allows one null key and multiple null values.PerformanceHashTable is faster.HashMap is slower than HashTable.Since Java Version1.21.5Exampleimport java.util.HashMap; import java.util.Hashtable; import java.util.Map; public class Tester {    public static void main(String args[]) {       Map map = new HashMap();       map.put("1", "One");       map.put("2", "Two");       map.put("3", "Three");       map.put("5", "Five");       ... Read More

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

298 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

413 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

472 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

Advertisements