Found 7442 Articles for Java

Traverse through a HashMap in Java

AmitDiwan
Updated on 20-Sep-2019 11:25:42

2K+ Views

To traverse through a HashMap, use Iterator. The HashMap class uses a hashtable to implement the Map interface. This allows the execution time of basic operations, such as get( ) and put( ), to remain constant even for large sets.Following is the code to traverse through a HashMap −Exampleimport java.util.*; public class Main {    public static void main(String args[]) {       HashMap hashMap = new HashMap();       hashMap.put("John", new Integer(10000));       hashMap.put("Tim", new Integer(25000));       hashMap.put("Adam", new Integer(15000));       hashMap.put("Katie", new Integer(30000));       hashMap.put("Jacob", new Integer(45000));   ... Read More

The new operator in Java

AmitDiwan
Updated on 20-Sep-2019 11:23:09

7K+ Views

The new operator is used in Java to create new objects. It can also be used to create an array object.Let us first see the steps when creating an object from a class −Declaration − A variable declaration with a variable name with an object type.Instantiation − The 'new' keyword is used to create the object.Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.Now, let us see an example −Examplepublic class Puppy {    public Puppy(String name) {       // This constructor has one parameter, name.     ... Read More

Nested Classes in Java

AmitDiwan
Updated on 20-Sep-2019 11:09:52

1K+ Views

Writing a class within another is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class. Nested classes are divided into two types −Non-static nested classes (Inner Classes) − These are the non-static members of a class.Static nested classes − These are the static members of a class.Following are the types of Nested classes in Java −Non-static nested classes (Inner Classes)Inner classes are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a ... Read More

Java Integer compareUnsigned() method

AmitDiwan
Updated on 20-Sep-2019 11:06:25

219 Views

The compareUnsigned() method compares two integer objects numerically considering the values as unsigned.The return value if 0 if both values are equal; -1 if the val1is less than val2. The return value is 1, if the val1 is more than val2.At first, set two Integer objects −int val1 = 50; int val2 = -10;Now, compare them considering the values as unsigned −System.out.println(Integer.compareUnsigned(val1, val2));Following is an example to implement the compareUnsigned() method in Java −Examplepublic class Main {    public static void main(String[] args) {       int val1 = 50;       int val2 = -10;     ... Read More

Java Integer compareTo() method

AmitDiwan
Updated on 20-Sep-2019 11:05:01

8K+ Views

The java.lang.Integer.compareTo() method compares two Integer objects numerically. This method returns the value 0 if this Integer is equal to the argument Integer, a value less than 0 if this Integer is numerically less than the argument Integer and a value greater than 0 if this Integer is numerically greater than the argument Integer.At first, set two Integer objects −Integer obj1 = new Integer("100"); Integer obj2 = new Integer("200");Now, compare those object −int res = obj1.compareTo(obj2);Following is an example to implement the compareTo() method in Java −Examplepublic class Main {    public static void main(String[] args) {       Integer obj1 ... Read More

Java Integer compare() method

Alshifa Hasnain
Updated on 06-Jan-2025 19:37:54

2K+ Views

Integer compare() Method The compare() method in the Integer class is a part of the Java interface and is used to compare two integers. It provides a way to compare two Integer objects or primitive int values and determine their relative ordering. This method is particularly useful when sorting or working with collections that involve integer values. Syntax public static int compare(int x, int y);Where − x: The first integer to be compared. y: The second integer to be compared. Return Value The compare() method returns an integer value that ... Read More

Java Integer byteValue() method

AmitDiwan
Updated on 20-Sep-2019 11:01:11

218 Views

The byteValue() method returns the value of this Integer as a byte.Following is an example to implement the byteValue() method in Java −Examplepublic class Main {    public static void main(String[] args) {       Integer val = new Integer(10);       byte res = val.byteValue();       System.out.println("Value = " + res);    } }OutputValue = 10Let us see another example −Exampleimport java.util.*; public class Main {    public static void main(String[] args) {       Byte b = new Byte("10");       byte res = b.byteValue();       System.out.println("Byte = " + b );       System.out.println("Primitive byte = "+ res);    } }OutputByte = 80 Primitive byte = 80

Java Integer bitCount() method

AmitDiwan
Updated on 20-Sep-2019 10:58:54

202 Views

The java.lang.Integer.bitCount() method returns the number of one-bits in the two's complement binary representation of the specified int value.At first, set an int value −int val = 210;Now, find the number of one-bits −Integer.bitCount(val)Following is an example to implement the bitCount() method in Java −Examplepublic class Main {    public static void main(String[] args) {       int val = 210;       System.out.println("Number = " + val);       System.out.println("Binary = " + Integer.toBinaryString(val));       // returns the number of one-bits       System.out.println("Number of one bits = " + Integer.bitCount(val));    } }OutputNumber = 210 Binary = 11010010 Number of one bits = 4

Check if a value is present in an Array in Java

AmitDiwan
Updated on 20-Sep-2019 10:57:19

829 Views

At first sort the array −int intArr[] = {55, 20, 10, 60, 12, 90, 59}; // sorting array Arrays.sort(intArr);Now, set the value to be searched in an int variable −int searchVal = 12;Check for the presence of a value in an array −int retVal = Arrays.binarySearch(intArr, searchVal); boolean res = retVal > 0 ? true : false;Following is an example to check if a value is present in an array −Exampleimport java.util.Arrays; public class Main {    public static void main(String[] args) {       // initializing unsorted int array       int intArr[] = {55, 20, 10, ... Read More

Check if a String starts with any of the given prefixes in Java

AmitDiwan
Updated on 20-Sep-2019 10:54:48

520 Views

Let’s say the string is −String str = "Malyalam";Let’s say the prefixes are in an array −String[] prefixArr = { "Ga", "Ma", "yalam" };Now, to check whether the string starts with any of the abive prefix, use the startsWith() −if (Stream.of(prefixArr)    .anyMatch(str::startsWith))    System.out.println("TRUE"); else    System.out.println("FALSE");Following is an example to check is a string starts with any of the given prefixes −Exampleimport java.util.stream.Stream; class Main {    public static void main(String[] args) {       String str = "Malyalam";       String[] prefixArr = { "Ga", "Ma", "yalam" };       if (Stream.of(prefixArr)     ... Read More

Advertisements