9th
OCT

Java Tips

Posted by M. 4Gani under Linuxi:

Convert Map to ArrayList:
Map hashmap = new HashMap () ;
hashmap.put ( “one”,new Integer (1) ) ;
hashmap.put ( “two”,null ) ;
hashmap.put ( “three”, new String (“THREE”) ) ;

aList = new ArrayList ( hashmap.values () ) ;

System.out.println ( “The size of HashMap = “+hashmap.size () ) ;
System.out.println ( “If hashmap empty = “+hashmap.isEmpty () ) ;
System.out.println ( “The value for the \”three\” key =”+hashmap.get (“three”) ) ;
hashmap.remove ( “two” ) ;

Set set= hashmap.keySet () ;
Iterator iter = set.iterator () ;
int i=1;
while ( iter.hasNext () ) {
System.out.println ( ” “+i+” ) “+hashmap.get ( iter.next () ) ) ;
i++;
}

OR:
for (Iterator iter = hashmap.keySet().iterator(); iter.hasNext();)
{
KeyType element = (KeyType) iter.next();
ValType value = (ValType)hashmap.get(element);
}

hashmap.clear () ;

Use for-each loop through Map entry set:
Map hashmap = new HashMap () ;
for ( Map.Entry pair : hashmap )
{
System.out.println ( pair.getKey () + ” :: ” + pair.getValue () ) ;
}

Use ArrayList:
ArrayList arraylist = new ArrayList () ;

arraylist.add ( “india”,””, new String(“three”),null,”new Integer (1) ” ) ;
arraylist.add ( new Float (3.5) ) ;
arraylist.add ( 1,dummy ) ;

Add array Elements to the arraylist:
String array [] = { “foo”,””,”tow”,null,”new Integer (1) ” } ;
for ( int i=0;i < array.length;i++ )
{
arraylist.add ( i,array [i] ) ;
}

for ( int i=0;i < arraylist.size () ;i++ )
{
System.out.println ( ” “+ ( i+1 ) +” ) “+arraylist.get ( i ) ) ;
}

System.out.println ( “The size of arraylist =”+arraylist.size () ) ;
System.out.println ( “The arraylist is Empty? =”+arraylist.isEmpty () ) ;

Set the element at position one:
arraylist.set ( 1,”J2EE Programmer” ) ;

Converts arraylist to array:
Object array [] = arraylist.toArray () ;

for ( int i=0;i < array.length;i++ )
{
System.out.println ( ” “+ ( i+1 ) +” ) “+ array [ i ] ) ;
}

String value = “FOO”;
if ( “FOO”.equals(value) ) {
// do something
}

substring in Java
returnString = (xmlString.substring(xmlString.indexOf(“<ns1:queryReference>”) + 20, xmlString.indexOf(“</ns1:queryReference>”)));

 

 

See more website here...

  • Odlinks
  • Farsi-News
  • Advertise Here