Found 7442 Articles for Java

Java Lambda Expression with Collections

AmitDiwan
Updated on 04-Jul-2020 09:16:54

870 Views

Sorting the elements of a list using lambda expression −Example Live Demoimport java.util.*; public class Demo{    public static void main(String[] args){       ArrayList my_arr = new ArrayList();       my_arr.add(190);       my_arr.add(267);       my_arr.add(12);       my_arr.add(0);       System.out.println("Before sorting, elements in the array list are : " + my_arr);       Collections.sort(my_arr, (o1, o2) -> (o1 > o2) ? -1 : (o1 < o2) ? 1 : 0);       System.out.println("After sorting, elements in the array list are : " + my_arr);    } }OutputBefore sorting, elements ... Read More

Java Interview Questions on Constructors

Shriansh Kumar
Updated on 11-Sep-2024 10:18:51

955 Views

There could be numerous interview questions on Constructors, it is not possible to cover all in just one article. However, we have researched and assorted the most popular Java interview questions on Constructors. In most of the Java interviews, the interviewers always start by asking basic questions. They can test one's knowledge in just a few minutes of the interviews. Therefore, it is essential to get thorough with the fundamental concepts of Java such as class, object and constructor. In Java interviews, the first question one might encounter is to define constructors. So, let's start our discussion with this question. ... Read More

Java ConcurrentHashMap - clear()

AmitDiwan
Updated on 04-Jul-2020 09:06:38

321 Views

The clear function is used to clear up the mapping between the key value pairs. This way, the ConcurrentHashMap mappings would be cleared.Syntaxpublic void clear()Let us see an example −Example Live Demoimport java.util.concurrent.ConcurrentHashMap; import java.util.*; public class Demo{    public static void main(String[] args){       Map my_map = new ConcurrentHashMap();       my_map.put("This", "35");       my_map.put("is", "78");       my_map.put("sample", "99");       System.out.println("The map contains the below elements " + my_map);       my_map.clear();       System.out.println("The elements after the clear function is called on it " + my_map);    } ... Read More

Java Concurrency – join() method

Alshifa Hasnain
Updated on 25-Feb-2025 19:20:42

571 Views

In this article, we will learn about concurrency in Java. It allows multiple threads to execute simultaneously, sharing system resources efficiently. One of the methods provided in the Thread class for managing thread execution is the join() method. What is the join() Method? the join () function is used to join the beginning of the execution of a thread to the end of the execution of another thread. This way, it is ensured that the first thread won’t run until the second thread has stopped executing. This function waits for a specific number of milliseconds for the thread to terminate. ... Read More

Java Concurrency – sleep() method

AmitDiwan
Updated on 04-Jul-2020 09:01:30

418 Views

The sleep functionThis sleep function is used to ensure that the currently executing thread goes to sleep for a specific amount of milliseconds which is passed as a parameter to the function. The thread stops executing for that number of milliseconds.Let us see an exampleExample Live Demoimport java.lang.*; public class Demo implements Runnable{    Thread my_t;    public void run(){       for (int i = 0; i < 3; i++){          System.out.println(Thread.currentThread().getName()+ " " + i);          try{             Thread.sleep(100);          }       ... Read More

Java Concurrency – yield() method

Alshifa Hasnain
Updated on 17-Jan-2025 19:44:20

875 Views

In this article, we will learn that concurrency in Java allows multiple threads to execute simultaneously, sharing system resources efficiently. One of the methods provided in the Thread class for managing thread execution is the yield() method. What is the yield() Method? The yield() method is a static method of the Thread class that hints to the thread scheduler that the current thread is willing to pause its execution in favor of other threads of the same or higher priority. It is part of Java's concurrency toolbox and is primarily used for fine-tuning thread execution. Syntax of yield function − ... Read More

Jar files in Java

AmitDiwan
Updated on 04-Jul-2020 08:55:10

743 Views

JAR is a file format that helps in aggregating Java class file, along with its associated metadata and resources into a single file.Creation of a JAR file − The jar cf command can be used, where ‘cf’ means creating the file.jar cf jar_file_name files_to_compressOnce a jar file has been created, a default manifest file is also created. Only one manifest file is present in a specific archive, and it will have the extension ‘mf’ and will be in the pathname. Thismanifest file helps give information about the files that have been compressed/present in the package.Viewing a JAR file − The ... Read More

Iterator vs Collection in Java

AmitDiwan
Updated on 04-Jul-2020 08:53:41

436 Views

IteratorIt is used in Collection Framework so as to retrieve elements as and when they are required.public interface IteratorIt can be used with the ‘next’ function to move and access the next elements. The ‘remove’ function can be used to remove an element from the data structure.It is quicker in comparison to Collections, since the number of operations associated with Iterator is less.Below is an example of an iterator working with a list −Example Live Demomport java.io.*; import java.util.*; public class Demo{    public static void main(String[] args){       ArrayList my_list = new ArrayList();       my_list.add("Its");   ... Read More

Island of Isolation in Java

AmitDiwan
Updated on 04-Jul-2020 08:51:19

1K+ Views

After an object has been used, it is deallocated from the memory using the Garbage Collector class. The objects are destroyed based on the fact that no reference to that object is present. The Garbage Collector class calls the ‘finalize’ function on the object that needs to be destroyed.What is island of isolation?When two objects ‘a’, and ‘b’ reference each other, and they are not referenced by any other object, it is known as island of isolation.It is a group of objects which reference each other but they are not referenced but other objects of other applications at all.Note − ... Read More

Is an array a primitive type or an object in Java?

AmitDiwan
Updated on 04-Jul-2020 08:45:15

2K+ Views

Array is considered to be an object in Java. The reason behind this is that an array can be created using the ‘new’ keyword. The ‘new’ keyword/operator is always used to create an object. This is how an array is perceived as an object.The direct parent class or super class of any array is the ‘Object’ class. Every array type in Java belongs to a certain class. This indicates that there are explicit classes for integer array types, float array types, double array types, and so on.Arrays can be dynamically created, and be assigned variables as well.Let us see an ... Read More

Advertisements