



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




























adrish.b@ardentcollaboratio
351
adrish.b@ardentcollaboratio
352
HashMap ^ while ( i.hasNext() ) {
Map.Entry me = (Map.Entry)i.next();
System.out.print("KEY: "+me.getKey());
System.out.println(",VAL: "+me.getValue());
}
}
}
adrish.b@ardentcollaboratio
354
KEY: Key, VAL: 2 KEY: 5, VAL: Value
TreeMap ^ import java.util.*; public class TreeMapSample
{
public static void main( String [] args )
{
TreeMap tm = new TreeMap();
tm.put("CKEY", "Just A String");
tm.put("BKEY", new Long(2));
tm.put("AKEY", new Long(1));
Set s = tm.entrySet();
adrish.b@ardentcollaboratio
355
Comparator ^ import java.util.*; class MyComparator implements Comparator
{
public int compare( Object a, Object b )
{
long la = ((Long)a).longValue();
long lb = ((Long)b).longValue();
long ra = la % 2;
long rb = lb % 2;
adrish.b@ardentcollaboratio
357
Comparator ^ if ( ( ra == 0 && rb == 1 ) || la < lb ) return -1;
else if ( ( rb == 0 && ra == 1 ) || la > lb )
return 1;
return 1;
}
}
adrish.b@ardentcollaboratio
358
Comparator ^ ts.add( new Long(10) ); Iterator i = ts.iterator();
while ( i.hasNext() )
{
System.out.print(i.next());
if (i.hasNext()) System.out.print(", ");
}
}
}
adrish.b@ardentcollaboratio
360
adrish.b@ardentcollaboratio
361
(^363) java.util package(part-2)-Utility Classes
adrish.b@ardentcollaboratio
By Adrish Bhattacharyay Ardent Collaborations
adrish.b@ardentcollaboratio
364
Method Description int countTokens( ) Using the current set of delimiters, the method determines the number of tokens left to be parsed and returns the result. boolean hasMoreElements( ) Returns true if one or more tokens remain in the string and returns false if there are none. boolean hasMoreTokens( ) Returns true if one or more tokens remain in the string and returns false if there are none. Object nextElement( ) Returns the next token as an Object. String nextToken( ) Returns the next token as a String. String nextToken(String delimiters) Returns the next token as a String and sets the delimiters string to that specified by delimiters.
adrish.b@ardentcollaboratio
366
Program demostrating StringTokenizer methods
import java.util.StringTokenizer; class STDemo { static String in = "name=Adrish Bhattacharyay;" + "address=Chandannagar;" + "[email protected];" + "Qualification=B.Tech"; public static void main(String args[]) { StringTokenizer st = new StringTokenizer(in, "=;"); while(st.hasMoreTokens()) { String key = st.nextToken(); String val = st.nextToken(); System.out.println(key + "\t" + val); } }}
adrish.b@ardentcollaboratio
367
name Adrish Bhattacharyay address Chandannagar email [email protected] Qualification B.Tech
Method Description void and(BitSet bitSet) ANDs the contents of the invoking BitSet object with those specified by bitSet. The result is placed into the invoking object. void andNot(BitSet bitSet) For each 1 bit in bitSet, the corresponding bit in the invoking BitSet is cleared. int cardinality( ) Returns the number of set bits in the invoking object. void clear( ) Zeros all bits. void clear(int index) Zeros the bit specified by index. void clear(int startIndex, int endIndex) Zeros the bits from startIndex to (endIndex– 1).
adrish.b@ardentcollaboratio
369
Method Description Object clone( ) Duplicates the invoking BitSet object. boolean equals(Object bitSet) Returns true if the invoking bit set is equivalent to the one passed in bitSet. Otherwise, the method returns false. boolean get(int index) Returns the current state of the bit at the specified index. BitSet get(int startIndex, int endIndex) Returns a BitSet that consists of the bits from startIndex to endIndex–1. The invoking object is not changed. boolean intersects(BitSet bitSet) Returns true if at least one pair of corresponding bits within the invoking object and bitSet are 1.
adrish.b@ardentcollaboratio
370