Convert a Set to Stream in Java using Generics

AmitDiwan
Updated on 25-Sep-2019 07:42:37

180 Views

Let’s say the following is our Set −Set set = new HashSet(Arrays.asList(15, 40, 60, 90, 120, 150, 200));Now, create a method to convert the above set to stream.StreamstreamOfInteger = convertSet(set);The method −private static Stream convertSet(Set set) {    return set.stream(); }ExampleFollowing is the program to convert a Set to Stream in Java using Generics −import java.util.*; import java.util.stream.*; import java.util.function.*; public class Demo {    private static Stream convertSet(Set set) {       return set.stream();    }    public static void main(String args[]) {       Set set = new HashSet(Arrays.asList(15, 40, 60, 90, 120, 150, ... Read More

Convert Map to Stream in Java

AmitDiwan
Updated on 25-Sep-2019 07:40:46

635 Views

At first, create a Map and set values −Map map = new HashMap(); map.put(1, "Kevin"); map.put(2, "Ryan"); map.put(3, "Nathan"); map.put(4, "Ricky"); map.put(5, "Shane"); map.put(6, "Adam");Now, convert the Map to Stream −Stream stream = map.entrySet().stream(); System.out.println("Stream (Map to Stream) = "+ Arrays.toString(stream.toArray()));ExampleFollowing is the program to convert a Map to a Stream in Java −import java.util.*; import java.util.stream.*; public class Demo {    public static void main(String args[]) {       Map map = new HashMap();       map.put(1, "Kevin");       map.put(2, "Ryan");       map.put(3, "Nathan");       map.put(4, "Ricky");       map.put(5, ... Read More

The String in Switch Case in Java

AmitDiwan
Updated on 25-Sep-2019 07:35:00

289 Views

The introduction of Java 7 enhanced the switch case i.e. it support string as well.At first, set a string −String department = "AKD05";Now, use the same string in switch as shown below −switch(department)ExampleNow, check for every string using case as we normally do while using SWITCH CASE. Following is an example to implement String in Switch Case −public class Demo {    public static void main(String[] args) {       String department = "AKD05";       switch(department) {          case "AKD01":             System.out.println("Finance");             break; ... Read More

Stream Distinct in Java

AmitDiwan
Updated on 25-Sep-2019 07:32:21

570 Views

The distinct() method of the stream class returns a stream consisting of the distinct elements of this stream. The syntax is as following −Stream distinct()ExampleFollowing is an example to implement the distinct() method in the Stream class −import java.util.*; public class Demo {    public static void main(String[] args) {       List list = Arrays.asList(10, 30, 40, 40, 50, 70, 90, 90, 100);       System.out.println("List = "+list);       System.out.println("Displaying only the distinct elements = ");       list.stream().distinct().forEach(System.out::println);    } }OutputList = [10, 30, 40, 40, 50, 70, 90, 90, 100] Displaying only ... Read More

Stream Concat in Java

AmitDiwan
Updated on 25-Sep-2019 07:30:33

264 Views

The concat() method of the Stream class in Java creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream. The syntax is as follows −concat(Stream

Copy Values of One Column to Another in MySQL

AmitDiwan
Updated on 25-Sep-2019 06:24:27

823 Views

Let us first create a table −mysql> create table DemoTable1 (    Name varchar(100),    Gender ENUM('MALE', 'FEMALE') ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values('Chris', 'Male'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1 values('Emma', 'Female'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;This will produce the following output −+-------+--------+ | Name  | Gender | +-------+--------+ | Chris | MALE   | | Emma  | FEMALE | +-------+--------+ 2 rows in set ... Read More

Implement HAVING Length Field in MySQL

AmitDiwan
Updated on 24-Sep-2019 14:14:14

133 Views

Let us first create a table −mysql> create table DemoTable (    Title text ); Query OK, 0 rows affected (0.39 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Introduction to MySQL'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Introduction to Java'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('Introduction to SQL'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Introduction to Python'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Introduction to SQL'); Query OK, 1 row affected (0.09 sec) ... Read More

Set Options While Creating a MySQL Table

AmitDiwan
Updated on 24-Sep-2019 14:10:19

208 Views

To display, use DESC command or information_schema.columns. Let us first create a table and set options −mysql> create table DemoTable (    Color SET('RED', 'GREEN', 'BLUE', 'YELLOW') ); Query OK, 0 rows affected (0.47 sec)Case 1Here is the query to get the list of available options for SET using the DESC command −mysql> desc DemoTable;This will produce the following output −+-------+------------------------------------+------+-----+---------+-------+ | Field | Type | Null | Key | ... Read More

Insert Random Numbers into a Table in MySQL

AmitDiwan
Updated on 24-Sep-2019 14:08:08

935 Views

To insert random numbers, use RAND() function from MySQL. Let us first create a table −mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (0.46 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.08 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-------+ | Value | +-------+ | ... Read More

Delete a Record with TableName, ColumnName, and Value in MySQL

AmitDiwan
Updated on 24-Sep-2019 14:04:14

137 Views

To delete a record in the required way, the syntax is as follows −delete from yourTableName where yourTableName .yourColumnName=yourValue;Let us first create a table −mysql> create table DemoTable (    StudentName varchar(100),    StudentAge int ); Query OK, 0 rows affected (0.81 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 21); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Robert', 20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('David', 22); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Mike', 24); Query OK, 1 row ... Read More

Advertisements