Readers and Writers in Java

Readers and Writers and their respective subclasses provide simple I/O for text / character-based data. BufferedReader Introduction The BufferedReader class is a wrapper for other Reader classes that serves two main purposes: A BufferedReader provides buffering for the wrapped Reader. This allows an application to read charactersmone at a time without undue I/O overheads. A … Read more

Preferences in Java

Using preferences Preferences can be used to store user settings that reflect a user’s personal application settings, e.g. their editor font, whether they prefer the application to be started in full-screen mode, whether they checked a “don’t show this again” checkbox and things like that. public class ExitConfirmer { private static boolean confirmExit() { Preferences … Read more

Collect Elements of a Stream into a Collection in Java

Elements from a Stream can be easily collected into a container by using the Stream.collect operation: Collect with toList() and toSet() Elements from a Stream can be easily collected into a container by using theStream.collect operation: System.out.println(Arrays .asList(“apple”, “banana”, “pear”, “kiwi”, “orange”) .stream() .filter(s -> s.contains(“a”)) .collect(Collectors.toList()) ); // prints: [apple, banana, pear, orange] Other … Read more

Finding Statistics about Numerical Streams in Java

Java 8 provides classes called IntSummaryStatistics, DoubleSummaryStatistics and LongSummaryStatistics which give a state object for collecting statistics such as count, min, max, sum, and average. Version ≥ Java SE 8 List naturalNumbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); IntSummaryStatistics stats = .mapToInt((x) -> x) .summaryStatistics(); System.out.println(stats); Which will result … Read more

Using Streams and Method References to Write Self-Documenting Processes in Java

Method references make excellent self-documenting code, and using method references with Streams makes complicated processes simple to read and understand. Consider the following code: public interface Ordered { default int getOrder(){ return 0; } } public interface Valued { boolean hasPropertyTwo(); V getValue(); } public interface Thing { boolean hasPropertyOne(); Valued getValuedProperty(); } public List … Read more

Streams in Java

A Streams represents a sequence of elements and supports different kind of operations to perform computations upon those elements. With Java 8, Collection interface has two methods to generate a Stream: stream() andparallelStream(). Stream operations are either intermediate or terminal. Intermediate operations return a Stream so multiple intermediate operations can be chained before the Stream … Read more

Console I/O in Java

The Java Console class is be used to get input from console. It provides methods to read texts and passwords. If you read password using Console class, it will not be displayed to the user. The class is attached with system console internally. Reading user input from the console Using BufferedReader: System.out.println(“Please type your … Read more

Reference Types in Java

Reference datatypes in java are those which contains reference/address of dynamically created objects. These are not predefined like primitive data types. Different Reference Types java.lang.ref package provides reference-object classes, which support a limited degree of interaction with the garbage collector. Java has four main different reference types. They are: Strong Reference Weak Reference Soft Reference Phantom … Read more

Annotations in Java

In Java, an annotation is a form of syntactic metadata that can be added to Java source code. It provides data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate. Classes, methods, variables, parameters and packages are allowed to be … Read more

Constructors in Java

While not required, constructors in Java are methods recognized by the compiler to instantiate specific values for the class which may be essential to the role of the object. This topic demonstrates proper usage of Java class constructors. Default Constructor The “default” for constructors is that they do not have any arguments. In case you … Read more

Operators in Java

Operators in Java programming language are special symbols that perform specific operations on one, two, or three operands, and then return a result. The Increment/Decrement Operators (++/–) Variables can be incremented or decremented by 1 using the ++ and — operators, respectively. When the ++ and — operators follow variables, they are called post-increment and … Read more

Iterating through the contents of a Map

Maps provide methods which let you access the keys, values, or key-value pairs of the map as collections. Iterating through the contents of a Map. You can iterate through these collections. Given the following map for example: Map repMap = new HashMap<>();repMap.put(“Jon Skeet”, 927_654);repMap.put(“BalusC”, 708_826);repMap.put(“Darin Dimitrov”, 715_567); Iterating through map keys: for (String key : … Read more

Making a list unmodifiable

The list unmodifiable method is used to returns an unmodifiable view of the specified list. The Collections class provides a way to make a list unmodifiable: List ls = new ArrayList();List unmodifiableList = Collections.unmodifiableList(ls); If you want an unmodifiable list with one item you can use: List unmodifiableList = Collections.singletonList(“Only string in the list”); Moving objects around … Read more

Lists in Java with Examples

A list is an ordered collection of values. In Java, lists are part of the Java Collections Framework. Lists implement the java.util.List interface, which extends java.util.Collection. Sorting a generic list The Collections class offers two standard static methods to sort a list: sort(List list) applicable to lists where T extends Comparable, and sort(List list, Comparator … Read more

Collections in Java

The collections framework in java.util provides a number of generic classes for sets of data with functionality that can’t be provided by regular arrays. Collections framework contains interfaces for Collection, with main sub-interfaces List and Set, and mapping collection Map. Collections are the root interface and are being implemented by many other collection frameworks. Removing … Read more