Differences Between Method Reference and Constructor Reference in Java

Aishwarya Naglot
Updated on 01-Sep-2025 13:30:13

3K+ Views

The Method Reference and Constructor Reference are part of Java 8's functional programming features, they used for refering to methods and constructors without executing them. They are often used in conjunction with functional interfaces, such as those defined in the java.util.function package. Method Reference A method reference is a shorthand representation of a lambda expression for calling a method. A method reference refers to a method without executing it. Method references can refer to static methods, instance methods, and constructors. Constructor Reference A constructor reference is a unique kind of method reference that is a reference to a constructor. ... Read More

Sort a Map by Values in Java

Aishwarya Naglot
Updated on 01-Sep-2025 13:28:52

493 Views

Java HashMap is a hash table-based implementation of Java's Map interface. It is a collection of key-value pairs.In this article, we will learn to sort a map by values in Java. Following are the ways to sort a map by values in Java: Using Comparator Using Streams Using Comparator We can sort a map by values using a custom comparator. The comparator will compare the values of the map entries and sort them accordingly. Steps to sort a map by values using Comparator: Step 1: Create a HashMap and populate it with some key-value pairs. Step 2: ... Read More

Constructor Chaining in Java Programming

Aishwarya Naglot
Updated on 01-Sep-2025 13:19:16

2K+ Views

The constructor chaining is a specific sequence of calling constructors when a user initializes an object in a certain way. This technique is used when multiple constructors are invoked one after another, based on the instance class. It is also closely related to inheritance, where the role of a subclass constructor is to call the constructor of its superclass. You can perform constructor chaining in two ways: Within the same class - where one constructor calls another constructor of the same class. Across different classes - where a subclass constructor calls ... Read More

Why an Interface Doesn't Have a Constructor in Java

Shriansh Kumar
Updated on 01-Sep-2025 13:16:22

9K+ Views

Interfaces in Java are used for defining a contract that classes can implement. They can contain method signatures, default methods, static methods, and constants. we must implement the methods defined in the interface in the implementing class. Constructor in an Interface A Constructor is to initialize the non-static members of a particular class with respect to an object. An Interface in Java doesn't have a constructor because all data members in interfaces are public static final by default, they are constants (assign the values at the time of declaration). There are ... Read More

Lookup Enum by String Value in Java

Aishwarya Naglot
Updated on 01-Sep-2025 12:56:48

885 Views

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables). For example, we have constants like week days, months, or set of languages etc. In Java, we can create an enum to represent these constants. In this article, we will learn how to lookup an enum by its string value in Java. Java Program to Lookup Enum by String Value lets learn how you can convert a string to its corresponding enum value using the following ways and also print the enum value: Using valueOf() Method Using EnumSet and stream() Using ... Read More

Remove Sublist from ArrayList in Java

Aishwarya Naglot
Updated on 01-Sep-2025 12:52:19

2K+ Views

In this article, let's learn how to remove a sublist from an ArrayList in Java. The ArrayList class is a part of the Java Collections Framework. It is a resizable array and an implementation of the List interface. The ArrayList class is used when we want to store a list of elements in a dynamic array. Sub-list is a specific portion of an array list. Basically, it is a list of elements that are part of the original list but it is not the entire list itself. Using subList() and clear() method. ... Read More

Check If a String Contains a Substring Ignoring Case in Java

Aishwarya Naglot
Updated on 01-Sep-2025 12:51:02

5K+ Views

Problem Statement The task is, given a string and a substring, you have to check if a substring is present in a string or not. Do not mind if the case of the string and substring is different, it should not matter. For example, if the string is "Hello World" and the substring is "hello", then the output should be true. Even if the substring is "HELLO" or "HeLLo", the output should be true. Solution To solve this problem, we will first convert the string and the substring to lowercase using the toLowerCase() method. The toLowerCase () method of the ... Read More

Sorting Contents of a String that Holds Integer Values in Java

Aishwarya Naglot
Updated on 01-Sep-2025 12:50:16

4K+ Views

Problem Statement Here we have a string that contains integer values, our task is to sort those integer values in ascending order. For example, if the string is "31134", the sorted string should be "11334". Solution We can solve the above problem using multiple ways: Using char array Using Stream API Using Bubble Sort Using char array We will use the toCharArray() method of the String class to convert the string into a character array. Then we will sort the character array using the Arrays.sort() method. ... Read More

Get First and Last Elements from ArrayList in Java

Aishwarya Naglot
Updated on 01-Sep-2025 12:48:08

16K+ Views

ArrayList is a part of the Java Collections Framework. Which is a dynamic type of array that can grow and shrink as needed. It is a resizable array implementation of the List interface. The ArrayList class is used when we want to store a list of elements in a dynamic array. In this article, let's learn how to get the first and last elements from an ArrayList in Java. The get() method of the ArrayList class accepts an integer representing the index value and returns the element of the current ArrayList object at the specified index. Therefore, if you pass ... Read More

Validate String for Alphabets in Java

Aishwarya Naglot
Updated on 01-Sep-2025 12:45:51

5K+ Views

Problem Statement The given problem is to validate a string to check if that string contains only alphabets (both uppercase and lowercase) and does not contains any other characters like numbers, special characters, etc.. For example, the string "HelloWorld" is valid as it contains only alphabets, while "Hello123" is not valid as it contains numbers. Solution We can solve this problem by using following methods: Using Regular Expressions Manual Checking using loop Using Character.isAlphabetic() Using Regular Expressions We can use regular expressions to validate a string for alphabets. The regular expression ^[a-zA-Z]+$ matches a string that contains only ... Read More

Previous 1 ... 3 4 5 6 7 ... 7806 Next
Advertisements