Java 8 API's
1. STREAM (java.util.stream) :
Q1a. What is features of Stream ?
Q1b. What is features of Streams API?
- It represents sequence of objects from a source which supprts aggregate options.
Q1b. What is features of Streams API?
- Aggregate Operations : You can use it to map, reduce, find, match,filter, collect, print and convert from one datastructure to other.
- It doesn't store elements. It just conveys elements from a source (like Datastructure, array or I/O channel) through pipeline of computational operations.
- It is functional in nature. Operations performed on stream, doesn't modify its source.
- It is lazy and evaluates code only when required.
Q1c. How to generate Streams?
In Java 8, Collection interface has two methods to generate a Stream :
- stream() − Returns a sequential stream considering collection as its source.
- parallelStream() - Returns a parallel Stream considering collection as its source.
eg. :
List<String> stringList = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> filtered = stringList.stream().filter(string -> !string.isEmpty()).collect
2. BASE64 ENCODE/DECODE :
Q2a. What is Base64 Class ?- Base64 class deals with encryption.
- You can encrypt/decrypt data using provided methods.
- Class provides 3 different encoders/decoders to encrypt information at each level :
- Basic Encoding/decoding :It uses the Base64 alphabet specified by Java in RFC 4648 and RFC 2045 for encoding and decoding operations. The encoder does not add any line separator character. The decoder rejects data that contains characters outside the base64 alphabet.
- Url & Filename Encoding/decoding :It uses the Base64 alphabet specified by Java in RFC 4648 for encoding and decoding operations. The encoder does not add any line separator character. The decoder rejects data that contains characters outside the base64 alphabet.
- MIME :It uses the Base64 alphabet as specified in RFC 2045 for encoding and decoding operations. The encoded output must be represented in lines of no more than 76 characters each and uses a carriage return '\r' followed immediately by a linefeed '\n' as the line separator. No line separator is added to the end of the encoded output. All line separators or other characters not found in the base64 alphabet table are ignored in decoding operation.
- Nested Classes of Base64 :
- Base64.Decoder
- Base64.Encoder
- Base64 Methods :
- getDecoder()
- getEncoder()
- getUrlDecoder()
- getUrlEncoder()
- getMimeDecoder()
- getMimeEncoder()
- getMimeEncoder(int lineLength, byte[] lineSeparator)
- Base64.Decoder Methods :
- byte[] decode(byte[] src)
- byte[] decode(String src)
- int decode(byte[] src, byte[] dst)
- ByteBuffer decode(ByteBuffer buffer)
- InputStream wrap(InputStream is)
- Base64.Encoder Methods :
- byte[] encode(byte[] src)
- int encode(byte[] src, byte[] dst)
- String encodeToString(byte[] src)
- ByteBuffer encode(ByteBuffer buffer)
- OutputStream wrap(OutputStream os)
- Base64.Encoder withoutPadding()
{{{CODE}}}
3. StringJoiner CLASS :
Q3a. What is StringJoiner class?- It is a new final class added in java.util package.
- It is used to construct a sequence of characters separated by a delimiter.
- You can create string by passing delimiters like comma(,),hypen(-).
- Also, you can pass prefix & suffix to char sequence.
- StringJoiner Constructors:
- StringJoiner(CharSequence delimiter)
- StringJoiner(CharSequence delimiter,CharSequence prefix,CharSequence suffix)
- StringJoiner Methods:
- StringJoiner add(CharSequence newElement)
- Public StringJoiner merge(StringJoiner other)
- int length()
- StringJoiner setEmptyValue(CharSequence emptyValue)
{{{CODE}}}
Q3c. Code to add 2 StringJoiners.
{{{CODE}}}
Q3d. Code for StringJoiners methods.
{{{CODE}}}
4. DATETIME API :
Q4a. What is DateTime API?- It comes under the java.time package.
- Important classes in java.time package:
- Local : Simplified date-time API with no complexity of timezone handling.
- Zoned − Specialized date-time API to deal with various timezones.
Q4b. What are advantages of new DateTime API's ?
They are introduces to cover the foll. drawbacks of old date-time API:
- Not Thread-safe :
- java.util.Date is not thread safe, which led to cooncurrency issues.
- New DateTime API is immutable & doesn't have setter methods.
- Poor Design :
- Default date starts from 1900, month starts from 1 & day starts from 0.
- New DateTime API provides many methods for easy date,time operations.
- Difficult Timezone handling :
- Developers have to write a code for timezone issues.
- Issues are avoided in new DateTime API's.
Q4c. Sample code for Local Date-Time API.
{{{CODE}}}
Q4d. Sample code for Zoned Date-Time API.
{{{CODE}}}
Q4e. What is Chrono Units Enum ?
- java.time.temporal.ChronoUnit
- It is used in Java8 to replace integer values used in old API to represent day, month, year,etc.
No comments:
Post a Comment