Display a TreeSet in Reverse Order in Java

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

410 Views

To display a TreeSet in reverse order, you need to use Comparator. Let us first create an Integer array and use it for the TreeSet elements:Integer arr[] = { 25, 100, 125, 200, 250, 400, 450, 550, 600, 700};Now, use reverseOrde() comparator to reverse the natural ordering:Set set = new TreeSet(Collections.reverseOrder());Now, add individual elements of the set as the Integer array elements:for (int i = 0, l = arr.length; i < l; i++) {    set.add(arr[i]); }Exampleimport java.util.Collections; import java.util.Set; import java.util.TreeSet; public class Demo {    public static void main(String args[]) throws Exception {       Integer arr[] ... Read More

Get Records in a Certain Order Using MySQL

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

127 Views

You can use ORDER BY IF() to get records in a certain order. Let us first create a table:mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(20),    Branch varchar(20) ); Query OK, 0 rows affected (1.96 sec)Following is the query to insert some records in the table using insert command:mysql> insert into DemoTable(FirstName, Branch) values('John', 'CS'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(FirstName, Branch) values('Carol', 'ME'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable(FirstName, Branch) values('David', 'ME'); Query OK, 1 row affected (0.11 sec) mysql> ... Read More

FloatBuffer Array Method in Java

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

208 Views

A float array for the buffer can be obtained using the method array() in the class java.nio.FloatBuffer. If the returned array is modified, then the contents of the buffer are also similarly modified and vice versa. 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 {          FloatBuffer buffer = FloatBuffer.allocate(n);          buffer.put(4.5F);          buffer.put(1.2F); ... Read More

Generate Parameters Method in Java

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

136 Views

The parameters can be generated using the method generateParameters() in the class java.security.AlgorithmParameterGenerator. This method requires no parameters and it returns the AlgorithmParameter object.A program that demonstrates this is given as follows −Example Live Demoimport java.security.*; import java.util.*; public class Demo {    public static void main(String[] argv) {       try {          AlgorithmParameterGenerator apGenerator = AlgorithmParameterGenerator.getInstance("DiffieHellman");          apGenerator.init(1024);          AlgorithmParameters aParameters = apGenerator.generateParameters();          System.out.println(aParameters);       } catch (NoSuchAlgorithmException e) {          System.out.println("Error!!! NoSuchAlgorithmException");       } catch (ProviderException e) ... Read More

Why is iostream EOF Inside a Loop Condition Considered Wrong

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

204 Views

The iostream::eof in a loop is considered as wrong because we haven’t reached the EOF. So it does not mean that the next read will succeed.When we want to read a file using file streams in C++. And when we use a loop to write in a file, if we check the end of file using stream.eof(), we are actually checking whether the file has reached end or not.Example Code#include #include using namespace std; int main() { ifstream myFile("myfile.txt"); string x; while(!myFile.eof()) { myFile >> ... Read More

Use NavigableSet Interface in Android

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

157 Views

This example demonstrates about How to use Navigable set interface in AndroidStep 1 &minus Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.                                 In the above code, we have taken the name and record number as Edit text, when the user clicks on save button it will store the data into ArrayList. Click on the refresh button to get ... Read More

Get Launcher Thumbnail Size in Android

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

139 Views

This example demonstrates How to get launcher thumbnail size in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view to show activity launcher thumbnail size.Step 3 − Add the following code to src/MainActivity.javapackage com.example.myapplication; import android.app.ActivityManager; import android.app.admin.DevicePolicyManager; import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity ... Read More

MonthDay equals Method in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

138 Views

The equality of two MonthDay objects can be determined using the equals() method in the MonthDay class in Java. This method requires a single parameter i.e. the MonthDay object to be compared. Also it returns true if both the MonthDay objects are equal and false otherwise.A program that demonstrates this is given as followsExample Live Demoimport java.time.*; public class Main {    public static void main(String[] args) {       MonthDay md1 = MonthDay.parse("--02-22");       MonthDay md2 = MonthDay.parse("--02-22");       System.out.println("The MonthDay md1 is: " + md1);       System.out.println("The MonthDay md2 is: " + ... Read More

Duration toMillis Method in Java

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

172 Views

The value of a particular duration in the number of milliseconds can be obtained using the toMillis() method in the Duration class in Java. This method requires no parameters and it returns the duration in the number of milliseconds.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.ofSeconds(1);       System.out.println("The duration is: " + d);       System.out.println("The number of milliseconds in the duration is: " + d.toMillis());    } }OutputThe duration is: PT1S The number of ... Read More

Convert Positive Value to Negative in MySQL

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

1K+ Views

Let us first create a tablemysql> create table recordsDemo    -> (    -> UserId int,    -> Value int    -> ); Query OK, 0 rows affected (0.52 sec)Now insert some records in the table using insert command.The query is as followsmysql> insert into recordsDemo values(1, 10); Query OK, 1 row affected (0.17 sec) mysql> insert into recordsDemo values(3, 598); Query OK, 1 row affected (0.18 sec) mysql> insert into recordsDemo values(5, 786); Query OK, 1 row affected (0.25 sec) mysql> insert into recordsDemo values(7, 189); Query OK, 1 row affected (0.16 sec) mysql> insert into recordsDemo values(9, 345); ... Read More

Advertisements