Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: HashMap usage in Java

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Smile HashMap usage in Java

    Hi Everybody,
    I made a small sample about using HashMaps in Java ,and i hope to be useful to everyone what to know how to manipulate with it.
    /**
     * @author 	: Mohamed Saleh AbdEl-Aziz
     */
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
     
    public class HashListSample {
     
        private Map<Integer, Integer> intMap;
        private Map<String, String> strMap;
        private Map<Integer, Boolean> boolMap;
        private Map<Integer, ArrayList> arrListMap;
     
        public HashListSample() {
            intMap = new HashMap<Integer, Integer>();
            strMap = new HashMap<String, String>();
            boolMap = new HashMap<Integer, Boolean>();
            arrListMap = new HashMap<Integer, ArrayList>();
     
            fillBoolMap();
            fillIntMap();
            fillStrMap();
        }
     
        public void fillArrayList() {
            ArrayList<String> objStrArrList = null;
            ArrayList<Byte> objBytArrList = null;
     
            objStrArrList.add("Hello");
            objStrArrList.add("World");
     
            objBytArrList.add(Byte.valueOf("12"));
            objBytArrList.add(Byte.valueOf("78"));
     
            arrListMap.put(3333, objStrArrList);
            arrListMap.put(5555, objBytArrList);
     
     
        }
     
        public void fillIntMap() {
            Integer myIntValues[] = new Integer[]{45, 666, 9778, 14522, 31011, 78};
            Integer myIntKeys[] = new Integer[]{1, 2, 33, 4, 5, 66};
     
            for (int x = 0; x < myIntValues.length; x++) {
                intMap.put(myIntKeys[x], myIntValues[x]);
            }
            System.out.println("Integer Map has been filled...");
        }
     
        public void fillStrMap() {
            String myStrValues[] = new String[]{"Mohamed", "Saleh", "Java Junior Developer"};
            String myStrKeys[] = new String[]{"FN", "MD", "Job"};
     
            for (int x = 0; x < myStrValues.length; x++) {
                strMap.put(myStrKeys[x], myStrValues[x]);
            }
            System.out.println("String Map has been filled...");
        }
     
        public void fillBoolMap() {
     
            Integer myBoolKeys[] = new Integer[]{1, 11, 111, 0};
            Boolean myBoolValues[] = new Boolean[]{true, true, true, false};
     
            for (int x = 0; x < myBoolValues.length; x++) {
                boolMap.put(myBoolKeys[x], myBoolValues[x]);
            }
            System.out.println("Boolean Map has been filled...");
        }
     
        public void doSomeOperations() {
            //Remove item from Map is depend on the Key value....
     
            System.out.println();
            System.out.println("Remove item from Integer Map...");
            intMap.remove((Integer) 4);
     
            System.out.println();
            System.out.println("Remove item from Boolean Map...");
            boolMap.remove((Integer) 0);
     
            System.out.println();
            System.out.println("Remove item from String Map...");
            strMap.remove((String) "MN");
     
        }
     
        public void clearAllMap() {
            System.out.println();
            System.out.println("Clear all Maps Objects....");
            intMap.clear();
            boolMap.clear();
            strMap.clear();
        }
     
        public void viewAllInfo() {
            /**********************************************************************/
            System.out.println("Ineger Map size: " + intMap.size());
            System.out.println("Boolean Map size: " + boolMap.size());
            System.out.println("String Map size: " + strMap.size());
            System.out.println();
            /**********************************************************************/
            //Map DataStructure is getting Data Depend on the key of your Item.
            System.out.println("View Some of Integer Map Values...");
            System.out.println("Item key is {66} in Ineger Map is :" + intMap.get((Integer) 66));
            System.out.println("Item key is {4} in Ineger Map is :" + intMap.get((Integer) 4));
            System.out.println();
     
            System.out.println("View Some of Boolean Map Values...");
            System.out.println("Item key is {1} in Boolean Map is :" + boolMap.get((Integer) 1));
            System.out.println("Item key is {0} in Boolean Map is :" + boolMap.get((Integer) 0));
            System.out.println();
     
            System.out.println("View Some of String Map Values...");
            System.out.println("Item key is {FN} in String Map is :" + strMap.get((String) "FN"));
            System.out.println("Item key is {Job} in String Map is :" + strMap.get((String) "Job"));
     
        }
     
        public static void main(String args[]) {
            HashListSample cls = new HashListSample();
            cls.viewAllInfo();
            cls.doSomeOperations();
            cls.viewAllInfo();
            cls.clearAllMap();
            cls.viewAllInfo();
        }
    }
    On the way to be Master...


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: HashMap usage in Java

    is this a sample program exercise?
    i think your should post it in 'java tips and tutorials'

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,282
    My Mood
    Happy
    Thanks
    70
    Thanked 158 Times in 154 Posts

    Default Re: HashMap usage in Java

    You should write a little example where you use your own created object as a key where you override equals and hashCode

    // Json