Programming Articles - Page 2181 of 3363

Binary Search in C++ Standard Template Library (STL)

sudhir sharma
Updated on 03-Jan-2020 06:21:12

290 Views

A binary search known as logarithmic search is a search algorithm that searches for an element in a sorted array. The algorithm recursively divides the array into two halves, if the element is found at the mid position then return otherwise call the divide and check again until the element is found.WorkingThe algorithm works by comparing the middle element of the sorted array with the element that is to be searched.If the search element is equal to the middle element, then return the index of the element.If the search element is greater than the middle element, search in the left ... Read More

Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)

sudhir sharma
Updated on 16-Jun-2025 18:18:23

4K+ Views

Binary search is the fastest algorithm for finding an item from a sorted list of items. C++ STL provides built-in functions to perform binary search operations on sorted ranges of elements. In this article, we will learn binary_search, lower_bound and upper_bound functions in C++ STL. First of all, let's understand what binary search is. What is Binary Search? Binary search is a fast search algorithm with run-time complexity of (log n). This search algorithm works on the principle of divide and conquer, since it divides the array into half before searching. This algorithm works only on sorted collection of ... Read More

How to use UnaryOperator interface in lambda expression in Java?

raja
Updated on 13-Jul-2020 08:00:30

1K+ Views

UnaryOperator is a functional interface that extends the Function interface. It represents an operation that accepts a parameter and returns a result of the same type as its input parameter. The apply() method from Function interface and default methods: andThen() and compose() are inherited from the UnaryOperator interface. A lambda expression and method reference can use UnaryOperator objects as their target.Syntax@FunctionalInterface public interface UnaryOperator extends FunctionExample-1import java.util.function.UnaryOperator; public class UnaryOperatorTest1 {    public static void main(String[] args) {       UnaryOperator operator = t -> t * 2;   // lambda expression       System.out.println(operator.apply(5));       System.out.println(operator.apply(7));       System.out.println(operator.apply(12));    } }Output10 ... Read More

How to use BinaryOperator interface in lambda expression in Java?

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

1K+ Views

BinaryOperator is one of a functional interface from java.util.function package and having exactly one abstract method. A lambda expression or method reference uses BinaryOperator objects as their target. BinaryOperator interface represents a function that takes one argument of type T and returns a value of the same type.BinaryOperator Interface contains two static methods, minBy() and maxBy(). The minBy() method returns a BinaryOperator that returns the greater of two elements according to a specified Comparator while the maxBy() method returns a BinaryOperator that returns the lesser of two elements according to a specified Comparator.Syntax@FunctionalInterface public interface BinaryOperator extends BiFunctionExampleimport java.util.function.BinaryOperator; public class BinaryOperatorTest {    public static void main(String[] args) {       BinaryOperator ... Read More

CharMatcher Class in Java

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

285 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

177 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

118 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

193 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

971 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

Advertisements