Learn Java and Programming through articles, code examples, and tutorials for developers of all levels.
Saturday, September 30, 2023
How to find length/size of ArrayList in Java? Example
How to remove all elements of ArrayList in Java - RemoveAll Example
How to loop over a TreeSet in Java with Example
How to Convert Vector to Array in Java? 2 Examples
How to sort a LinkedList in Java? Example Tutorial
Friday, September 29, 2023
10 Example of List in Java
Hello guys, Java, as a versatile and widely used programming language, offers a plethora of data structures to developers. One of them is List which is also fundamental component in Java's collection framework, play a pivotal role in managing and manipulating sequences of elements. In the past, I have shared 10 Examples of HashMap in Java and In this article, we'll delve into Java lists, exploring their features and providing ten illustrative examples to deepen your understanding. List are also a popular topic from Java interviews with questions like difference between ArrayList and LinkedList which have been asked to me almost 10 times in past 20 years.
How to Convert a List to a Set in Java with Example
Difference between ArrayList and HashMap in Java
One of the most critical differences between the HashMap and ArrayList class is that the former is the implementation of the hash table while the latter is a dynamic array that can resize itself. The HashMap and ArrayList are two of the most popular classes from the Java Collection framework. Though both are used to store objects they are completely different in their implementation, working, and usage. The main difference between ArrayList and HashMap is that ArrayList is an index-based data structure backed by an array while HashMap is a map data structure that works on hashing to retrieve stored values.
Thursday, September 28, 2023
How to sort HashSet in Java? Example
How to declare ArrayList with values in Java? Examples
int[] primes = {2, 3, 5, 7, 11, 13, 17};
or
String[] names = {"john", "Johnny", "Tom", "Harry"};
but unfortunately, ArrayList doesn't support such kind of declaration in Java. But don't worry, there is a workaround to declare an ArrayList with values e.g. String, integers, floats, or doubles by using the Arrays.asList() method, which is nothing but a shortcut to convert an Array to ArrayList.
How to add element at first and last position of linked list in Java? Example Tutorial
Difference between Class and Record in Java?
Hello guys, when it comes to defining data structures in Java, two primary options are at your disposal: records and classes. Each has its unique characteristics and use cases, and understanding the differences between them is crucial for making the right design choices in your Java applications. This has now also become a popular Java interview question and asked to one of my reader recently as well. Java classes have been the cornerstone of object-oriented programming for years, offering full customization and flexibility. They allow you to define complex objects with custom behavior, encapsulation, and fine-grained control over state changes.
Difference between HashMap and LinkedHashMap in Java
HashMap and LinkedHashMap are two of the most commonly used Map implementation in Java. The main difference between HashMap and LinkedHashMap is that LinkedHashMap maintains the insertion order of keys, the order in which keys are inserted into LinkedHashMap. On the other hand, HashMap doesn't maintain any order or keys, or values. In terms of performance, there is not much difference between HashMap and LinkedHashMap but yes LinkedHashMap has more memory footprint than HashMap to maintain doubly LinkedList which it uses to keep track of the insertion order of keys.
Difference between TreeMap and TreeSet in Java? Answer
The main difference between TreeMap and TreeSet is that TreeMap is an implementation of Map interface while TreeSet is an implementation of the Set interface. There are some similarities between both TreeMap and TreeSet and few differences as well. In this Java tutorial, we will first see similarities between TreeMap and TreeSet, and then you will learn some differences between TreeMap and TreeSet in Java.
Difference between FileReader vs FileInputStream in Java? Answer
How to Create Read Only and Unmodifiable ArrayList in Java? Example
Difference between HashSet and TreeSet in Java
Wednesday, September 27, 2023
How to handle click event in jQuery - Example
In the world of web development, interactivity is key to creating engaging user experiences. One fundamental aspect of interactivity is the ability to respond to user actions, such as clicks. jQuery, a fast and concise JavaScript library, has long been a favorite among developers for simplifying common tasks, including event handling. You can handle click event in jQuery by using event handlers. You can bind event handlers to any element by using on() method or click() method. The event handler is nothing but a function containing some code which will be executed when user will click on the element. For example, if user clicks on a button then color of button might changed to show on and off behavior. All this can be implemented by handling click event in jQuery.
Top 5 Java Main method Interview Questions with Answers
- Can we overload the main method in Java? Which main method JVM will call?
- Can we override the main method in Java?
- Can we make the main final in Java?
- Can we make the main synchronized in Java?
- How to call a non static method from main in Java?
Difference between NoClassDefFoundError vs ClassNotFoundExcepiton in Java
Difference between Public, Package, Private and Protected in Java?
Difference between HashSet vs TreeSet in Java? [Answer]
What is fail safe and fail fast Iterator in Java?
Tuesday, September 26, 2023
Difference between ROW_NUMBER(), RANK() and DENSE_RANK() in SQL
Difference between Method and Constructor in Java and OOP? Example
What is static in Java? Example Tutorial
Static in Java is related to class if a field is static means it belongs to the class, similarly static method belongs to classes and you can access both static method and field using the class name, for example, if count field is static in Counter class than you can access it as Counter.count, of course, subject to restriction applied by access modifier like private fields are only accessible in class on which they are declared, protected fields are accessible to all classes in the same package but only accessible in subclass outside the package, you can further see private vs protected vs public for complete details on access modifier.
Can you make an Abstract Class or Method Final in Java? Example
What is Variable and Method Hiding in Java - Example Tutorial
Difference between Abstract class and Interface in Java 8? Answer
Monday, September 25, 2023
How to Fix java.lang.OutOfMemoryError: Metaspace in Java? [Solution]
Hello guys, An OutOfMemoryError related to Metaspace indicates that your Java application has exhausted the memory allocated for the Metaspace area, which is used for class metadata and method information. This error typically occurs when an application dynamically generates and loads a large number of classes or when the Metaspace size is not properly configured to handle the application's requirements. Java class metadata (the virtual machine's internal presentation of Java class) is allocated in native memory (referred to here as metaspace). If metaspace for class metadata is exhausted, a java.lang.OutOfMemoryError exception with a detail MetaSpace is thrown.
Java Interface Example Tutorial
How Constructor Chaining works in Java - Example
5 Rules of Method Overloading and Overriding in Java? Examples
Difference between Method Overloading and Overriding in Java? Answer
In the last couple of articles, we have seen What is method overloading and What is method overriding in Java and now we will see What is difference between overloading and overriding in Java. Overloading vs overriding is one of those frequently asked Java interview questions which can not be ignored. Overloading vs Overriding has appeared in almost every Java interview, mostly at beginner and intermediate level like 2 to 4 years experience. In fact, most of those tricky Java interview Questions came from Overloading and Overriding.
Can You Override Private Method in Java ? Example
Sunday, September 24, 2023
How to fix java module error "Caused by: java.lang.reflect.InaccessibleObjectException: module java.base does not "opens java.util" to unnamed module"
The error message you're seeing, (Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.util.Comparator java.util.TreeMap.comparator accessible: module java.base does not "opens java.util" to unnamed module @6d4a82") or java.lang.reflect.InaccessibleObjectException, is related to module system changes introduced in Java 9 and later versions. It occurs when you try to access a field, method, or class that is not accessible due to module restrictions. In this case, it appears that you're trying to access the private final java.util.Comparator field of a java.util.TreeMap, which is not accessible because the java.base module does not open java.util to unnamed modules.
Difference between Class and Interface in Java and OOP? (with Example)
10 Essential Object Oriented Concepts for Java Developers
Difference between Static binding vs Dynamic binding in Java? [Answer]
What is Inheritance in Java with example - Object Oriented Programming Tutorial
Inheritance in Java or OOPS (Object-oriented programming) is a feature that allows coding reusability. In other words, Inheritance self-implies inheriting or we can say acquiring something from others. Along with Abstraction, Encapsulation, and Polymorphism, Inheritance forms the backbone of Object-oriented programming and Java. In Java, we use the term inheritance when one object acquires some property from other objects. In Java, inheritance is defined in terms of superclass and subclass. it is normally used when some object wants to use an existing feature of some class and also wants to provide some special feature, so we can say inheritance has given the advantage of reusability.
19 Java and OOP Method Overloading and Overriding Interview Questions and Answers
Difference between Abstraction and Polymorphism in Java and OOP [Answer]
Difference between instance and Object in Java
Saturday, September 23, 2023
How to create a class with methods and attributes in Java? Example Tutorial
Hello guys, we are again with new article that is on Creating Class with methods and attributes in Java. The main aim of this article is to give you idea about how to declare a class or method in Java and about different ways of declaring. In the world of Java programming, class serve as the fundamental building blocks for creating objects and defining the structure of your applications. These classes encapsulate data and behavior, allowing you to model real-world entities and implement the logic that governs their interactions.
How to convert a List to Array in Java? Example Tutorial
How to search a LinkedList in Java? Example
How to get first and last elements form ArrayList in Java
Difference between synchronized ArrayList and CopyOnWriteArrayList in Java?
PriorityQueue in Java? Example Tutorial
Friday, September 22, 2023
10 Common Coding Mistakes Every Java Developers Should be aware of
Hello guys, Java is a versatile and powerful programming language, but it's not immune to common coding mistakes that developers may inadvertently make. These errors can lead to unexpected behavior, performance issues, and even security vulnerabilities. In this article, I am going to share 10 such common coding mistakes Java developers often encounter, provide examples of each mistake, explain why they are problematic, and offer solutions to correct them. I strongly recommend every Java developer whether he is beginner, intermediate or senior developer to go through the list once so that you are aware of these mistakes. If you have been coding in Java then most likely you would have also made the same mistake, if you do, let us know in comments so that we all can learn from each other.
10 Examples of HashMap in Java - Programming Tutorial
How to convert a Map to List in Java? HashMap to ArrayList Example
How HashSet works in Java [Explained with Example]
How get() and put() methods of HashMap works in Java? [Explained]
Thursday, September 21, 2023
How to check if a Key Object Exists in HashMap Java? containsKey() Example Tutorial
if(map.get(key) != null){
System.out.println("key exists in Map");
}
This code is fragile, it will not work if you have added null values into HashMap because HashMap does allow null values.
How to sort HashMap by values in Java 8 [using Lambdas and Stream] - Example Tutorial
How to use ConcurrentHashSet from ConcurrentHashMap in Java 8 - Example Tutorial
Wednesday, September 20, 2023
How to initialize HashMap with values in Java? Example
How to Sort HashMap in Java based on Keys and Values
Monday, September 18, 2023
How to create a gzip file in Java? Example Tutorial
Right way to check if String is empty in Java with Example
Java 8 StringJoiner Example - How to join multiple Strings with delimiter in Java?
How to Join Multiple Strings in Java 8 - String join() Example
10 Points about String in Java with Examples
How to Remove all white spaces from String in Java? From Start, End and between Words Examples
3 ways to convert String to byte array in Java - Example Tutorial
Java - Convert String to Boolean Example
How to convert lowercase String to uppercase in Java? Example
How to Compare and Sort String by their length in Java? Example
Difference between == and equals() method in Java? String Example
How to check String contains a text in Java? contains() and indexOf() Example
Sunday, September 17, 2023
How to Send Asynchronous HTTP Requests using HttpClient in Java? Example Tutorial
ending HTTP requests is a common operation in modern web development for dealing with various APIs, online services, and servers. HTTP requests are often delivered synchronously, which means the programme waits for the response before moving on. However, asynchronous HTTP queries are helpful in situations when responsiveness and performance are essential. Asynchronous requests enable the programme to carry on running even as it waits in the background for the server to respond. The process of sending asynchronous HTTP requests in Java using HttpClient will be covered in this article, along with sample code, pros, cons, and some key points.
Difference between String and StringBuffer in Java? Answer
String and StringBuffer are two classes that are most widely used in any Java program. If I say you can not write a program without using String in Java then it would not be an exaggeration. The string is everywhere, the main method accepts a String argument, logs are String, etc. Though many Java programmers familiar with String, not many are careful while performing operations on String, Since String is final in Java; every operation like converting String into Uppercase, creating SubString, converting String to Lowercase all result in a separate new String Object, which can take trigger frequent garbage collection and affect your application performance.
7 Examples of formatting String Java - String.format() and printf() Examples
How to split String by comma in Java - Example Tutorial
5 Examples of substring() in Java
2 ways to parse String to int in Java - Example Tutorial
6 ways to convert char to String in Java - Example Tutorial
4 ways to concatenate Strings in Java [Example and Performance]
How to convert String to Float in Java and vice-versa - Tutorial
How to Generate Random Number between 1 to 10 - Java Example
How to Make Executable JAR file in Eclipse IDE - Java Example
Saturday, September 16, 2023
Difference between @GetMapping @PostMapping @PutMapping @DeleteMapping @PatchMapping
In the world of web development, the Spring Framework is widely known for its versatility and ease of use. It provides several annotations that simplify the process of handling HTTP requests and mapping them to specific methods in Spring controllers. Among these annotations, @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, and @PatchMapping are some of the most commonly used ones. In this article, we will explore each of these annotations in detail, highlighting their purposes, differences, and providing examples to better understand their usage.