Found 9150 Articles for Object Oriented Programming

Java Integer bitCount() method

AmitDiwan
Updated on 20-Sep-2019 10:58:54

202 Views

The java.lang.Integer.bitCount() method returns the number of one-bits in the two's complement binary representation of the specified int value.At first, set an int value −int val = 210;Now, find the number of one-bits −Integer.bitCount(val)Following is an example to implement the bitCount() method in Java −Examplepublic class Main {    public static void main(String[] args) {       int val = 210;       System.out.println("Number = " + val);       System.out.println("Binary = " + Integer.toBinaryString(val));       // returns the number of one-bits       System.out.println("Number of one bits = " + Integer.bitCount(val));    } }OutputNumber = 210 Binary = 11010010 Number of one bits = 4

Check if a value is present in an Array in Java

AmitDiwan
Updated on 20-Sep-2019 10:57:19

828 Views

At first sort the array −int intArr[] = {55, 20, 10, 60, 12, 90, 59}; // sorting array Arrays.sort(intArr);Now, set the value to be searched in an int variable −int searchVal = 12;Check for the presence of a value in an array −int retVal = Arrays.binarySearch(intArr, searchVal); boolean res = retVal > 0 ? true : false;Following is an example to check if a value is present in an array −Exampleimport java.util.Arrays; public class Main {    public static void main(String[] args) {       // initializing unsorted int array       int intArr[] = {55, 20, 10, ... Read More

Check if a String starts with any of the given prefixes in Java

AmitDiwan
Updated on 20-Sep-2019 10:54:48

520 Views

Let’s say the string is −String str = "Malyalam";Let’s say the prefixes are in an array −String[] prefixArr = { "Ga", "Ma", "yalam" };Now, to check whether the string starts with any of the abive prefix, use the startsWith() −if (Stream.of(prefixArr)    .anyMatch(str::startsWith))    System.out.println("TRUE"); else    System.out.println("FALSE");Following is an example to check is a string starts with any of the given prefixes −Exampleimport java.util.stream.Stream; class Main {    public static void main(String[] args) {       String str = "Malyalam";       String[] prefixArr = { "Ga", "Ma", "yalam" };       if (Stream.of(prefixArr)     ... Read More

Convert a Map to JSON using the Gson library in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 15:54:04

4K+ Views

Converting a map to a JSON object is our task here. Let's see how to convert a map to a JSON object in Java using the Gson library. Converting a Map to JSON using the Gson library JSON is a simple and lightweight data interchange format. It stores data in key-value pairs. A map is also a collection of key-value pairs. So, we can easily convert a map to a JSON object. If you are not familiar with JSON, refer JSON. And if you want to know more about Map, refer Map. Gson is developed by Google. It is an ... Read More

Convert a list of objects to JSON using the Gson library in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 15:58:08

8K+ Views

In this article, we will be discussing how to convert a list of objects to JSON using the Gson library in Java. JSON is an interchangeable format that is used for data exchange. Values in JSON are represented as key-value pairs. To know more about JSON, refer JSON. Java GSON Library: Converting a list of objects to JSON Gson is a third-party Java library developed by Google. It is used for converting Java objects to JSON and vice versa. In the Gson library, we can convert a list of objects to JSON using the toJson() method. We cannot use the ... Read More

Check if a string contains only alphabets in Java using ASCII values

AmitDiwan
Updated on 20-Sep-2019 08:29:26

482 Views

Let’s say we have set out inut string in myStr variable. Now loop through until the length of string and check for alphabets with ASCII values −for (int i = 0; i < myStr.length(); i++) {    char c = myStr.charAt(i);    if (!(c >= 'A' && c = 'a' && c = 'A' && c = 'a' && c

Check if a string contains only alphabets in Java using Regex

AmitDiwan
Updated on 20-Sep-2019 08:24:32

976 Views

At first, convert the string into character array. Here, name is our string −char[] ch = name.toCharArray();Now, loop through and find whether the string contains only alphabets or not. Here, we are checking for not equal to a letter for every character in the string −for (char c : ch) {    if(!Character.isLetter(c)) {       return false;    }Following is an example to check if a string contains only alphabets using RegexExample Live Demopublic class Main {    public static boolean checkAlphabet(String name) {       char[] ch = name.toCharArray();       for (char c : ch) { ... Read More

Check if a string contains only alphabets in Java using Lambda expression

AmitDiwan
Updated on 20-Sep-2019 08:19:04

782 Views

Let’s say our string is −String str = "Amit123";Now, using allMatch() method, get the boolean result whether the string has only alphabets or now −boolean result = str.chars().allMatch(Character::isLetter);Following is an example to check if a string contains only alphabets using Lambda Expressions −Exampleclass Main {    public static void main(String[] args) {       String str = "Amit123";       boolean result = str.chars().allMatch(Character::isLetter);       System.out.println("String contains only alphabets? = "+result);    } }OutputLet us see another example with a different input −String contains only alphabets? = falseExampleclass Main {    public static void main(String[] args) ... Read More

Insert a string into another string in Java

AmitDiwan
Updated on 27-Aug-2024 18:46:59

4K+ Views

In this article, we will explore how to insert a string into another string at a specific position using Java. To do so we will be using the StringBuffer class.  StringBuffer class: StringBuffer class creates and manipulates strings that can be changed. It changes the contents of the string without creating a new object each time. Problem Statement Write a program in Java to insert a string into another string − Input That's good! Output Index where new string will be inserted = 6Resultant String = That's no good! Steps to insert a string into ... Read More

Initialize HashMap in Java

AmitDiwan
Updated on 20-Sep-2019 07:57:58

710 Views

The HashMap class uses a hashtable to implement the Map interface. This allows the execution time of basic operations, such as get( ) and put( ), to remain constant even for large sets.Following is the list of constructors supported by the HashMap class.Sr.NoConstructor & Description1HashMap( )This constructor constructs a default HashMap.2HashMap(Map m)This constructor initializes the hash map by using the elements of the given Map object m.3HashMap(int capacity)This constructor initializes the capacity of the hash map to the given integer value, capacity.4HashMap(int capacity, float fillRatio)This constructor initializes both the capacity and fill ratio of the hash map by using its ... Read More

Advertisements