Set.of() example in Java
Java Hungry
by Subham Mittal
2d ago
In this post, I will be sharing the Java Set.of() example. This method was introduced in Java 9. Let's dive deep into the topic: Read Also: How to Iterate HashSet in Java with Examples Java Set.of() example Syntax According to Oracle docs, the Java Set.of() method has the following syntax: static <E> Set<E> of(E... elements) Returns It returns an immutable set containing an arbitrary number of elements. 1. Simple Set.of() Example Simple example of Set.of() method is shown below: import java.util.Set;public class SetOfExample {public static void main(String args ..read more
Visit website
[Solved] Syntax error on token(s), misplaced construct(s)
Java Hungry
by Subham Mittal
4d ago
In this post, I will be sharing how to fix syntax error on token(s), misplaced construct(s) in Java. This syntax-based error is a compile-time error and mostly troubles Java beginners. Read Also: [Fixed] Syntax error on token "else", delete this token As always, first, we will produce the syntax error on token(s), misplaced construct(s) error before moving on to the solution. Let's dive deep into the topic: Note: This syntax-based compiler error can be produced only in the Eclipse IDE. [Fixed] Syntax error on token(s), misplaced construct(s) 1. Producing the error using the else statement i ..read more
Visit website
Print deck of cards in Java with Source Code
Java Hungry
by Subham Mittal
1w ago
In this post, I will be sharing how to print a deck of cards in Java with source code. We all know that there is a total of 52 cards in a deck and they belong to one of the four suits(Spade, Heart, Diamond, and Club). Read Also: Dice Roll program in Java Let's dive deep into the topic: Print Deck of Cards - Java ProgramWe will perform the below steps to write a Java program for printing a deck of cards: 1. We will create two string arrays named cardRank and cardSuit. 2. We will create another array named deckOfCards of length 52 to represent the deck of cards. 3. We will initialize the de ..read more
Visit website
Return an ArrayList in Java [2 ways]
Java Hungry
by Subham Mittal
1w ago
In this post, we will learn how to return an ArrayList in Java. There are two kinds of methods in Java 1. static method 2. non-static method Read Also: Create ArrayList with values in Java We will look into both of them for returning ArrayList. Let's dive deep into the topic: Return ArrayList in Java 1. Returning ArrayList for static method You might already know that static methods can be called with the help of the class name. Below are the steps to perform for returning an ArrayList from a static method: 1. Create a method that has ArrayList as the return type. 2. Create an ArrayList ..read more
Visit website
Top 10 Java Generics Interview Questions and Answers
Java Hungry
by Subham Mittal
1M ago
In this post, I will be sharing the frequently asked top 10 Java generics interview questions and answers for experienced professionals. Generics was introduced in Java 5. Generics is used for type safety and it automatically manages the casting during compilation. That helps developers write the code without worrying about Type-management. It is introduced to enhance or supplement the Java collections. But we can use those outside collections as well. Top 10 Java Generics Interview Questions and Answers Q1. How to create a Generic class in Java? We can declare a class using the following sy ..read more
Visit website
Java Initialize an Array with 0 [4 ways]
Java Hungry
by Subham Mittal
1M ago
In this post, I will be sharing how to initialize an Array with 0 in Java. There are 4 ways to achieve our goal: 1. Using default initialization of Array 2. Using the Arrays.fill() method 3. Using declaration and initialization simultaneously 4. Using for loop Read Also: Find first non-repeated element in integer array in Java Java Initialize an Array with 0 1. Using default initialization of ArrayBy using default initialization, we can easily initialize Array with 0.  This line int[] arr = new int[4]; will create an array arr of length 4 with each element has been assigned 0 value ..read more
Visit website
Get first and last element of an array in Java
Java Hungry
by Subham Mittal
1M ago
In this post, I will be sharing how to get the first and last element of an array in Java. We will first understand the question with the help of examples before moving on to the solution. Read Also:  Array Interview Questions and Answers in Java Example 1 Given Integer array is: [12, 81, 17, 13, 53, 25] Output: firstElement:12, lastElement:25 Example 2 Given String array is: ["Alive", "is", "Awesome"] Output: firstElement:Alive lastElement:Awesome Let's dive deep into the topic: Get first and last element of an array in Java 1. Get first and last element of a Str ..read more
Visit website
Check if Collection is empty or null in Java with examples
Java Hungry
by Subham Mittal
1M ago
In this short post, I will be sharing how to check if a Collection is empty or null in Java with examples. There are two ways to achieve our goal: 1. Using the Helper method 2. Using Apache commons library [Easiest] Read Also:  Check if HashMap is empty in Java Let's dive deep into the topic: Check if Collection is empty or null in Java with examples 1. Using Helper method We can easily check whether a Collection is empty or null in Java using a helper method. We have created a helper method with the name isEmptyOrNull() that returns the boolean value as shown below in the example ..read more
Visit website
Convert String to LocalDateTime in Java [2 ways]
Java Hungry
by Subham Mittal
2M ago
In this post, I will be sharing how to convert String to LocalDateTime in Java. LocalDateTime provides date-time without time-zone information. This class is thread-safe and immutable. It is represented in the ISO-8601 calendar system. LocalDateTime class was introduced in Java8. Let's dive deep into the topic: Read Also: [Solved] Unable to obtain LocalDateTime from TemporalAccessor Convert String to LocalDateTime in Java 1. Using parse() method We can easily convert String to LocalDateTime in Java using LocalDateTime's static parse() method as shown below in the example. import java.time ..read more
Visit website
[Solved] Syntax error on token "else", delete this token
Java Hungry
by Subham Mittal
2M ago
In this post, I will be sharing how to fix syntax error on token "else", delete this token error in Java. It is a compile-time error. It is a syntax based error and mostly troubles Java beginners. Read Also: [Fixed] Syntax error on token ",", { expected after this token As always, first, we will produce the syntax error on token "else", delete this token error before moving on to the solution. Let's dive deep into the topic: Note: This compiler error can be produced only in the Eclipse IDE. [Fixed] Syntax error on token "else", delete this token 1. Producing the error using ; (semi-colon) W ..read more
Visit website

Follow Java Hungry on Feedspot

Continue with Google
OR