Hasttable

What is the problem with this code. Can anyone please help...
new Hashtable(){
this.put("characteristic",new String("path.bandwidth.achivevable.TCP"));
};

I doubt this is recommended practice, but it does run:
import java.util.Hashtable;
public class HashTest {
     Hashtable myHashtable = new Hashtable(){
            {  // NOTE EXTRA SET OF BRACES
               this.put("characteristic",new String("path.bandwidth.achivevable.TCP"));
            } // END EXTRA SET OF BRACES
   public static void main(String[] args)
      System.out.println(new HashTest().myHashtable);
}The extra set of braces puts the "this.put(...)" into an "instance initializer" that runs when the Hashtable instance is created--so the "put" gets run, and the output of the program does show that Hashtable contains the specified data.
But, I would do as Nick suggested and do the "put" separately.

Similar Messages

  • What is HastTable String, Object ?

    Hello
    I am new to HashTables and I have a bit of code that I don't understand.
    private Hashtable<String, CalendarEvent> events = new Hashtable<String, CalendarEvent>();CalendarEvent is a class

    Not sure exactly what you're asking, but my guess is it's related to Generics. They allow you to control the types of objects that a collection can hold, or what types can be passed to a method, or various other type-related tasks. Google "java generics" for more info.
    With a HashMap, there's a key and a value. If you want the key to be a String, and the Value to be an Integer, you could say:
    HashMap<String,Integer> myMap = new HashMap<String,Integer>();Or if you want an ArrayList to hold objects of type Widget:
    ArrayList<Widget> myList = new ArrayList<Widget>();

  • Inserting Hashtable into a BLOB column..

    Hi,
    Iam trying to store a hashtable object into a BLOB column..when i try to retrieve
    from hte blob column i get the following exception
    StreamCorruptedException:out of range is 0

    I could insert into database successfully,while retrieving I got this exception.
    I could able to insert and retrieve anyother java objects except Hashtable and
    HashMap...
    also I tried putting Hasttable into a Vector..that also didn'
    t work.
    thanks in advance.
    Muthu
    public void conn(){
    ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
    ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
              try {
                   ctx = new InitialContext(ht);
                   javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup ("WorkFlowDataSource");
                   java.sql.Connection connection = ds.getConnection();
              connection.setAutoCommit(false);
              Statement statement = connection.createStatement();
              rs = statement.executeQuery("select blob_data from test for update");
              while (rs.next()) {
                        myRegularBlob = rs.getBlob("blob_data");
              OutputStream os = ((weblogic.jdbc.common.OracleBlob)myRegularBlob).getBinaryOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(os);
              Hashtable hash = new Hashtable();
              hash.put("1","111111111hgfdhgkdkvalue");
              oos.writeObject(hash);
              oos.flush();
              connection.commit();
              System.out.println("Object inserted into persistence..");
              rs.close();
              statement.close();
              } catch(Exception ex){
                   ex.printStackTrace();
              retrieve();
              public void retrieve(){
              try {
                        ctx = new InitialContext(ht);
                        javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup ("WorkFlowDataSource");
                        java.sql.Connection connection = ds.getConnection();
                        Statement statement = connection.createStatement();
                        rs = statement.executeQuery("select blob_data from test ");
              while (rs.next()) {
              myRegularBlob = rs.getBlob("blob_data");
              ObjectInputStream oos1 = new ObjectInputStream(os1);
              Hashtable sections = (Hashtable)oos1.readObject();
              System.out.println("Obj retrieved..\n"+sections);
              statement.close();
              rs.close();
              connection.close();
         } catch(Exception ex){
              ex.printStackTrace();
    "Slava Imeshev" <[email protected]> wrote:
    Muthu,
    could you show us how you write/read the hashtable?
    Regards,
    Slava Imeshev
    "Muthu" <[email protected]> wrote in message
    news:[email protected]...
    Hi,
    Iam trying to store a hashtable object into a BLOB column..when i tryto
    retrieve
    from hte blob column i get the following exception
    StreamCorruptedException:out of range is 0

  • Loading Properties in memory !

    Hi All !,
    Is there a way in which I can store all my properties from a file say(xyz.properties) into memory in the form of a java Object like a HashMap or HastTable so that I can have reference to it through out my application.I am trying do this because I want to manipulate properties couple of times in my application.Any suggestions!!
    Sample source code would be really helpful
    Thanks!
    sash

    I think it was a stupid question..anyways
    thanks
    sash

Maybe you are looking for

  • Authorization object for "set TECO" and "undo TECO"

    We want to control the authorization for "set TECO" and "undo TECO",but we can't find relevant Authorization object. Is there any Authorization object  for these two functions? If there's no Authorization object for them ,then how can we achieve the

  • Pass text to Number/Quantity field

    Hi All, I need to pass text to a number field which in turn needs to be displayed in drop down list. Is there any way I can achieve this? Kindly advise. Thanks, Vinodha.R

  • URGENT: Help to Change Ordered Items

    I'd just made an online order (Order No: W224606857) but want to change items that I ordered.  However, I can't find the order record that I made yesterday by login the Apple ID.  I just can check the order status login by order no and the email....h

  • Delete history/cookies on closing Safari

    I can't be certain, but I seem to remember there was a setting in Safari for deleting history and cookies upon closing the application automatically. I can't seem to find that setting. It appears to me that the only way to do that now is manually at

  • How do i unistall the numbers prgram?

    how do i unistall the numbers porgram?