Found 2616 Articles for Java

BinaryOperator Interface in Java

AmitDiwan
Updated on 02-Jan-2020 11:12:11

1K+ Views

The BinaryOperator interface represents an operation upon two operands of the same type, producing a result of the same type as the operands.Following are the methods −Modifier and TypeMethod and DescriptionmaxBy(Comparator

CaseFormat Class in Java

AmitDiwan
Updated on 02-Jan-2020 11:07:50

46 Views

The CaseFormat class is a utility class for converting between various ASCII case formats −Modifier and TypeMethod and DescriptionObjectclone()Overrides Cloneable.booleanequals(Object obj)Overrides equals.String.format(double number)Specialization of format.abstract StringBufferformat(double number, StringBuffer toAppendTo, FieldPosition pos)Specialization of format.Stringformat(long number)Specialization of format.abstract StringBufferformat(long number, StringBuffer toAppendTo, FieldPosition pos)Specialization of format.ExampleLet us now see an example to implement the CaseFormat class with java file GuavaTester.java −import com.google.common.base.CaseFormat; public class GuavaTester {    public static void main(String args[]) {       GuavaTester tester = new GuavaTester();       tester.testCaseFormat();    }    private void testCaseFormat() {       String data = "test_data";       ... Read More

NumberFormat class in Java

AmitDiwan
Updated on 02-Jan-2020 11:05:52

1K+ Views

NumberFormat helps you to format and parse numbers for any locale. It is the abstract base class for all number formats.Following are some of the methods of the NumberFormat class−Modifier and TypeMethod and DescriptionObjectclone()Overrides Cloneable.booleanequals(Object obj)Overrides equals.String.format(double number)Specialization of format.abstract StringBufferformat(double number, StringBuffer toAppendTo, FieldPosition pos)Specialization of format.Stringformat(long number)Specialization of format.abstract StringBufferformat(long number, StringBuffer toAppendTo, FieldPosition pos)Specialization of format.ExampleLet us now see an example to implement the NumberFormat class − Live Demoimport java.text.NumberFormat; import java.util.Locale; public class Demo {    public static void main(String[] args) {       NumberFormat n = NumberFormat.getCurrencyInstance(Locale.FRANCE);       double points = 2.15;   ... Read More

Byte Class Fields in Java with example

AmitDiwan
Updated on 02-Jan-2020 11:00:57

106 Views

The Byte class wraps a value of primitive type byte in an object.Following are the fields for Byte class−static byte MAX_VALUE − This is constant holding the maximum value a byte can have, 27-1.static byte MIN_VALUE − This is constant holding the minimum value a byte can have, -27.static int SIZE − This is the number of bits used to represent a byte value in two's complement binary form.static Class TYPE − This is the Class instance representing the primitive type byte.ExampleLet us now see an example − Live Demoimport java.lang.*; public class Demo {    public static void main(String[] args){ ... Read More

Byte Class in Java

AmitDiwan
Updated on 02-Jan-2020 10:57:36

766 Views

The Byte class wraps a value of primitive type byte in an object. An object of type Byte contains a single field whose type is byte.Following are some of the methods of the Byte class −Sr.No.Method & Description1byte byteValue()This method returns the value of this Byte as a byte.2int compareTo(Byte anotherByte)This method compares two Byte objects numerically.3static Byte decode(String nm)This method decodes a String into a Byte.4double doubleValue()This method returns the value of this Byte as a double.5boolean equals(Object obj)This method compares this object to the specified object.6float floatValue()This method returns the value of this Byte as a float.7int hashCode()This ... Read More

Java program to create a sorted merged array of two unsorted arrays

AmitDiwan
Updated on 02-Jan-2020 13:08:52

597 Views

To create a sorted merged array of two unsorted arrays, at first, let us create two unsorted arrays−int[] arr1 = new int[] {50, 22, 15, 40, 65, 75}; int[] arr2 = new int[] {60, 45, 10, 20, 35, 56};Let us now create a new resultant array, that would have the merged array−Exampleint count1 = arr1.length; int count2 = arr2.length; int [] resArr = new int[count1 + count2]; Now, we will merge both the arrays in the resultant array resArr: while (i < arr1.length){    resArr[k] = arr1[i];    i++;    k++; } while (j < arr2.length){    resArr[k] = arr2[j]; ... Read More

Java program to remove duplicates elements from a List

AmitDiwan
Updated on 02-Jan-2020 13:08:15

547 Views

To remove duplicates from a List, the code is as follows −Example Live Demoimport java.util.ArrayList; import java.util.LinkedHashSet; import java.util.Set; public class Demo {    public static void main(String[] args){       ArrayList list = new ArrayList();       list.add("Jacob");       list.add("Gary");       list.add("Gary");       list.add("Harry");       list.add("Harry");       list.add("Kevin");       System.out.println("List = "+list);       Set set = new LinkedHashSet(list);       System.out.println("List after removing duplicate elements = "+set);    } }OutputList = [Jacob, Gary, Gary, Harry, Harry, Kevin] List after removing duplicate elements = ... Read More

Java program to find Largest, Smallest, Second Largest, Second Smallest in an array

AmitDiwan
Updated on 02-Jan-2020 10:41:35

3K+ Views

To find largest, smallest, second largest, second smallest in an array, the code is as follows -Example Live Demoimport java.util.*; public class Demo {    public static void main(String []args){       int arr[] = {55, 10, 8, 90, 43, 87, 95, 25, 50, 12};       System.out.println("Array = "+Arrays.toString(arr));       Arrays.sort(arr);       System.out.println("Sorted Array = "+Arrays.toString(arr));       System.out.println("Smallest element = "+arr[0]);       System.out.println("2nd Smallest element = "+arr[0]);       System.out.println("Largest element = "+arr[9]);       System.out.println("2nd Largest element = "+arr[8]);    } }OutputArray = [55, 10, 8, 90, ... Read More

Find average of a list in Java

AmitDiwan
Updated on 02-Jan-2020 10:37:56

748 Views

To find average of a list in Java, the code is as follows -Example Live Demoimport java.util.*; public class Demo {    public static void main(String []args){       List list = Arrays.asList(10, 20, 50, 100, 130, 150, 200, 250, 500);       IntSummaryStatistics summaryStats = list.stream()       .mapToInt((a) -> a)       .summaryStatistics();       System.out.println("Average of a List = "+summaryStats.getAverage());    } }OutputAverage of a List = 156.66666666666666Let us now see another example -Example Live Demoimport java.util.*; public class Demo {    public static void main(String []args){       List list = Arrays.asList(10, ... Read More

Java Program for credit card number validation

Sunidhi Bansal
Updated on 20-Dec-2019 11:42:07

5K+ Views

Given a long number containing digits of a credit card number; the task is to find whether the credit card number is valid or not with a program.For checking a credit card is valid or not, the following are the validations we have to be sure for declaring the result.A credit card’s number must have 13 to 16 digits, it must start with the following digits.All the visa cards start from 4 All the master cards start from 537 is the starting for American express cardsAll the discover cards start from 6Steps to check whether the credit card is valid or ... Read More

Advertisements