Sort a list that places nulls first in Java

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

475 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 first with nullsFirst:list.sort(Comparator.nullsFirst(String::compareTo));The following is an example to sort a List that places nulls first: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.nullsFirst(String::compareTo));       System.out.println("List placing nulls first = "+list); ... Read More

How do I catch a Ctrl+C event in C++?

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

10K+ Views

The CTRL + C is used to send an interrupt to the current executing task. In this program, we will see how to catch the CTRL + C event using C++.The CTRL + C is one signal in C or C++. So we can catch by signal catching technique. For this signal, the code is SIGINT (Signal for Interrupt). Here the signal is caught by signal() function. Then one callback address is passed to call function after getting the signal.Please see the program to get the better idea.Example#include #include #include #include using namespace std; // Define ... Read More

Java DatabaseMetaData getTimeDateFunctions() method with example

Vikyath Ram
Updated on 30-Jul-2019 22:30:26

40 Views

This method retrieves the list of time and date functions supported by the current database. The names returned by this method are the Open CLI time and date function names.This method returns a String value holding the list of functions separated by commas (", ").To get the list of the time and date functions supported by the underlying database −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 ... Read More

How to increment inside the update() function in MongoDB?

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

209 Views

You can use $inc operator to increment. Let us first create a collection with documents −> db.addInUpdateFunctionDemo.insertOne({"PlayerName":"Chris", "PlayerScore":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2b3f4345990cee87fd893") } > db.addInUpdateFunctionDemo.insertOne({"PlayerName":"Robert", "PlayerScore":88}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2b3fc345990cee87fd894") } > db.addInUpdateFunctionDemo.insertOne({"PlayerName":"David", "PlayerScore":99}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2b407345990cee87fd895") }Following is the query to display all documents from a collection with the help of find() method −> db.addInUpdateFunctionDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd2b3f4345990cee87fd893"),    "PlayerName" : "Chris",    "PlayerScore" : 78 } {    "_id" : ObjectId("5cd2b3fc345990cee87fd894"),    "PlayerName" : "Robert", ... Read More

How to set JCheckBoxMenuItem for a MenuItem in Java?

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

82 Views

Let’s say we have a menu and within that we need to set the JCheckBoxMenuItem. Here’s the Menu −JMenu editMenu = new JMenu("Edit"); editMenu.setMnemonic(KeyEvent.VK_E); menuBar.add(editMenu);Now, set the JCheckBoxMenuItem and add it to the editMenu created above −JCheckBoxMenuItem checkMenuItem = new JCheckBoxMenuItem("SelectAll"); checkMenuItem.setMnemonic(KeyEvent.VK_B); editMenu.add(checkMenuItem);The following is an example to set JCheckBoxMenuItem for a MenuItem in Java −Examplepackage my; import java.awt.Color; import java.awt.event.KeyEvent; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.UIManager; public class SwingDemo {    public static void main(final String args[]) {       JFrame frame = new JFrame("MenuBar Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     ... Read More

How to display vertical grid lines in a table with Java?

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

262 Views

To display vertical grid lines in a table, use the setShowVerticalLines() method. Let us first create a table −String[][] rec = {    { "1", "Steve", "AUS" },    { "2", "Virat", "IND" },    { "3", "Kane", "NZ" },    { "4", "David", "AUS" },    { "5", "Ben", "ENG" },    { "6", "Eion", "ENG" }, }; String[] header = { "Rank", "Player", "Country" }; JTable table = new JTable(rec, header);Now, let us display vertical grid lines −table.setShowVerticalLines(true);You can also give a color to these lines −table.setGridColor(Color.blue);The following is an example to display vertical grid lines in JTable ... Read More

Get the count of the most frequently occurring values in MySQL?

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

950 Views

For this, use the aggregate function COUNT() along with GROUP BY. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Value int    ); Query OK, 0 rows affected (0.74 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Value) values(976); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Value) values(67); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Value) values(67); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Value) values(1); Query OK, 1 row affected (0.27 sec) mysql> ... Read More

C++ Program to Implement Sparse Array

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

2K+ Views

A sparse matrix is a matrix in which majority of the elements are 0. An example for this is given as follows.The matrix given below contains 5 zeroes. Since the number of zeroes is more than half the elements of the matrix, it is a sparse matrix.0 0 9 5 0 8 7 0 0AlgorithmBegin    Declare a 2D array a[10][10] to the integer datatype.       Initialize some values of array.    Declare i, j, count to the integer datatype.       Initialize count = 0.    Declare row, col to the integer datatype.       ... Read More

HTML DOM Input Checkbox name Property

Rama Giri
Updated on 30-Jul-2019 22:30:26

111 Views

The HTML DOM Input Checkbox name property returns a string, which is the value of the name attribute of input checkbox. User can also set it to a new string.SyntaxFollowing is the syntax −Returning string valueinputCheckboxObject.nameSetting name attribute to a string valueinputCheckboxObject.name = ‘String’ExampleLet us see an example of Input Checkbox name property − Live Demo Name Attribute of Checkbox Enable new name: Change Name Attribute    function getFormID(){       var oldNameAttr = document.getElementById("formID");       var newNameAttr = document.getElementById("nameAttr");       if(oldNameAttr.checked == true){     ... Read More

C/C++ Program for Largest Sum Contiguous Subarray?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

328 Views

An array of integers is given. We have to find sum of all elements which are contiguous. Whose sum is largest, that will be sent as output.Using dynamic programming we will store the maximum sum up to current term. It will help to find sum for contiguous elements in the array.Input: An array of integers. {-2, -3, 4, -1, -2, 1, 5, -3} Output: Maximum Sum of the Subarray is : 7AlgorithmmaxSum(array, n)Input − The main array, the size of the array.Output − maximum sum.Begin    tempMax := array[0]    currentMax = tempMax    for i := 1 to n-1, ... Read More

Advertisements