Found 2617 Articles for Java

Java example to return a string representation of the deep contents of the array

AmitDiwan
Updated on 02-Jan-2020 11:19:29

62 Views

To return a string representation of the deep contents of the specified array −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       Object[] ob = {"One", "Two", "Three", "Four"};       System.out.println("Array elements...");       for (Object value : ob) {          System.out.println("Value = " + value);       }       System.out.println("The string representation of array is:");       System.out.println(Arrays.deepToString(ob));    } }OutputThis will produce the following output −Array elements... Value = One Value = Two Value = Three Value = Four The ... Read More

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

598 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

548 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

749 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

Advertisements