Found 9150 Articles for Object Oriented Programming

IntBuffer compareTo() method in Java

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

161 Views

A buffer can be compared with another buffer using the method compareTo() in the class java.nio.IntBuffer. This method returns a negative integer if the buffer is less than the given buffer, zero if the buffer is equal to the given buffer and a positive integer if the buffer is greater than the given buffer.A program that demonstrates this is given as follows −Example Live Demoimport java.nio.*; import java.util.*; public class Demo {    public static void main(String[] args) {       int n = 5;       try {          IntBuffer buffer1 = IntBuffer.allocate(n);     ... Read More

IntBuffer as ReadOnlyBuffer() method in Java

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

72 Views

A read-only int buffer can be created using the contents of a buffer with the method asReadOnlyBuffer() in the class java.nio.IntBuffer. The new buffer cannot have any modifications as it is a read-only buffer. However, the capacity, positions, limits etc. of the new buffer are the same as the previous buffer.A program that demonstrates this is given as follows −Example Live Demoimport java.nio.*; import java.util.*; public class Demo {    public static void main(String[] args) {       int n = 5;       try {          IntBuffer buffer = IntBuffer.allocate(5);          buffer.put(8); ... Read More

Hashmap vs WeakHashMap in Java

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

2K+ Views

Details about HashMap and WeakHashMap that help to differentiate them are given as follows −HashMap in JavaA HashMap has key-value pairs i.e. keys that are associated with the values and the keys are in arbitrary order. A HashMap object that is specified as a key is not eligible for garbage collection. This means that the HashMap has dominance over the garbage collector.A program that demonstrates this is given as follows −Example Live Demoimport java.util.*; class A {    public String toString() {       return "A ";    }    public void finalize() {       System.out.println("Finalize method");   ... Read More

Differences between TreeMap, HashMap and LinkedHashMap in Java

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

455 Views

Details about TreeMap, HashMap and LinkedHashMap that help to differentiate them are given as follows −TreeMap in JavaA TreeMap in Java is implemented using a Red-Black trees. It has key-value pairs i.e. keys that are associated with the values and the keys are ordered. A TreeMap can only have unique elements and cannot have a null key but have null elements.A program that demonstrates this is given as follows −Example Live Demoimport java.util.*; import java.lang.*; import java.io.*; public class Demo {    public static void main (String[] args) {       TreeMap tMap = new TreeMap();       int[] ... Read More

CopyOnWriteArraySet in Java

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

93 Views

A thread safe version of the set is the CopyOnWriteArraySet in Java. This set uses an CopyOnWriteArrayList internally for the set operations. The CopyOnWriteArraySet was introduced by the JDK 1.5.A program that demonstrates this is given as follows −Example Live Demoimport java.util.concurrent.*; public class Demo extends Thread {    public static void main(String[] args) {       CopyOnWriteArraySet cowArraySet = new CopyOnWriteArraySet();       cowArraySet.add("Amy");       cowArraySet.add("John");       cowArraySet.add("Bob");       cowArraySet.add("Clara");       cowArraySet.add("Peter");       System.out.println(cowArraySet);    } }The output of the above program is as follows −Output[Amy, John, Bob, ... Read More

ConcurrentSkipListSet in Java

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

275 Views

The ConcurrentSkipListSet has elements that are sorted by default. Also, its implementation is based on the ConcurrentSkipListMap. The ConcurrentSkipListSet class also implements the Collection interface as well as the AbstractSet class. It is a part of the Java Collection Framework.A program that demonstrates this is given as follows −Example Live Demoimport java.util.concurrent.ConcurrentSkipListSet; public class Demo {    public static void main(String[] args) {       ConcurrentSkipListSet csls = new ConcurrentSkipListSet();       csls.add(7);       csls.add(4);       csls.add(1);       csls.add(9);       csls.add(3);       System.out.println("The elements in ConcurrentSkipListSet are: " + ... Read More

LinkedBlockingDeque in Java

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

350 Views

The LinkedBlockingDeque Class in Java has a blockingdeque that is optionally bounded and based on linked nodes. This class implements the Collection interface as well as the AbstractQueue class. It is a part of the Java Collection Framework.A program that demonstrates this is given as follows −Example Live Demoimport java.util.concurrent.LinkedBlockingDeque; public class Demo {    public static void main(String[] args) {       LinkedBlockingDeque lbDeque = new LinkedBlockingDeque();       lbDeque.add("James");       lbDeque.add("May");       lbDeque.add("John");       lbDeque.add("Sara");       lbDeque.add("Anne");       System.out.println("Size of LinkedBlockingDeque is: " + lbDeque.size());     ... Read More

ConcurrentLinkedDeque in Java

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

82 Views

The ConcurrentLinkedDeque Class in Java implements a deque and used a concurrent linked list for help. This class implements the Collection interface as well as the AbstractCollection class. It is a part of the Java Collection Framework.A program that demonstrates this is given as follows −Example Live Demoimport java.util.concurrent.*; public class Demo {    public static void main(String[] args) {       ConcurrentLinkedDeque clDeque = new ConcurrentLinkedDeque();       clDeque.add("James");       clDeque.add("May");       clDeque.add("John");       clDeque.add("Sara");       clDeque.add("Anne");       System.out.println("The elements in ConcurrentLinkedDeque are: " + clDeque);    } ... Read More

PriorityBlockingQueue Class in Java

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

149 Views

The PriorityBlockingQueue Class in Java has a blocking queue that has unbounded functionality and is based on the class PriorityQueue with the same ordering rules. The PriorityBlockingQueue Class is a part of the Java Collection Framework.A program that demonstrates this is given as follows −Example Live Demoimport java.util.concurrent.PriorityBlockingQueue; public class Demo {    public static void main(String[] args) {       PriorityBlockingQueue pbQueue = new PriorityBlockingQueue();       pbQueue.add("James");       pbQueue.add("May");       pbQueue.add("John");       pbQueue.add("Sara");       pbQueue.add("Anne");       System.out.println("The elements in PriorityBlockingQueue are: " + pbQueue);    } }The ... Read More

LinkedTransferQueue in Java

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

70 Views

The LinkedTransferQueue Class in Java has a transfer queue that has unbounded functionality and is based on linked nodes. It uses FIFO for ordering elements. This class implements the Collection interface as well as the AbstractQueue class. It is a part of the Java Collection Framework.A program that demonstrates this is given as follows −Example Live Demoimport java.util.concurrent.LinkedTransferQueue; public class Demo {    public static void main(String[] args) throws InterruptedException {       LinkedTransferQueue ltQueue = new LinkedTransferQueue();       ltQueue.add("Amy");       ltQueue.add("John");       ltQueue.add("May");       ltQueue.add("Harry");       ltQueue.add("Anne");     ... Read More

Advertisements