Binary Search can be performed in Java using the method java.util.Collections.binarySearch(). This method requires two parameters i.e. the list in which binary search is to be performed and the element that is to be searched. It returns the index of the element if it is in the list and returns -1 if it is not in the list.A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.Collections; import java.util.List; public class Demo { public static void main(String args[]) { List aList = new ArrayList(); aList.add("James"); aList.add("George"); ... Read More
vars() function belongs to the collection of inbuilt functions provided by the Python standard library. It returns the __dic__ attribute of an associated object to the console.Syntaxvars(object)Return TypeParametersvars() function accepts only one parameter. It takes an object as its parameter which can be any module, class or any object having __dict__ attribute associated with it.This parameter is optional in nature. In case the function is used without parameters A dictionary containing local symbol table is displayed.Exceptions InvolvedIf the argument passed doesn’t match the attribute, it raises the TypeError exception.ScopeVars() acts like locals() method when no argument is passed.The locals() method ... Read More
A list can be sorted in ascending order using the java.util.Collections.sort() method. This method requires a single parameter i.e. the list to be sorted and no value is returned. The ClassCastException is thrown by the Collections.sort() method if there are mutually incomparable elements in the list.A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.Collections; import java.util.List; public class Demo { public static void main(String args[]) { List aList = new ArrayList(); aList.add("James"); aList.add("Harry"); aList.add("Susan"); aList.add("Emma"); aList.add("Peter"); ... Read More
To get max values for distinct elements, use $sort and $group in MongoDB aggregate(). Let us create a collection with documents −> db.demo750.insertOne({id:101, value:50}); { "acknowledged" : true, "insertedId" : ObjectId("5eae74b2a930c785c834e566") } > db.demo750.insertOne({id:102, value:40}); { "acknowledged" : true, "insertedId" : ObjectId("5eae74c8a930c785c834e567") } > db.demo750.insertOne({id:101, value:110}); { "acknowledged" : true, "insertedId" : ObjectId("5eae74dba930c785c834e568") }Display all documents from a collection with the help of find() method −> db.demo750.find();This will produce the following output −{ "_id" : ObjectId("5eae74b2a930c785c834e566"), "id" : 101, "value" : 50 } { "_id" : ObjectId("5eae74c8a930c785c834e567"), "id" : 102, "value" : 40 } ... Read More
Simplex Stop – and – Wait protocol for noisy channel is data link layer protocol for data communications with error control and flow control mechanisms. It is popularly known as Stop – and –Wait Automatic Repeat Request (Stop – and –Wait ARQ) protocol. It adds error control facilities to Stop – and – Wait protocol.This protocol takes into account the facts that the receiver has a finite processing speed and that frames may get corrupted while transmission. If data frames arrive at the receiver’s end at a rate which is greater than its rate of processing, frames can be dropped ... Read More
To update each field of documents in collection with a formula, use MongoDB update(). Let us create a collection with documents −> db.demo749.insertOne({"details":[{"id":1, a:10}, {"id":2, a:5}, {"id":3, a:20}]}); { "acknowledged" : true, "insertedId" : ObjectId("5eae6fb0a930c785c834e565") }Display all documents from a collection with the help of find() method −> db.demo749.find().pretty();This will produce the following output −{ "_id" : ObjectId("5eae6fb0a930c785c834e565"), "details" : [ { "id" : 1, "a" : 10 }, { "id" : 2, ... Read More
The document is a record in a collection. Each document has the limitation of 16 MB size. The document is wrapped inside the curly bracket ({}).Let us create a collection with documents −> db.demo748.insertOne({_id:101, Name:"Chris", Age:21}); { "acknowledged" : true, "insertedId" : 101 } > db.demo748.insertOne({_id:102, Name:"Bob", Age:20}); { "acknowledged" : true, "insertedId" : 102 } > db.demo748.insertOne({_id:103, Name:"David", Age:23}); { "acknowledged" : true, "insertedId" : 103 } > db.demo748.insertOne({_id:104, Name:"Sam", Age:19}); { "acknowledged" : true, "insertedId" : 104 }Display all documents from a collection with the help of find() method −> db.demo748.find();This will produce the following output −{ "_id" ... Read More
The elements of a Collection can be appended at the end of the Vector using the method java.util.Vector.addAll(). This method takes a single parameter i.e. the Collection whose elements are added to the Vector and it returns true if the Vector is changed.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec1 = new Vector(); vec1.add(7); vec1.add(3); vec1.add(5); vec1.add(9); vec1.add(2); System.out.println("The Vector vec1 elements ... Read More
To push values, use $push along with update() with multi field set to TRUE. Let us create a collection with documents −> db.demo747.insertOne({"CountryName":["US", "IND"]}); { "acknowledged" : true, "insertedId" : ObjectId("5eae6a50a930c785c834e55f") } > db.demo747.insertOne({"CountryName":["UK", "US"]}); { "acknowledged" : true, "insertedId" : ObjectId("5eae6a57a930c785c834e560") } > db.demo747.insertOne({"CountryName":["UK", "IND"]}); { "acknowledged" : true, "insertedId" : ObjectId("5eae6a60a930c785c834e561") }Display all documents from a collection with the help of find() method −> db.demo747.find();This will produce the following output −{ "_id" : ObjectId("5eae6a50a930c785c834e55f"), "CountryName" : [ "US", "IND" ] } { "_id" : ObjectId("5eae6a57a930c785c834e560"), "CountryName" : [ "UK", "US" ] } ... Read More
To find posts older than current date in MongoDB, use $lte. Let us create a collection with documents −> db.demo746.insertOne({DueDate:new Date("2020-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5eae67eca930c785c834e55b") } > db.demo746.insertOne({DueDate:new Date("2020-10-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5eae67eda930c785c834e55c") } > db.demo746.insertOne({DueDate:new Date("2020-03-05")}); { "acknowledged" : true, "insertedId" : ObjectId("5eae67eea930c785c834e55d") } > db.demo746.insertOne({DueDate:new Date("2020-05-04")}); { "acknowledged" : true, "insertedId" : ObjectId("5eae67f1a930c785c834e55e") }Display all documents from a collection with the help of find() method −> db.demo746.find();This will produce the following output −{ "_id" : ObjectId("5eae67eca930c785c834e55b"), "DueDate" : ISODate("2020-01-10T00:00:00Z") } { "_id" : ObjectId("5eae67eda930c785c834e55c"), ... Read More