What is the default value for HashMap in Java?

JAVA PROGRAMMERS NEEDED HERE?

  • Below is my JAVA CODE it is for a vehicle registry application, which records details of different cars. It is capable of storing different records and then displaying what is in the table. The problem is I have tried changing the code so that it stores and displays some other type of data e.g store information about users of a system but it will fail to run where could my problem be PLEASE HELP ME PREFERABLY CHANGE THE CODE AND EXPLAIN WITH CODE COMMENTS BRIEFLY THANKS ALOT!!!!!!!!!!!!!!!!!!!!!!! import java.util.HashMap; import java.util.Iterator; public class HashTableExample{ public static class Vehicle{ public String RegNumber; public String Manufacturer; public String OwnerName; public String toString(){ return "Registration: " + RegNumber + ", Manufacturer: " + Manufacturer + ", Owner: " + OwnerName; } public Vehicle(String regno, String manu, String owner){ RegNumber = regno; Manufacturer = manu; OwnerName = owner; } } public static void main(String args[]){ // here is an example of how to create a new Vehicle object Vehicle v = new Vehicle("UAE 123A", "Toyota","A. Person"); // here is how to create a new hash table and add key/value pairs HashMap hashMap = new HashMap(); hashMap.put("VehicleOne", v); // here is how to list the values stored in the hash table System.out.println("Retrieving all values"); Iterator entryIterator = hashMap.entrySet().iterator(); while( entryIterator. hasNext() ){ System.out.println("...." + entryIterator.next() ); } /* * Some other useful properties/functions: * * hashMap.contains(key) : tells you if the key is stored in the table * hashMap.size() : how many items are stored in the hash table * hashMap.get(key) : get the value associated with that key * e.g. Vehicle v2 = (Vehicle) hashMap.get("VehicleOne"); * * Example of adding a value using an integer key: * hashMap.put(new Integer(1234), v); */ } }

  • Answer:

    You fail on grasping Object. Try to understand the following line, it is like an Array Object nameLabel { fld1, fld2, fld3, fld4...}; with an array, we get the flds by number (index) with an Object we get the flds by name A Hashtable is a special data collection to store Object by key. In your code example, they have included an inner class. An inner class makes all its variables naked to the outer class. Do you grasp what a constructor is? Do you see why a constructor is different than a method? class HashtableExample { HashMap<Integer, UsersOn> hash; // main is a METHOD, and this class has no constructor // it could have a constructor, though public static main(String[] args ) { hash = new HashMap<Integer, UsersOn>(); } public static addUser( String id ) { UsersOn uo = new UsersOn( Integer.parseInt( id ) ); hash.put( uo.id, uo); } //>================= public class UsersOn { Integer id; String name; boolean status; // constuctor public UsersOn( Integer i ) { id = i; } // add other methods() } //>================= a HashMap is same as HashTable, except HashMap will not allow any duplicate values. You need to learn to make a class for Object that you want and an application with main() that will create from your new class. Make this two files in one folder until you grasp why.

Solomon O at Yahoo! Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.