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 2 of 2

Thread: HashMap : Simple Usage and Understanding

  1. #1

    Post HashMap : Simple Usage and Understanding

    Map is an object that stores key/volume pairs. Given a key, you can find its value. Keys must be unique, but values may be duplicated.

    The HashMap class uses a hash table to implementation of the map interface. This allows the execution time of basic operations, such as get() and put() to be constant.


    An example is below:

    Q. Find the numbers Divisible by an array of numbers ie div[] between a range ie from 1 to N?
    Ans. This can be solved using many ways, but we will be solving using HashMap which will help u understand the concept of HashMap in a better way..

    NOTE:
    For this u can use your command prompt, eclipse, netbeans or any IDE that supports JAVA. Using eclipse is much easier.


    CODE :
    1:  import java.util.HashMap; 
     
    2:  public class Main { 
     
    3:    public static int usingHashmap(int N,int div[]){ 
     
    4:      int count=0; 
     
    5:      HashMap<Integer, Integer> key=new HashMap<Integer, Integer>(); 
     
    6:      for(int i=1;i<N;i++){ 
     
    7:        key.put(i, i); 
     
    8:      } 
     
    9:      for(int i=0;i<div.length;i++){ 
     
    10:        for(int k=1;k<N;k++){ 
     
    11:        if(key.get(k)==null){ 
     
    12:          key.put(k, -1); 
     
    13:        } 
     
    14:          if(key.get(k)%div[i]==0){ 
     
    15:            count++; 
     
    16:            key.remove(k); 
     
    17:          } 
     
    18:        } 
     
    19:      } 
     
    20:      System.out.println(count); 
     
    21:      return count; 
     
    22:    } 
     
    23:    public static void main(String[] args) { 
     
    24:      new Main().usingHashmap(50, new int[]{3,2}); 
     
    25:    } 
     
    26:  }
    Last edited by weakprogrammer; June 25th, 2011 at 03:48 PM.
    Warm Regards,

    weakprogrammer

    Code 2 Learn


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HashMap : Simple Usage and Understanding

    Do you have a question?

    Please wrap your code in code tags. Use the # icon.
    And remove the line numbers.

Similar Threads

  1. Usage of ClassLoader.setSigners() method
    By AlexDorogensky in forum Java Theory & Questions
    Replies: 1
    Last Post: January 13th, 2011, 10:37 AM
  2. How to get a System's total CPU usage percentage using a java program
    By userj2ee in forum Java Theory & Questions
    Replies: 2
    Last Post: January 7th, 2011, 01:28 AM
  3. Eclipse is always locked [High CPU & RAM usage]
    By talha06 in forum Java IDEs
    Replies: 4
    Last Post: March 16th, 2010, 10:07 AM
  4. Help understanding this code
    By Zepx in forum Java Theory & Questions
    Replies: 2
    Last Post: October 20th, 2009, 10:18 AM
  5. HashMap usage in Java
    By neo_2010 in forum Collections and Generics
    Replies: 2
    Last Post: September 18th, 2009, 02:12 AM

Tags for this Thread