Posts

Showing posts with the label java tutorials

How to Convert Java 8 IntStream to a List

Image
1. Overview                     In this tutorial, we are going to convert an IntStream to List and to an array. IntStream is a stream of primitive int values and a List is an ordered collection that holds generic values. To convert a stream to the collection, we can call collect() method of Stream class, but this works when the streams hold non-primitive types. Calling collect() method on primitive streams give a compilation error "collect() in IntStream cannot be applied to". 2. Solutions 2.1. Call boxed() method before collecting to a List        IntStream has a method boxed() that returns a Stream consisting of the elements each boxed to an Integer. After getting a boxed stream, we can call the collect() method to convert into a List. Code: package streams; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; public class IntStreamTest { public stati...

How to write Immutable class in Java?

What is Immutability? 1. Immutability means read only, which means once you create Immutable object, you can't change it's state. How to make class Immutable? To make any class immutable, you have to apply following steps 1. Make the class as final, so that no other classes can extend. 2. Declare all the fields as private, so that no other classes can access directly. 3. Declare all the fields as final, and initialize those from constructor. 4. Provide getter methods for the private fields, But make sure to return copy of the field, not the actual field. if you return actual reference, it's state can be changeable. Advantages Of Immutable Objects: 1. One main advantage for using immutable objects is Thread Safety, because the values cannot ever change, they can be freely passed from thread to thread without ever needing locking. 2. This means that, because there is no locking with synchronized blocks, there is no bottleneck where multiple threads need to...

What Is Java? Where is It Used?

What is Java ? Java is a programming language developed by James Gasoling at Sun Microsystems. What is programming language? Do we really need a programming language? Programming Langauge:  We humans, communicate with each other using regional languages such as English, Hindi, Telugu, but if you want to communicate with computer, you need special languages such as C, C++, Java and so on. So a programming language is nothing but a language, which will be used to communicate with computers to get the work done. Using programming languages, we write programs - (sequence of instructions to the computer) - those will be executed by the computer and gives the result. For example, to search for a word in millions of words, you'll need a lot of time, but using programs you can get the result in fractions of seconds. Coming to Java, Java is an object oriented programming language. If you want to know different types of programming refer here "http://www.speakingcs.com/20...