Feb 23

How to write a java program to check hashmap isEmpty or not?

[pullquote]To find map is empty or not isEmpty method is used.HashMap is empty method returns falseif there are keys and values and it returns true if they are key and values [/pullquote]

 

[code lang=”java”]public class HashMapIsEmptyDemo {

public static void main(String[] args) {

Map<Integer,String> map=new HashMap<Integer,String>();

map.put(1, "A");

map.put(2, "B");

map.put(3, "C");

System.out.println(map.isEmpty());

}

}

[/code]

Output :

false

Please watch this video tutorial for explantation on How to check whether hashmap isEmpty or not.

Feb 17

How to get all keys from Hashmap in java

[pullquote]keyset method is used to retrieve all keys from hashmap.
[/pullquote]

[code lang=”java”]public class HashMapGetkeysDemo {

public static void main(String[] args) {

//creating hashmap
Map<Integer,String> map=new HashMap<Integer,String>();

//Adding elements to map
map.put(1, "A");
map.put(3,"B");
map.put(4,"C");

System.out.println(map);

//Getting keyset object
Set<Integer> keySet=map.keySet();

//Iterating through keyset
for (Integer integer : keySet) {
System.out.println(integer);
}

}

}
[/code]

Output:

{1=A, 3=B, 4=C}
1
3
4

Here you can check the video for above program explanation.

Feb 09

Java interview questions and answers-part57

281) To what values primitives in java get automatically initialized?

Type Default Value
boolean False
byte 0
char u000
short 0
int 0
long 0L
float 0.0f
double 0.0d

282) To which value Strings get automatically initialized to?

As Strings are not primitives,Strings get automatically intialized to null value

 

283) Which package gets automatically imported by default?

java.lang package get automatically imported by default. java.lang package are fundamental for java programming language such as classes and interfaces.java.lang package contains some important classes such as Object,String,Thread,interfaces etc.

 

284) Which non unicode letters can be used as first letters for naming identifiers?

The non unicode letters _ and $ can be used to define first character of identifier.

285) Which characters can be used for naming identifiers as second letter but not as first letter?

Digits 0 through 9 cannot be used for naming as first character of identifier but they can be used to name second character in identifier.

 286) Is null a keyword in java?

null is a literal but not keyword in java.