InetAddress, InetSocketAddress and multiple IP addresses assignment to a single host

InetAddress: This address maps to a single IP address for a host name even though the host name might resolve to multiple IP addresses.

One way of creating an InetAddress:

Here the “host” argument can be either a string representation of the IP Address (as in “x.x.x.x.” for IPv4) or a host name. If it is an IP Address then an InetAddress instance is returned with that IP. In such cases, the InetAddress instance would be bound to the IP specified in the host. If that host has multiple IP addresses then this InetAddress instance would remain bound to the IP address passed in. That returned instance could be an instance of the two subclasses of InetAddress for IPv4 and IPv6.

=> This constructor, if passed an IP address would invoke the InetAddress.getByName(String host) with the IP address and there would not be any reverse lookups.

Java 8 Streams: difference between map and flatMap

Difference between map() and flatMap():
The map() operates on the individual elements of the stream and returns a value as a Stream with the results. The flatMap() operates on the individual elements of the stream and returns a value as a Stream with the results. In this they are similar.
However the map() takes in a function that returns any R (could be a Stream as well) whereas the flatMap() operation takes in a function that returns a Stream.

CRTP

Streams employ the CRTP (Curiously Recurring Template Pattern).

For instance:

 

Regular Expressions and the void in a string (zero-length matches)

While attempting to do a substitution of word characters   “[a-zA-Z0-9_]” in a string, I noticed using a “\w*” pattern lead to 2 matches:

  1. One for the string
  2. Another for the void at the end of the string

For instance: see: https://regex101.com/r/ptz8Cm/1

The explanation is at: http://www.regular-expressions.info/zerolength.html

In short, we should use “\w+” instead of “\w*” so that zero-length matches are avoided.

Note: if you make the quantifier lazy as in https://regex101.com/r/ptz8Cm/2 then you have 3 matches.