1. Overview
In this tutorial, We'll learn what are the new methods added to the Integer class in Java 8.
Below are the new methods of integer class and look at each example to understand its usage.
All methods are static and access directly with class name. All unsigned methods work for positive and negetive integer values.
public static String toUnsignedString(int i, int radix) public static String toUnsignedString(int i) public static int parseUnsignedInt(String s, int radix) throws NumberFormatException public static int parseUnsignedInt(String s) throws NumberFormatException public static int hashCode(int value) public static int compareUnsigned(int x, int y) public static long toUnsignedLong(int x) public static int divideUnsigned(int dividend, int divisor) public static int remainderUnsigned(int dividend, int divisor) public static int sum(int a, int b) public static int max(int a, int b) public static int min(int a, int b)
2. Java 8 Integer toUnsignedString()
String toUnsignedString(int i): Returns String for the given unsigned decimal value.
String toUnsignedString(int i, int radix): Returns string for the given integer with the given radix value from the arguments.
Example 1:
package com.javaprogramto.java8.integer; public class IntegerToUnsignedStringExample { public static void main(String[] args) { String unsignedString1 = Integer.toUnsignedString(100); System.out.println("Unsigned decimal string for 100 is "+unsignedString1); String unsignedString2 = Integer.toUnsignedString(-100); System.out.println("Unsigned decimal string for -100 is "+unsignedString2); String unsignedString3 = Integer.toUnsignedString(-10000000); System.out.println("Unsigned decimal string for -10000000 is "+unsignedString3); String unsignedString4 = Integer.toUnsignedString(8, 2); System.out.println("Unsigned binary string for 8 is "+unsignedString4); String unsignedString5 = Integer.toUnsignedString(15, 2); System.out.println("Unsigned binary string for 15 is "+unsignedString5); String unsignedString6 = Integer.toUnsignedString(32, 16); System.out.println("Unsigned octa string for 32 is "+unsignedString6); } }
Output:
Unsigned decimal string for 100 is 100 Unsigned decimal string for -100 is 4294967196 Unsigned decimal string for -10000000 is 4284967296 Unsigned binary string for 8 is 1000 Unsigned binary string for 15 is 1111 Unsigned octa string for 32 is 20
3. Java 8 Integer parseUnsignedInt()
parseUnsignedInt(String s) throws NumberFormatException
Retruns an integer value in decimal form for the given string.
parseUnsignedInt(String s, int radix) throws NumberFormatException
Retruns an integer value in decimal form for the given string with given radix value.
Example 2:
This method works similar to the parseInt() method but parseInt() return singed int value.
package com.javaprogramto.java8.integer; public class IntegerParseUnsignedIntExample { public static void main(String[] args) { Integer unsignedInteger1 = Integer.parseUnsignedInt("100"); System.out.println("Unsigned decimal integer for 100 is " + unsignedInteger1); Integer unsignedInteger2 = Integer.parseUnsignedInt("4294967196"); System.out.println("Unsigned decimal integer for 4294967196 is " + unsignedInteger2); Integer unsignedString4 = Integer.parseUnsignedInt("10", 2); System.out.println("Unsigned binary integer for 8 is " + unsignedString4); Integer unsignedString5 = Integer.parseUnsignedInt("111", 2); System.out.println("Unsigned binary integer for 15 is " + unsignedString5); Integer unsignedString6 = Integer.parseUnsignedInt("20", 16); System.out.println("Unsigned octa integer for 32 is " + unsignedString6); } }
Output:
Unsigned decimal integer for 100 is 100 Unsigned decimal integer for 4294967196 is -100 Unsigned binary integer for 8 is 2 Unsigned binary integer for 15 is 7 Unsigned octa integer for 32 is 32
4. Java 8 Integer hashCode()
int hashCode(int value) This method returns hashcode for the given intger value.
Internal code returns the same integer value as hashcode.
Example 3:
package com.javaprogramto.java8.integer; public class IntegerHashcodeExample { public static void main(String[] args) { Integer unsignedInteger1 = Integer.hashCode(100); System.out.println("hashcode integer for 100 is " + unsignedInteger1); Integer unsignedInteger2 = Integer.hashCode(200); System.out.println("hashcode integer for 4294967196 is " + unsignedInteger2); } }
Output:
Unsigned decimal integer for 100 is 100 Unsigned decimal integer for 4294967196 is 200
5. Java 8 Integer compareUnsigned()
int compareUnsigned(int x, int y)
Compares two int values numerically treating the values as unsigned.
Returns value 0 if x == y; -1 if x < y as unsigned values; and +1 if x > y as unsigned values
Example 4:
package com.javaprogramto.java8.integer; public class IntegerCompareUnsignedExample { public static void main(String[] args) { Integer integer1 = Integer.compareUnsigned(10, 20); System.out.println("Result of comparing two unsigned values (10, 20) : " + integer1); Integer integer2 = Integer.compareUnsigned(20, 10); System.out.println("Result of comparing two unsigned values (20, 10) : " + integer2); Integer integer3 = Integer.compareUnsigned(10, 10); System.out.println("Result of comparing two unsigned values (10, 10) : " + integer3); } }
Output:
Result of comparing two unsigned values (10, 20) : -1 Result of comparing two unsigned values (20, 10) : 1 Result of comparing two unsigned values (10, 10) : 0
6. Java 8 Integer toUnsignedLong()
long toUnsignedLong(int x) returns an unsigned long value for the given unsigned int value.
Converts the argument to a long by an unsigned conversion. In an unsigned conversion to a long, the high-order 32 bits of the long are zero and the low-order 32 bits are equal to the bits of the integer argument. Consequently, zero and positive int values are mapped to a numerically equal long value and negative int values are mapped to a long value equal to the input plus 2^32.
Example 5:
package com.javaprogramto.java8.integer; public class IntegerToUnsignedLongExample { public static void main(String[] args) { Long long1 = Integer.toUnsignedLong(10); System.out.println("unsigned long value for int 10 is " + long1); Long long2 = Integer.toUnsignedLong(4294967); System.out.println("unsigned long value for int 4294967 is " + long2); Long long3 = Integer.toUnsignedLong(100000); System.out.println("unsigned long value for int 10 is " + long3); } }
Output:
unsigned long value for int 10 is 10 unsigned long value for int 4294967 is 4294967 unsigned long value for int 10 is 100000
7. Java 8 Integer divideUnsigned() & remainderUnsigned()
int divideUnsigned(int dividend, int divisor)
Returns the unsigned quotient of dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.
Note that in two's complement arithmetic, the three other basic arithmetic operations of add, subtract, and multiply are bit-wise identical if the two operands are regarded as both being signed or both being unsigned. Therefore separate addUnsigned, etc. methods are not provided.
int remainderUnsigned(int dividend, int divisor)
Returns the unsigned remainder from dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.
Example 6:
package com.javaprogramto.java8.integer; public class IntegerReminderDivideUnsignedExample { public static void main(String[] args) { //divideUnsigned examples Integer integer1 = Integer.divideUnsigned(10, 20); System.out.println("Result of divide two unsigned values (10, 20) : " + integer1); Integer integer2 = Integer.divideUnsigned(20, 10); System.out.println("Result of divide two unsigned values (20, 10) : " + integer2); Integer integer3 = Integer.divideUnsigned(10, 10); System.out.println("Result of divide two unsigned values (10, 10) : " + integer3); //remainderUnsigned examples Integer reminder1 = Integer.remainderUnsigned(10, 20); System.out.println("Result of remainder two unsigned values (10, 20) : " + reminder1); Integer reminder2 = Integer.remainderUnsigned(20, 10); System.out.println("Result of remainder two unsigned values (20, 10) : " + reminder2); Integer reminder3 = Integer.remainderUnsigned(10, 10); System.out.println("Result of remainder two unsigned values (10, 10) : " + reminder3); } }
Output:
Result of divide two unsigned values (10, 20) : 0 Result of divide two unsigned values (20, 10) : 2 Result of divide two unsigned values (10, 10) : 1 Result of remainder two unsigned values (10, 20) : 10 Result of remainder two unsigned values (20, 10) : 0 Result of remainder two unsigned values (10, 10) : 0
8. Java 8 Integer sum()
int sum(int a, int b) Adds two integers together as per the + operator and retuns result of sum as integer.
Example 7:
package com.javaprogramto.java8.integer; public class IntegerSumExample { public static void main(String[] args) { //divideUnsigned examples Integer integer1 = Integer.sum(10, 20); System.out.println("Result of sum two int values (10, 20) : " + integer1); Integer integer2 = Integer.sum(-20, 10); System.out.println("Result of sum two int values (-20, 10) : " + integer2); Integer integer3 = Integer.sum(-10,- 10); System.out.println("Result of sum two int values (-10, -10) : " + integer3); } }
Output:
Result of sum two int values (10, 20) : 30 Result of sum two int values (-20, 10) : -10 Result of sum two int values (-10, -10) : -20
The sum method can be used with the streams.
9. Java 8 Integer max()
int max(int a, int b)
Returns the greater of two int values as if by calling Math.max.
Example 8:
package com.javaprogramto.java8.integer; import java.util.Arrays; import java.util.List; import java.util.Optional; public class IntegerMaxExample { public static void main(String[] args) { // divideUnsigned examples Integer integer1 = Integer.max(10, 20); System.out.println("Result of max two int values (10, 20) : " + integer1); Integer integer2 = Integer.max(-20, 10); System.out.println("Result of max two int values (-20, 10) : " + integer2); Integer integer3 = Integer.max(-10, -10); System.out.println("Result of max two int values (-10, -10) : " + integer3); } }
Output
Result of max two int values (10, 20) : 20 Result of max two int values (-20, 10) : 10 Result of max two int values (-10, -10) : -10
10. Java 8 Integer min()
int min(int a, int b)
Returns the smaller of two int values as if by calling Math.min.
Example 9
package com.javaprogramto.java8.integer; import java.util.Arrays; import java.util.List; import java.util.Optional; public class IntegerMinExample { public static void main(String[] args) { // divideUnsigned examples Integer integer1 = Integer.min(10, 20); System.out.println("Result of min two int values (10, 20) : " + integer1); Integer integer2 = Integer.min(-20, 10); System.out.println("Result of min two int values (-20, 10) : " + integer2); Integer integer3 = Integer.min(-10, -10); System.out.println("Result of min two int values (-10, -10) : " + integer3); } }
Output
Result of min two int values (10, 20) : 10 Result of min two int values (-20, 10) : -20 Result of min two int values (-10, -10) : -10
11. Java 8 Integer sum(), max(), min() with Stream API
All 3 methods of integer class max(), min() and max() methods can be used along with the stream api using reduce() method.
Example 10
package com.javaprogramto.java8.integer; import java.util.Arrays; import java.util.List; import java.util.Optional; public class IntegerMinMaxSumStreamsExample { public static void main(String[] args) { // Integer max with stream api List<Integer> maxIntList = Arrays.asList(40, 50, 10, 20, 30); Optional<Integer> maxOptional = maxIntList.stream().reduce(Integer::max); if (maxOptional.isPresent()) { System.out.println("max is " + maxOptional.get()); } // Integer sum with stream api List<Integer> sumIntList = Arrays.asList(10, 20, 30, 40, 50); Optional<Integer> sumOptional = sumIntList.stream().reduce(Integer::sum); if (sumOptional.isPresent()) { System.out.println("sum is " + sumOptional.get()); } // Integer min with stream api List<Integer> minIntList = Arrays.asList(40, 50, 10, 20, 30); Optional<Integer> minOptional = minIntList.stream().reduce(Integer::min); if (minOptional.isPresent()) { System.out.println("min is " + minOptional.get()); } } }
Output:
max is 50 sum is 150 min is 10
12. Conclusion
In this article, we've seen the examples on all new methods of java 8 integer class.
No comments:
Post a Comment
Please do not add any spam links in the comments section.