Map to create new value from int array in Java

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

387 Views

Let’s say the following is our int array elements:10, 50, 100, 200, 250, 300, 400, 500Here, we are mapping and creating a new value by incrementing each int element with 1:Arrays.stream(new int[] {10, 50, 100, 200, 250, 300, 400, 500}).map(val -> val + 1)Now find the average:Arrays.stream(new int[] {10, 50, 100, 200, 250, 300, 400, 500}).map(val -> val + 1).average()The following is an example to Map and create new value from int array:Exampleimport java.util.Arrays; public class Demo {    public static void main(String[] args) throws Exception {       Arrays.stream(new int[] {10, 50, 100, 200, 250, 300, 400, 500}).map(val -> val + 1).average()          .ifPresent(System.out::println);    } }Output227.25

What is the difference between const int*, const int * const, and int const *?

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

390 Views

Here we will see some different types of variable declaration based on integer pointers integer constants and the integer constant pointers.To determine them we will use the Clockwise/Spiral Rule. By discussing the terms, we can understand the rules also.The const int *. This is used to tell the compiler that this is a pointer type variable, and this can store address of some constant int. The Clock rule is saying like this −Now the another one is const int * const. This is used to denote that this is one constant pointer variable, which can store the address of another ... Read More

Java Connection getClientInfo() method with example

Arushi
Updated on 30-Jul-2019 22:30:26

980 Views

The getClientInfo() method of the Connection interface returns the name and values of the client info properties of the current connection. This method returns a properties object.To retrieve the values of the client info properties file.Register the driver using the registerDriver() method of the DriverManager class as −//Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver());Get the connection using the getConnection() method of the DriverManager class as −//Getting the connection String url = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(url, "root", "password");Create a properties object as −Properties properties = new Properties();Add the required key-value pairs to the above created Properties object as −properties.put("user_name", "new_user"); properties.put("password", "password");Set the above ... Read More

Query for multiple parameters in MongoDB?

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

1K+ Views

To query for multiple parameters in MongoDB, you can use the dot(.) notation. Let us first create a collection with documents −> db.multipleParametersDemo.insertOne( ...    { ...       "CustomerName" : "Larry", ...       "CustomerDetails" : [ ...          { ...             "CustomerCountryName" : "US", ...             "CustomerBankName" : "HDFC", ...             "CustomerBalance" : 17363, ...          } ...       ], ...       "Purchase" : 1456, ... ...    } ... ); ... Read More

How to set the background color of a single tab in a JTabbedPane Container with Java?

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

742 Views

To set the background color of a single tab, use the setBackgroundAt() method. This gives an option to mention the index and the color. The index here is the index of the specific tab you want to color.Let us first create a JTabbedPane −JTabbedPane tabbedPane = new JTabbedPane();Now, set the background color for one of the tabs with index 2 −tabbedPane.setBackgroundAt(2, Color.RED);The following is an example to set the background color of a single tab in a JTabbedPane container −Examplepackage my; import javax.swing.*; import java.awt.*; public class SwingDemo {    public static void main(String args[]) {       JFrame ... Read More

Java Program to set JComboBox in JOptionPane

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

1K+ Views

Let us first crate a ComboBox and add items to it −Object[] sports = { "Football", "Cricket", "Squash", "Baseball", "Fencing", "Volleyball", "Basketball" }; JComboBox comboBox = new JComboBox(sports);Now, add the combo box to the JOptionPane −JOptionPane.showMessageDialog(null, comboBox, "Fav Sports", JOptionPane.QUESTION_MESSAGE);The following is an example to set JComboBox in JOptionPane −Examplepackage my; import java.awt.GridBagLayout; import javax.swing.JComboBox; import javax.swing.JOptionPane; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) throws Exception {       JPanel panel = new JPanel(new GridBagLayout());       Object[] sports = { "Football", "Cricket", "Squash", "Baseball", "Fencing", "Volleyball", "Basketball" };       JComboBox ... Read More

MySQL ORDER BY with different ordering for some of the values as descending and others ascending?

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

99 Views

You can use the field() for this. Let us first create a table −mysql> create table DemoTable    (    Value int    ); Query OK, 0 rows affected (0.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(70); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(60); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(56); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(81); Query OK, 1 row affected (0.44 sec) mysql> ... Read More

What is difference between a pointer and reference parameter in C++?

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

6K+ Views

PointersPointer variables are used to store the address of variable.SyntaxType *pointer;InitializationType *pointer; Pointer=variable name;ReferencesWhen a parameter is declared as reference, it becomes an alternative name for an existing parameter.SyntaxType &newname=existing name;InitializationType &pointer; Pointer=variable name;The main differences between pointers and reference parameters are −References are used to refer an existing variable in another name whereas pointers are used to store address of variable.References cannot have a null value assigned but pointer can.A reference variable can be referenced by pass by value whereas a pointer can be referenced by pass by reference.A reference must be initialized on declaration while it is not ... Read More

C/C++ program to shutdown a system?

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

473 Views

Here we will see how we can shut down the system by writing a simple C or C++ code. The shutdown process varies in different OS. If we are Linux user, we can use this terminal command to shut down.shutdown –P nowIf we are using Windows system, we can use this command −c:\windows\system32\shutdown /iWe will see the code for Linux and WindowsExample(Linux)#include using namespace std; int main() {    system("shutdown -P now"); }Example(Windows)#include using namespace std; int main() {    system("c:\windows\system32\shutdown /i "); }

Java Program to select all the items in a JList

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

460 Views

To select all the items in a JList, use setSelectionInterval() and set a range:JList list = new JList(sports); int begn = 0; int end = list.getModel().getSize() - 1; if (end >= 0) {    list.setSelectionInterval(begn, end); }The following is an example to select all the items in a JList:Examplepackage my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame {    static JFrame frame;    static JList list;    public static void main(String[] args) {       frame = new JFrame("JList Demo");       SwingDemo s = new SwingDemo();       JPanel panel = new JPanel(); ... Read More

Advertisements