148.
149.
integer which specifies the position of the element being
searched if present. Otherwise, it returns–1.
The following code
demonstrates the binarySearch() method:
String [] strArr = {“one”, “two”, “four”};
System.out.println(“Search index of one is:
”+Arrays.binarySearch(strArr, “one”));
This code prints the following output:
Search index of one is:0
Since the String one is at position
0, so the binarySearch()
method returns
0.
Which collection would you choose if do not want
duplicates and do not care about the order?
Answer:
HashSet should be used if you want to avoid duplicates and do
not care about the order. HashSet is an implementation of the Set
interface. It is unsorted and unordered.
In addition, TreeSet and
LinkedHashSet are also Set implementations. Both of these do
not allow duplicates. However, TreeSet
maintains the sorting
order while LinkedHashSet maintains the insertion order.
Explain with a code sample how you can remove the head
of a queue.
Answer:
The interface Queue has a method called poll() which is used
to remove the head of the queue. The following code
demonstrates this: