Java Program to sort a list placing nulls in the end

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

302 Views

Let us create a list first with string elements. Some of the elements are null in the List:List list = Arrays.asList("Jack", null, "Thor", null, "Loki", "Peter", null, "Hulk");Now, sort the above list and place nulls in the end with nullsLast:list.sort(Comparator.nullsLast(String::compareTo));The following is an example to sort a list placing nulls in the end:Exampleimport java.util.Arrays; import java.util.Comparator; import java.util.List; public class Demo {    public static void main(String... args) {       List list = Arrays.asList("Jack", null, "Thor", null, "Loki", "Peter", null, "Hulk");       System.out.println("Initial List = "+list);       list.sort(Comparator.nullsLast(String::compareTo));       System.out.println("List placing nulls ... Read More

How to initialize const member variable in a C++ class?

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

13K+ Views

Here we will see how to initialize the const type member variable using constructor?To initialize the const value using constructor, we have to use the initialize list. This initializer list is used to initialize the data member of a class. The list of members, that will be initialized, will be present after the constructor after colon. members will be separated using comma.Example#include using namespace std; class MyClass{    private:       const int x;    public:       MyClass(int a) : x(a){       //constructor    }    void show_x(){       cout

Java DatabaseMetaData getTableTypes() method with example

Rishi Raj
Updated on 30-Jul-2019 22:30:26

183 Views

The getTableTypes() method of the DatabaseMetadata interface is used to find out the type of tables supported by the underlying database.This method returns a ResultSet object holding the names of table types in each row in String format, under the column TABLE_TYPE.To get the description DatabaseMetadata object.Make sure your database is up and running.Register the driver using the registerDriver() method of the DriverManager class. Pass an object of the driver class corresponding to the underlying database.Get the connection object using the getConnection() method of the DriverManager class. Pass the URL the database and, user name, password of a user in the database, ... Read More

Increment a field in MongoDB document which is embedded?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

118 Views

Let’s say, here we are incrementing StudentScores for MongoDB which is inside StudentDetails −... "StudentScores": { ...    "StudentMathScore": 90, ...    "StudentMongoDBScore": 78 ... }Let us first create a collection with documents −> db.embeddedValueIncrementDemo.insertOne( ...    { ...       "StudentDetails": { ...          "StudentScores": { ...             "StudentMathScore": 90, ...             "StudentMongoDBScore": 78 ...          } ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2b670345990cee87fd896") }Following is the query to display ... Read More

How to set Echo Char for JPasswordField in Java?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

3K+ Views

With echo char, you can set a character that would appear whenever a user will type the password in the JPasswordField.Let us first create a new JPassword field −JPasswordField passwd = new JPasswordField();Now, use the setEchoChar() to set the echo char for password field. Here, we have asterisk (*) as the field for password −passwd.setEchoChar('*');The following is an example to set echo char for password field −Examplepackage my; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new ... Read More

How to create input Pop-Ups (Dialog) and get input from user in Java?

George John
Updated on 30-Jul-2019 22:30:26

554 Views

Use the JOptionPane.showInputDialog() to get input from user in a dialog box like “Which sports you play the most”, “What is your name”, etc. The following is an example to create input Pop-Ups (Dialog) and get input from user −Examplepackage my; import javax.swing.JOptionPane; public class SwingDemo {    public static void main(String[] args) {       String[] sports = { "Football", "Cricket", "Squash", "Baseball", "Fencing", "Volleyball", "Basketball" };       String res = (String) JOptionPane.showInputDialog(null, "Which sports you play the most?", "Sports",          JOptionPane.PLAIN_MESSAGE, null, sports, sports[0]);       switch (res) {     ... Read More

How to perform $gt on a hash in a MongoDB document?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

56 Views

The $gt is for greater than, wherein select those documents where the value of the field is greater than the specified value. Let us first create a collection with documents −> db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":1000, "PlayerLevel":2}, "PlayerName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7eba41a844af18acdffa9") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":0, "PlayerLevel":1}, "PlayerName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7ebbb1a844af18acdffaa") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":-10, "PlayerLevel":0}, "PlayerName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7ebd41a844af18acdffab") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":1, "PlayerLevel":1}, "PlayerName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7ebe31a844af18acdffac") }Following is the query to display all documents from a collection with the help of ... Read More

C++ Program to Find Closest Pair of Points in an Array

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

649 Views

This is the program to find closest pair of points in an array.AlgorithmsFor Distance between Closest pointBegin    Declare function Closest_dist_Spoint(poi stp[], int s, double dist, poi &pnt1, poi &pnt2) to the double datatype.    Declare Minimum to the double datatype.       Initialize Minimum = dist.    for (int i = 0; i < s; ++i)       for (int j = i+1; j < s && (stp[j].poi2 - stp[i].poi2) < Minimum; ++j)          if (Distance(stp[i], stp[j]) < Minimum) then          Minimum = Distance(stp[i], stp[j]).             ... Read More

What is instance variable hiding in Java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

1K+ Views

Whenever you inherit a superclass a copy of superclass’s members is created at the subclass and you using its object you can access the superclass members.If the superclass and the subclass have instance variable of same name, if you access it using the subclass object, the instance variables of the subclass hides the instance variables of the superclass irrespective of the types. This mechanism is known as field hiding or, instance variable hiding.But, since it makes code complicated field hiding is not recommended.ExampleIn the following example we have two classes Super and Sub one extending the other. They both have ... Read More

HTML DOM Input Checkbox Object

Kumar Varma
Updated on 30-Jul-2019 22:30:26

321 Views

The HTML DOM Input Checkbox Object represents an input HTML element with type checkbox.SyntaxFollowing is the syntax −Creating an with type checkboxvar checkboxObject = document.createElement(“input”); checkboxObject.type = “checkbox”;AttributesHere, “checkboxObject” can have the following attributes −AttributesDescriptionautofocusIt defines if the checkbox should be focused on initial page load.checkedIt defines the state of checkbox i.e. checked/unchecked.defaultCheckedIt returns the default value of checked attribute i.e. true/falsedefaultValueIt sets/returns the default value of checkboxdisabledIt defines if checkbox is disabled/enabledformIt returns a reference of enclosing form that contains the checkboxindeterminateIt sets/returns indeterminate state of checkboxnameIt defines the value of name attribute of a checkboxrequiredIt defines if ... Read More

Advertisements