Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Download 1.52 Mb.
Pdf ko'rish
bet100/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   96   97   98   99   100   101   102   103   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

258.
259.

What is StringJoiner? Explain with a code sample.
Answer:
StringJoiner is a new class added by Java 8. It helps to
concatenate Strings separated by a delimiter and having a prefix
and suffix. The following code demonstrates this:
StringJoiner strJoiner = new StringJoiner(“–”,”[“,”]”);
//Line 1
strJoiner.add(“cat”); //Line 2
strJoiner.add(“dog”); //Line 3
strJoiner.add(“mouse”); //Line 4
System.out.println(strJoiner); //Line 5
This code creates a StringJoiner. It uses the colon symbol(:)
as the delimiter and square brackets ([]) as the prefix and suffix. It
then invokes the add() method with some String values. So this
code prints the following output:
[cat:dog:mouse]
Explain the differences between the Comparator.reverse
and Comparator.reverseOrder methods.
Answer:
Both the Comparator.reversed and
Comparator.reverseOrder are methods added by Java 8 to
the Comparator interface. Both help in sorting a Collection in
reverse order. However, there are some differences between the
two as follows:
Comparator.reversed is a default method while
Comparator.reverseOrder is a static method.




260.
The Comparator.reversed requires a comparator to be
present. It simply reverses that comparator. The
Comparator.reverseOrder does not require a
Comparator to be present. It directly returns a comparator that
sorts in reverse order.
The Comparator.reversed returns a Comparator that
imposes the reverse ordering of the Comparator on which it is
invoked. The Comparator.reverseOrder on the other
hand returns a Comparator that imposes the reverse of the
natural ordering of the elements in the Collection.
Identify the error in the code below and explain what

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   96   97   98   99   100   101   102   103   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling