Skip to main content

Posts

Showing posts from December, 2018

How to determine whether an array contains a particular value in Java?

Java Array : Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. So to determine or to check whether a value is contained by the array or not, we have so many solutions. You can use ArrayUtils.contains from Apache Commons Lang public static boolean contains(Object[] array, Object objectToFind) Note that this method returns false if the passed array is null. There are also methods available for primitive arrays of all kinds. But after Java 8 introduction of stream api's and enhacements of java array its become easy to deal such situations.
Since java-8 you can now use Streams. String[] values = {"AB","BC","CD","AE"}; boolean contains = Arrays.stream(values).anyMatch("s"::equals); To check whether an array of int, double or long conta…

What are heap memory generation in Java?

This is a very frequently asked question in Java Tuning. Other related question are like 
What is the young generation?What is the old generation?What is the permanent generation?How does the three generations interact/relate to each other?
For the HotSpot Java VM, the memory pools for serial garbage collection are the following.
Eden Space (heap): The pool from which memory is initially allocated for most objects.Survivor Space (heap): The pool containing objects that have survived the garbage collection of the Eden space.Tenured Generation (heap): The pool containing objects that have existed for some time in the survivor space.Permanent Generation (non-heap): The pool containing all the reflective data of the virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas.
Code Cache (non-heap): The HotSpot Java VM also includes a code cache, containing memory that is used for compilatio…