Programming Articles - Page 2183 of 3366

CharMatcher Class in Java

AmitDiwan
Updated on 20-Feb-2020 09:25:44

259 Views

The CharMatcher class determines a true or false value for any Java char value, just as Predicate does for any Object.Sr.NoMethods & Description1CharMatcher and(CharMatcher other)Returns a matcher that matches any character matched by both this matcher and other.2static CharMatcher anyOf(CharSequence sequence)Returns a char matcher that matches any character present in the given character sequence.3boolean apply(Character character)Deprecated. Provided only to satisfy the Predicate interface; use matches(char) instead.4String collapseFrom(CharSequence sequence, char replacement)Returns a string copy of the input character sequence, with each group of consecutive characters that match this matcher replaced by a single replacement character.5int countIn(CharSequence sequence)Returns the number of matching ... Read More

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

AmitDiwan
Updated on 11-Nov-2024 19:15:27

153 Views

In this article, we will learn how to generate a string representation of the contents of arrays in Java. Using Arrays.deepToString() transforms arrays into readable strings, making it easy to visualize their structure. This method is particularly useful for one-dimensional and multidimensional arrays. Problem StatementGiven arrays with varying structures, write a Java program that prints the elements of these arrays and their string representations using Arrays.deepToString().Input A one-dimensional array:Object[] ob = {"One", "Two", "Three", "Four"} A two-dimensional array:int[][] arr = {{10, 20, 30}, {40, 50, 75}, {100, 150, 200}}Output For the one-dimensional array:Array elements...Value = OneValue = TwoValue ... Read More

CaseFormat Class in Java

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

106 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

2K+ 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

173 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

950 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 find Largest, Smallest, Second Largest, Second Smallest in an array

Ashin Vincent
Updated on 16-Apr-2025 15:53:47

4K+ Views

In this problem we are given an array of numbers, and we have to find the largest, smallest, second largest, and second smallest elements in an array in Java. Let’s understand the problem better with the help of an example: Input int arr[] = {55, 10, 8, 90, 43, 87, 95, 25, 50, 12}; Output Smallest element = 8 2nd Smallest element = 10 Largest element = 95 2nd Largest element = 90 To solve this problem, use the following approaches: Using Arrays.sort() Using nested for-loops Optimized one-pass approach Using Arrays.sort() In this ... Read More

Find average of a list in Java

Ashin Vincent
Updated on 16-Apr-2025 15:53:07

1K+ Views

Given a list of numbers, our task is to calculate the average of this list. The average of a list can be found by adding all the elements in the list and dividing the sum by the total number of elements. In this article we will learn different methods to implement this in Java. There are multiple ways in Java to find the average of a list. In this article we will explore two different approaches: Using Java Streams Using for loop Find Average of a List Using Java Streams This approach shows how to find ... Read More

How to use Supplier interface in lambda expression in Java?

raja
Updated on 13-Jul-2020 07:22:11

7K+ Views

A Supplier interface is a pre-defined interface that represents the supplier of results. It is instantiated using lambda expression or method reference or default constructor. The functional method of a Supplier interface is the get() method. This interface belongs to java.util.function package.Syntax@FunctionalInterface public interface SupplierIn the below program, we can use the Supplier interface in a lambda expression. The get() method only returns a value and doesn't accept any argument, so a lambda expression having an empty argument part.Exampleimport java.util.*; import java.util.function.*; public class SupplierTest {    public static void main(String args[]) {       Supplier supplier1 = () -> "TutorialsPoint";   // lambda expression       System.out.println(supplier1.get());       ... Read More

Python - Column summation of tuples

Pradeep Elance
Updated on 02-Jan-2020 10:26:34

293 Views

Python has extensive availability of various libraries and functions which make it so popular for data analysis. We may get a need to sum the values in a single column for a group of tuples for our analysis. So in this program we are adding all the values present at the same position or same column in a series of tuples.It can be achieved in the following ways.Using for loop and zipUsing the for loop we loop through each item and apply zip function to gather the values from each column. Then we apply the sum function and finally get ... Read More

Advertisements