In Java, i have an arrayList of ip address. how do i find the min and max ?
I have used the Collection.min() but it doesnt work given a case like :
192.168.0.1 <--min
192.168.0.250
192.168.0.9 <--max
how do i return
192.168.0.1 <--min
192.168.0.250 <--max
instead?
ArrayList is retrieve from the database. I would need to do this operation per tick (each tick is at 5 sec interval). The number of IP address would hit a max of probably 300.
IPv4 Addresses Integers "0-9", ".", and "-" can be part of an IP address range. The range of IP addresses must be between 0.0. 0.0 and 255.255. 255.255.
The maximum number of hosts can be determined from the number of hosts available in the network, It is the number obtained by subtracting two of less and broadcast address.
Using the formula 2H-2 to calculate usable IPs, we get the following: Class A = 224 – 2 = 16,777,214 total IPs. Class B = 216 – 2 = 65,534 total IPs. Class C = 28 – 2 = 254 total IPs.
/24 indicates a subnet mask of 255.255. 255.0, or in binary octets. A Subnet mask is a 32-bit number that masks an IP address, and divides the IP address into network address and host address. Subnet Mask is made by setting network bits to all "1"s and setting host bits to all "0"s.
Convert the IP address into a long integer, then sort it. 192.168.0.1
can be converted to an integer using binary arithmetic/operators:
( 192 << 24 ) + ( 168 << 16 ) + ( 0 << 8 ) + ( 1 << 0 )
and so on. Do read the comments below about using the correct data type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With