Found 9150 Articles for Object Oriented Programming

How to limit the number of records, while retrieving data from a MongoDB collection using Java?

Maruthi Krishna
Updated on 10-Apr-2020 07:21:52

639 Views

While retrieving records from a MongoDB collection, you can limit the number of records in the result using thelimit() method.Syntaxdb.COLLECTION_NAME.find().limit(no.of records needed)The Java MongoDB library provides a method with the same name, to limit the number of records invoke this method (on the result of the find() method) by passing an integer value representing the required number of records.Exampleimport com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; public class LimitingRecords {    public static void main( String args[] ) {       //Creating a MongoDB client       MongoClient mongo = ... Read More

How to get the list of all the MongoDB databases using java?

Maruthi Krishna
Updated on 10-Apr-2020 07:18:10

1K+ Views

In MongoDB, you can view the list of databases using the show dbs command> show dbs admin config local myDatabase sampleDatabase students test testDBIn Java, you can get the list of all the databases in MongoDb using the getDatabaseNames() method.Exampleimport com.mongodb.client.MongoIterable; import com.mongodb.MongoClient; public class ListOfDatabases {    public static void main( String args[] ) {       // Creating a Mongo client       MongoClient mongo = new MongoClient( "localhost" , 27017 );       //Retrieving the list of collections       MongoIterable list = mongo.listDatabaseNames();       for (String name : list) {          System.out.println(name);       }    } }Outputadmin config local myDatabase sampleDatabase students test testDB

Explain Java MongoDB projections

Maruthi Krishna
Updated on 10-Apr-2020 07:16:54

1K+ Views

While retrieving data from MongoDb collections you can select only necessary data using projections. In Java, you can project necessary data while reading the documents from a collection using the projection() method. Invoke this method on the result of find(), bypassing the names of the required filed names as −projection(Projections.include("name", "age"));ExampleFollowing Java examples read the documents from a collection, using projection we are displaying the values of name and age fields only.import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Projections; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; public class ProjectionExample {    public static void main( String args[] ... Read More

How to skip documents while retrieving data from MongoDB, using java?

Maruthi Krishna
Updated on 10-Apr-2020 07:14:51

290 Views

While retrieving records from a MongoDB collection, you can skip records in the result using the skip() method.Syntaxdb.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)The Java MongoDB library provides a method with the same name, to skip records, invoke this method (on the result of the find() method) bypassing an integer value representing the number of records to skip.Exampleimport com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; public class SkipRecords {    public static void main( String args[] ) {       //Creating a MongoDB client       MongoClient mongo = new MongoClient( "localhost" , 27017 );   ... Read More

How to display OpenCV Mat object using Swings?

Maruthi Krishna
Updated on 10-Apr-2020 07:12:22

590 Views

The class ImageIcon is an implementation of the Icon interface that paints Icons from Images. You can display images on a Swing window using this class, the constructor of this class accepts a BufferedImage object as a parameter.Therefore to display an OpenCV image that is stored in a Mat object using Swing window, you need to convert it into a BufferedImage object and pass it as a parameter to the ImageIcon method.Exampleimport java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.InputStream; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.imgcodecs.Imgcodecs; public class DisplayingImagesUsingSwings {    public static ... Read More

How to display OpenCV Mat object using JavaFX?

Maruthi Krishna
Updated on 10-Apr-2020 07:10:36

462 Views

The JavaFX library provides a class with name ImageView using this you can display an image. This class accepts an object of the WritableImage class.To display an image Stored in OpenCV Mat object you need to convert it into a WritableImage object and pass it the ImageView class.Exampleimport java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.ImageView; import javafx.scene.image.WritableImage; import javafx.stage.Stage; import javax.imageio.ImageIO; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.imgcodecs.Imgcodecs; public class DisplayingImagesJavaFX extends Application {    @Override    public void start(Stage stage) throws IOException {       WritableImage writableImage = ... Read More

What is the importance of jmod format in Java 9?

raja
Updated on 09-Apr-2020 18:15:29

1K+ Views

Java 9 has introduced a new format called "jmod" to encapsulate modules. The jmod files can be designed to handle more content types than jar files. It can also package local codes, configuration files, local commands, and other types of data. The "jmod" format hasn't support at runtime and can be based on zip format currently. The jmod format can be used at both compile and link time and can be found in JDK_HOME\jmods directory, where JDK_HOME is a directory. The files in jmod format have a ".jmod" extension.Java 9 comes with a new tool called jmod, and it is located in the ... Read More

What are the different "/types" commands in JShell in Java 9?

raja
Updated on 09-Apr-2020 14:57:27

474 Views

JShell tool has introduced in Java 9 version. It is also called a REPL(Read-Evaluate-Print-Loop) tool that allows us to execute Java code and getting immediate results. We need to list out the declared types like class, interface, enum, and etc by using the "/types" command.Below are the different "/types" commands in JShell./types /types [ID] /types [Type_Name] /types -start /types -all/types: This command lists all active types (class, interface, enum) created in JShell./types [ID]: This command displays the type corresponding to the id [ID]./types [Type_Name]: This command displays the type corresponding to [Type_Name]./types -start: This command allows us to list the types that ... Read More

When to use the ServiceLoader class in a module in Java 9?

raja
Updated on 09-Apr-2020 12:22:21

285 Views

Java has a ServiceLoader class from java.util package that can help to locate service providers at the runtime by searching in the classpath. For service providers defined in modules, we can look at the sample application to declare modules with service and how it works.For instance, we have a "test.app" module that we need to use Logger that can be retrieved from System.getLogger() factory method with the help of LoggerFinder service.module com.tutorialspoint.test.app {    requires java.logging;    exports com.tutorialspoint.platformlogging.app;    uses java.lang.System.LoggerFinder; }Below is the test.app.MainApp class:package com.tutorialspoint.platformlogging.app; public class MainApp {    private static Logger LOGGER = System.getLogger();    public static void ... Read More

How to declare OpenCV Mat object using Java?

Maruthi Krishna
Updated on 09-Apr-2020 09:24:23

748 Views

In OpenCV Mat class represents a matrix object which is used to store images. You can also declare a Mat object manually −Load the OpenCV native library − While writing Java code using OpenCV library, the first step you need to do is to load the native library of OpenCV using the loadLibrary().Instantiate the Mat class − Instantiate the Mat class using any of the functions mentioned in this chapter earlier.Fill the matrix using the methods − You can retrieve particular rows/columns of a matrix by passing index values to the methods row()/col().you can set values to these using any of ... Read More

Advertisements