public class SimpleHashtableExample
{
public static void main(String[] args)
{
//create object of Hashtable
Hashtable ht = new Hashtable();
/*
Add key value pair to Hashtable using
Object put(Object key, Object value) method of Java Hashtable class,
where key and value both are objects
and can not be null.
put method returns Object which is either the value previously tied
to the key or null if no value mapped to the key.
*/
ht.put("One", new Integer(1));
ht.put("Two", new Integer(2));
/*
Please note that put method accepts Objects. Java Primitive values CAN NOT
be added directly to Hashtable. It must be converted to corrosponding
wrapper class first.
*/
//retrieve value using Object get(Object key) method of Java Hashtable class
Object obj = ht.get("One");
System.out.println(obj);
/*
Please note that the return type of get method is Object. The value must
be casted to the original class.
*/
}
}
public class SimpleHashSetExample
{
public static void main(String[] args)
{
//create object of HashSet
HashSet hSet = new HashSet();
/*
Add an Object to HashSet using
boolean add(Object obj) method of Java HashSet class.
This method adds an element to HashSet if it is not already present in HashSet.
It returns true if the element was added to HashSet, false otherwise.
*/
hSet.add(new Integer("1"));
hSet.add(new Integer("2"));
hSet.add(new Integer("3"));
/*
Please note that add method accepts Objects. Java Primitive values CAN NOT
be added directly to HashSet. It must be converted to corrosponding
wrapper class first.
*/
System.out.println("HashSet contains.." + hSet);
}
}
public class JavaHashMapExample
{
public static void main(String[] args)
{
//create object of HashMap
HashMap hMap = new HashMap();
/*
Add key value pair to HashMap using
Object put(Object key, Object value) method of Java HashMap class,
where key and value both are objects
put method returns Object which is either the value previously tied
to the key or null if no value mapped to the key.
*/
hMap.put("One", new Integer(1));
hMap.put("Two", new Integer(2));
/*
Please note that put method accepts Objects. Java Primitive values CAN NOT
be added directly to HashMap. It must be converted to corrosponding
wrapper class first.
*/
//retrieve value using Object get(Object key) method of Java HashMap class
Object obj = hMap.get("One");
System.out.println(obj);
/*
Please note that the return type of get method is an Object. The value must
be casted to the original class.
*/
}
}
class HarmonicSeries
{
public static void main(String args[])
{
int num = Integer.parseInt(args[0]);
double result = 0.0;
while(num > 0)
{
result = result + (double) 1 / num;
num--;
}
System.out.println("Output of Harmonic Series is "+result);
}
}
public class JavaFloatCompareExample
{
public static void main(String[] args)
{
/*
To compare two float primitive values use
compare(float f1, float f2) method of Float class. This is a static method.
It returns 0 if both the values are equal, returns value less than 0 if
f1 is less than f2, and returns value grater than 0 if f2 is grater than f2.
*/
float f1 = 5.35f;
float f2 = 5.34f;
int i1 = Float.compare(f1,f2);
if(i1 > 0)
{
System.out.println("First is grater");
}
else if(i1 < 0)
{
System.out.println("Second is grater");
}
else
{
System.out.println("Both are equal");
}
/*
To compare a Float object with another Float object use
int compareTo(Float f) method.
It returns 0 if both the values are equal, returns value less than 0 if
this Float object is less than the argument, and returns value grater
than 0 if this Float object is grater than f2.
*/
Float fObj1 = new Float("5.35");
Float fObj2 = new Float("5.34");
int i2 = fObj1.compareTo(fObj2);
if(i2 > 0)
{
System.out.println("First is grater");
}
else if(i2 < 0)
{
System.out.println("Second is grater");
}
else
{
System.out.println("Both are equal");
}
}
}