addIfAbsent Method of CopyOnWriteArrayList in Java

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

814 Views

The addIfAbsent() method appends the element if it is not in the list. If the element is already in the list, then FALSE is returned.The syntax is as follows.public boolean addIfAbsent(E ele)Here, ele is the element to be added to this list, if it is not already in the list.To work with CopyOnWriteArrayList class, you need to import the following package.import java.util.concurrent.CopyOnWriteArrayList;The following is an example to implement CopyOnWriteArrayList class addIfAbsent() method in Java.Example Live Demoimport java.util.concurrent.CopyOnWriteArrayList; public class Demo {    public static void main(String[] args) {       CopyOnWriteArrayList arrList = new CopyOnWriteArrayList();       arrList.add(30); arrList.add(40); ... Read More

Perform Descending Order Sort in MongoDB

Anvi Jain
Updated on 30-Jul-2019 22:30:25

410 Views

To sort in ascending order, the syntax is as follows −db.yourCollectionName.find().sort({yourField:1});To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.sortingDemo.insertOne({"Value":100}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f8e2ed3c9d04998abf006") } > db.sortingDemo.insertOne({"Value":1}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f8e31d3c9d04998abf007") } > db.sortingDemo.insertOne({"Value":150}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f8e34d3c9d04998abf008") } > db.sortingDemo.insertOne({"Value":250}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f8e37d3c9d04998abf009") } > db.sortingDemo.insertOne({"Value":5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f8e3bd3c9d04998abf00a") } > db.sortingDemo.insertOne({"Value":199}); {    "acknowledged" : ... Read More

Select Dates in 30-Day Range in MySQL

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

1K+ Views

To select dates in 30-day range, you can use arithmetic operation - with interval.The syntax is as follows −select *from yourTableName where yourDateColumnName > NOW() - INTERVAL 30 DAY and yourDateColumnName < NOW() + INTERVAL 30 DAY;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table selectDatesDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ArrivalDate datetime    -> ); Query OK, 0 rows affected (0.77 sec)Now you can insert some records in the table using insert command. The query is as ... Read More

Add Unique Key Constraint to a Column in a Database using JDBC API

Nitya Raut
Updated on 30-Jul-2019 22:30:25

627 Views

You can add a unique constraint to a column using the ALTER TABLE commandSyntaxALTER TABLE table_name ADD CONSTRAINT MyUniqueConstraint UNIQUE(column1, column2...);Assume we have a table named Dispatches in the database with 7 columns namely id, CustomerName, DispatchDate, DeliveryTime, Price and, Location with description as shown below:+--------------+--------------+------+-----+---------+-------+ | Field        | Type         | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | ProductName  | varchar(255) | YES  |     | NULL    | | | CustomerName | varchar(255) | No   |     | NULL ... Read More

Access Objects from Nested Structure in MongoDB

Chandu yadav
Updated on 30-Jul-2019 22:30:25

731 Views

Access objects using dot notation. Let us first create a collection with documents> db.nestedObjectDemo.insertOne({"Student" : { "StudentDetails" : { "StudentPersonalDetails" : { "StudentName" : [ "John" ], ... "StudentCountryName" : [ "US" ], ... "StudentCoreSubject" : [ "C", "Java" ], ... "StudentProject" : [ "Online Book Store", "Pig Dice Game" ] } } } }); {    "acknowledged" : true,    "insertedId" : ObjectId("5c99dfc2863d6ffd454bb650") }Following is the query to display all documents from a collection with the help of find() method> db.nestedObjectDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c99dfc2863d6ffd454bb650"),    "Student" : {       "StudentDetails" : ... Read More

Generate All Subsets of a Given Set in Lexicographic Order in C++

Paul Richard
Updated on 30-Jul-2019 22:30:25

461 Views

This is C++ Program to Generate All Subsets of a Given Set in the Lexico Graphic Order. This algorithm prints all the possible combination of each length from the given set of array in increasing order. The time complexity of this algorithm is O(n*(2^n)).AlgorithmBegin    For each length ‘i’ GenAllSubset() function is called:    1) In GenAllSubset(), if currLen is more than the reqLen then return.    2) Otherwise, if currLen is equal to reqLen then there will be a new sequence generated, print it.    3) If proceed with a start as ‘true’ and recursively call GenAllSubset() with incremented ... Read More

Query MongoDB Using the $ne Operator

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

156 Views

To query MongoDB using the $ne operator, following is the syntax −db.yourCollectionName.find({yourFieldName:{$ne:yourValue}}).pretty();Let us create a collection with documents −> db.notEqaulToDemo.insertOne({"StudentName":"Larry", "StudentMathMarks":68}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd3a6bde8cc557214c0ded") } > db.notEqaulToDemo.insertOne({"StudentName":"Chris", "StudentMathMarks":88}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd3a79de8cc557214c0dee") } > db.notEqaulToDemo.insertOne({"StudentName":"David", "StudentMathMarks":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd3a89de8cc557214c0def") } > db.notEqaulToDemo.insertOne({"StudentName":"Carol", "StudentMathMarks":69}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd3a97de8cc557214c0df0") }Display all documents from a collection with the help of find() method −> db.notEqaulToDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cbd3a6bde8cc557214c0ded"),    "StudentName" : "Larry",    "StudentMathMarks" : ... Read More

Create a Record in MySQL Database with TTL Option

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

4K+ Views

You need to use MySQL event scheduler. It manages the execution of events as well as scheduling.First, you need to create a table. After that you can create a event that will schedule every single day.Let us create a table. The query to create a table is as follows −mysql> create table EventDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> EventDateTime datetime -> ); Query OK, 0 rows affected (0.71 sec)Now you can insert some records in the table using insert command. The query ... Read More

ByteBuffer Compact Method in Java

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

293 Views

The buffer can be compacted using the compact() method in the class java.nio.ByteBuffer. This method does not require a parameter and it returns the new compacted ByteBuffer with the same content as the original buffer. If the buffer is read-only, then the ReadOnlyBufferException is thrown.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 { ByteBuffer buffer ... Read More

Duration hashCode Method in Java

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

183 Views

The hash code value of the duration can be obtained using the hashCode() method in the Duration class in Java. This method requires no parameters and it returns the hash code value of the duration.A program that demonstrates this is given as follows −Example Live Demoimport java.time.Duration; public class Demo { public static void main(String[] args) { Duration d = Duration.ofDays(2); System.out.println("The duration is: " + d); System.out.println("The hash code of the duration is: " + d.hashCode()); ... Read More

Advertisements