Found 7442 Articles for Java

Sum of list with stream filter in Java

AmitDiwan
Updated on 13-Jul-2020 11:53:38

1K+ Views

To get sum of list with stream filter in Java, the code is as follows −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args)    {       List my_list = new ArrayList();       my_list.add(11);       my_list.add(35);       my_list.add(56);       my_list.add(78);       my_list.add(91);       System.out.println(sum(my_list));    }    public static int sum(List my_list)    {       System.out.println("In the main function, the sum of list with filter is ");       return my_list.stream().filter(i -> i > 5).mapToInt(i -> i).sum();    } ... Read More

Structure and Members of the Java Program

AmitDiwan
Updated on 13-Jul-2020 11:52:46

315 Views

While writing any piece of code in Java, there is a certain set of rules and regulations that need to be followed, that is considered as a standard. For example − A class contains variables, and functions. The functions can be used to work with the variables. Classes can be extended, and improvised too.Basic structureList of packages that are imported; public class {    Constructor (can be user defined or implicitly created)    {       Operations that the constructor should perform;    }    Data elements/class data members;    User-defined functions/methods;    public static void main (String ... Read More

Ways to read input from console in Java

AmitDiwan
Updated on 09-Jul-2020 06:50:11

5K+ Views

Let us see some ways to read input from console in Java −Exampleimport java.util.Scanner; public class Demo{    public static void main(String args[]){       Scanner my_scan = new Scanner(System.in);       String my_str = my_scan.nextLine();       System.out.println("The string is "+my_str);       int my_val = my_scan.nextInt();       System.out.println("The integer is "+my_val);       float my_float = my_scan.nextFloat();       System.out.println("The float value is "+my_float);    } }OutputThe string is Joe The integer is 56 The float value is 78.99A class named Demo contains the main function. An instance of the ... Read More

Using Variables in JShell of Java 9

AmitDiwan
Updated on 09-Jul-2020 06:47:43

499 Views

In JShell 9, variables can be declared during a session. Once the user has logged into the session, they can declare a variable as follows −jshell> int val = 56 ;Italics indicate the terminal, once the user has logged in to their session.The above line would print the below output. The semicolon in the above line is optional, and it will run fine without the semicolon also.Outputval = = > 56When an integer value is defined by assigning it to a variable name on the JShell, and it is executed, by pressing ‘Enter’ key, it is displayed on the next ... Read More

Using underscore in Numeric Literals in Java

AmitDiwan
Updated on 09-Jul-2020 06:45:34

176 Views

Followig is the code showing how to use underscore in numeric literals in Java −Example Live Demopublic class Demo{    public static void main (String[] args) throws java.lang.Exception{       int my_num_1 = 6_78_00_120;       System.out.println("The number is : " + my_num_1);       long my_num_2 = 2_00_11_001;       System.out.println("The number is : " + my_num_2);       float my_num_3 = 4.01_981F;       System.out.println("The number is : " + my_num_3);       double my_num_4 = 12.89_46_061;       System.out.println("The number is : " + my_num_4);    } }OutputThe number is ... Read More

Using predefined class name as Class or Variable name in Java

AmitDiwan
Updated on 09-Jul-2020 06:43:55

1K+ Views

Using a predefined class name as a class nameLet us see an example −Example Live Demopublic class Number{    public static void main (String[] args){       System.out.println("Pre-defined class name can be used as a class name");    } }OutputPre-defined class name can be used as a class nameThe class Number has a main function that displays a message when it is executed. The main function takes string values as arguments.Using a predefined class name as a variable nameLet us see an example −Example Live Demopublic class String{    public static void main (java.lang.String[] args){       System.out.println("Pre-defined class name ... Read More

Using TreeMap to sort User-defined Objects in Java

AmitDiwan
Updated on 09-Jul-2020 06:42:38

202 Views

To sort user-defined object in Java, the code is as follows −Example Live Demoimport java.io.*; import java.util.*; public class Demo{    static void sort_objects(String my_data){       String[] my_vals = my_data.split(" ");       Map my_map = new TreeMap();       for (int i = 1; i < my_vals.length; i += 2){          int my_age = Integer.parseInt(my_vals[i]);          String name = my_vals[i - 1];          if (my_map.containsKey(my_age)){             ArrayList my_list = my_map.get(my_age);             my_list.add(name);           ... Read More

Unreachable Code Error in Java

AmitDiwan
Updated on 09-Jul-2020 06:40:33

1K+ Views

Unreachable code error occurs when the code can’t be compiled due to a variety of reasons, some of which include: infinite loop, return statement before the unreachable line of code.Let us see an example −Example Live Demopublic class Demo{    public static void main(String args[]){       int val = 5;       for (;;){          if (val == 5){             break;             System.out.println("If the condition is not true, this line would be printed. ");          }       }    } ... Read More

Java program to find the first non-repeating character from a stream of characters

AmitDiwan
Updated on 23-Jul-2024 18:21:33

1K+ Views

Finding the first non-repeating character in a string is a common programming problem. It involves finding the first character that appears only once in the string. This task helps understand how to manipulate strings and use basic data structures in Java. Problem Statement Given a string, identify the first character that does not repeat. If all characters repeat, indicate that there is no non-repeating character. Input tutorialspoint Output The first non-repeating character of the string is T Steps to find the first non-repeating character from a stream of characters Below are the steps to find the first non-repeating character from a ... Read More

Java program to find the number occurring odd number of times

AmitDiwan
Updated on 07-Nov-2024 01:07:17

588 Views

In this article, we will learn how to find the number in an array that appears an odd number of times using Java. By looping through the array and counting occurrences of each number, the program will detect and return the one with an odd frequency. Problem Statement Given an array identify the integer that occurs an odd number of times. Below is the demostration of the same − Input 34, 56, 99, 34, 55, 99, 90, 11, 12, 11, 11, 34 Output The number that occurs odd number of times in the array is 34 Steps to find the number occurring ... Read More

Advertisements