Found 4338 Articles for Java 8

How to convert an array to Set and vice versa in Java?

Maruthi Krishna
Updated on 03-Jul-2020 08:18:11

2K+ Views

Array is a container which can hold a fix number of entities, which are of the same type. Each entity of an array is known as element and, the position of each element is indicated by an integer (starting from 0) value known as index.Exampleimport java.util.Arrays; public class ArrayExample {    public static void main(String args[]) {       Number integerArray[] = new Integer[3];       integerArray[0] = 25;       integerArray[1] = 32;       integerArray[2] = 56;       System.out.println(Arrays.toString(integerArray));    } }Output[25, 32, 56]Whereas a Set object is a collection (object) stores ... Read More

Why TreeSet Does not allow null values in Java?

Maruthi Krishna
Updated on 03-Jul-2020 08:19:19

2K+ Views

TreeSet provides an implementation of the Set interface that uses a tree for storage. Objects are stored in a sorted and ascending order.Access and retrieval times are quite fast, which makes TreeSet an excellent choice when storing large amounts of sorted information that must be found quickly.The reason is, if you look at the internal implementation of the TreeSet, it uses natural ordering, that means TreeSet uses Comparable interface by default to sort its value by comparing other value.Examplepublic class TreeSetDemo {    public static void main(String args[]) {       TreeSet treeSet = new TreeSet();       ... Read More

Is there any method to convert a Set to immutable in Java

Maruthi Krishna
Updated on 03-Jul-2020 08:20:05

1K+ Views

Whenever you need to create an object which cannot be changed after initialization you can define an immutable object. There are no specific rules to create immutable objects, the idea is to restrict the access of the fields of a class after initialization.A Set is an interface in collection framework, which does not allow duplicate values.Method to convert a set to immutableYes, Java provides a method named unmodifiableSet() in the Collections class. This method accepts a collection object as a parameter and returns an unmodifiable i.ie immutable form of it.ExampleIn the following Java program, we have created a HashSet object ... Read More

Can we add null elements to a Set in Java?

Maruthi Krishna
Updated on 07-Aug-2019 11:31:05

12K+ Views

A Set is a collection that cannot contain duplicate elements. It models the mathematical set abstraction.It does not allow duplicate elements and allow one null value at most.Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.There are three classes implementing this interface −HashSet − Set implementation based on hash table.LinkedHashSet − HashSet implementation based on linked list.TreeSet − Set implementation based on trees.Null values in a Set objectAs per the definition a set object does not allow duplicate values but it ... Read More

How to convert Date to String in Java 8?

Maruthi Krishna
Updated on 07-Aug-2019 11:27:30

520 Views

The java.time package of Java provides API’s for dates, times, instances and durations. It provides various classes like Clock, LocalDate, LocalDateTime, LocalTime, MonthDay, Year, YearMonth etc. Using classes of this package you can get details related to date and time in much simpler way compared to previous alternatives.Java.time.LocalDate − This class represents a date object without time zone in ISO-8601 calendar system. The now() method of this class obtains the current date from the system clock.The toString() method of the LocalDate class converts the date value of the current Date object in to String and returns it.ExampleFollowing Java example accepts ... Read More

How to check Leap Year in Java 8 How to get current timestamp in Java 8?

Maruthi Krishna
Updated on 07-Aug-2019 11:25:49

770 Views

The java.time package of Java provides API’s for dates, times, instances and durations. It provides various classes like Clock, LocalDate, LocalDateTime, LocalTime, MonthDay, Year, YearMonth etc. Using classes of this package you can get details related to date and time in much simpler way compared to previous alternatives.Java.time.LocalDate − This class represents a date object without time zone in ISO-8601 calendar system. The now() method of this class obtains the current date from the system clock.The isLeapYear() method of java.time.LocalDate verifies if the year in the current object is a leap year according to the ISO proleptic calendar system rules, returns ... Read More

How to represent fixed date like credit card expiry, YearMonth in java 8?

Maruthi Krishna
Updated on 07-Aug-2019 11:22:07

433 Views

The java.time package of Java provides API’s for dates, times, instances and durations. It provides various classes like Clock, LocalDate, LocalDateTime, LocalTime, MonthDay, Year, YearMonth etc. Using classes of this package you can get details related to date and time in much simpler way compared to previous alternatives.Java.time.LocalDate − This class represents a date object without time zone in ISO-8601 calendar system. The now() method of this class obtains the current date from the system clock.The of() method of java.time.LocalDate class accepts three integer parameters representing and year, a month of an year, day of a month and, returns the ... Read More

How to see if a date is before or after another date in Java 8?

Maruthi Krishna
Updated on 07-Aug-2019 11:20:25

933 Views

The java.time package of Java provides API’s for dates, times, instances and durations. It provides various classes like Clock, LocalDate, LocalDateTime, LocalTime, MonthDay, Year, YearMonth etc. Using classes of this package you can get details related to date and time in much simpler way compared to previous alternatives.Java.time.LocalDate − This class represents a date object without time zone in ISO-8601 calendar system.The now() method of this class obtains the current date from the system clock.The isAfter() method accepts an object of the class ChronoLocalDate (representing a date without time zone or, time), compares the given date with the current one ... Read More

How to use Clock in Java 8?

Maruthi Krishna
Updated on 07-Aug-2019 11:18:30

179 Views

The java.time package of Java provides API’s for dates, times, instances and durations. It provides various classes like Clock, LocalDate, LocalDateTime, LocalTime, MonthDay, Year, YearMonth etc. Using classes of this package you can get details related to date and time in much simpler way compared to previous alternatives.The Clock class of the java.time package is used to access the current instant using a time zone. Using this class you can get the current time zone, instant of the clock, current milli seconds etc.ExampleFollowing Java example demonstrates the usage of Clock class in Java.import java.time.Clock; import java.time.ZoneId; public class CurentTime { ... Read More

How to find Date after 1 week in java 8?

Maruthi Krishna
Updated on 07-Aug-2019 11:16:58

1K+ Views

The java.time package of Java provides API’s for dates, times, instances and durations. It provides various classes like Clock, LocalDate, LocalDateTime, LocalTime, MonthDay, Year, YearMonth etc. Using classes of this package you can get details related to date and time in much simpler way compared to previous alternatives.Java.time.LocalDate − This class represents a date object without time zone in ISO-8601 calendar system.The now() method of this class obtains the current date from the system clock.The plus() method of the LocalDate class accepts a long value representing the amount to add and an object of the interface TemporalAmount representing the unit ... Read More

Advertisements