Using Hash maps

Hi
Just a quick one
I have two hash maps - each containing information on the location of characters in a individual string.
Is there some sort of method that allows me to compare the two maps and return the string locations where they contain the same information ?

Hi evett,
You might want to check out the method "entrySet()" in the Map class, once you get the 2 Set's, you can then use the "retainAll()" method of a Set to only keep the information that is the same in both Set's (which contains the information you want).
Good Luck,
Bud

Similar Messages

  • How to create a dicitionary using hash map.

    Hi All,
    I want to create a dictionary using hash map where in i can add new words and there meaning. I have written some code But I am not happy with this. Can you suggest me how to add the words and there meanings to the hash map.
    I am not able to add the words and there meaning to the hash map.
    for this i tried creating a dictionary class.
    // Using hashmap
    import java.util.*;
    public class Dictionary
        public static void main(String[] args)
            // Create a hash map
            HashMap hm = new HashMap();
            // put elements to the map
            hm.put("Abode: ", "Home");
            hm.put("Balance: ", "An intrument to measure or weigh");
            hm.put("Car: ", "Automobile");
            hm.put("Dinner: ","Last meal of the day generally had after evening and before sleeping");
            hm.put("Embossed: ", "Engraved kind");
            Set set = hm.entrySet();
            Iterator i = set.iterator();
            while (i.hasNext())
                Map.Entry me = (Map.Entry) i.next();
                System.out.print(me.getKey() + ": ");
                System.out.println(me.getValue());
            System.out.println();
    }This is the other Dictionary class which i created.
    public class Main_Dictionary
        public static void main(String[] args)
            Content_Dictionary cd = new Content_Dictionary();
            cd.getContent_Dictionary("Abode", "Home");
            cd.displayValues();
    class Content_Dictionary
        String word, meaning;
        Content_Dictionary()
            word = "a";
            meaning = "b";
        Content_Dictionary(String x, String y)
            word = x;
            meaning = y;
        void getContent_Dictionary(String w, String m)
            word = w;
            meaning = m;
        void displayValues()
            System.out.print(word + ": ");
            System.out.println(meaning);
    }If i create an interface containing all the words and there meaning
    public interface Words_Dictionary
        String Abode = "Home";
        String Dark = "Lacking brightness";
        String Balance = "An intrument to measure or weigh";
        String Car = "Type of Automobile";
        String Dinner = "Last meal of the day";
        String Embossed  = "Engraved kind";
        String Adroit = "SkillFul";
    }and then create a another class which implements this interface, but how should i add these words and there meaning in the hashmap.

    I tried creating word document but i was unable to
    figure out how to do the operations on the word
    document and its content,
    So This is what i could come up with, i just created
    one class Dictionary , now I am able to search for
    the meaning of the word specified by me, and I am
    able to print all the words and there meanings added
    to the hashmap, but i am not able to figure out how
    to add a new word and the meaning in the hashmap and
    how to find is a word is there or not...May I suggest a slightly different approach?
    Do not create a String ==> String mapping, but a String ==> List<String> mapping. And do NOT place everything inside your main method.
    Here's a small demo:
    import java.util.ArrayList;
    import java.util.Map;
    import java.util.HashMap;
    import java.util.List;
    public class Dictionary {
        private Map< String, List<String >> dictionary;
        public Dictionary() {
            dictionary = new HashMap< String, List<String >>();
        public void addWord(String base, String meaning) {
            List<String> allMeanings = dictionary.get(base);
            if(allMeanings == null) { // if null, there is no entry mapped to 'base' yet: create a new one
                List<String> newMeanings = new ArrayList<String>();
                newMeanings.add(meaning);
                dictionary.put(base, newMeanings);
            } else {
                allMeanings.add(meaning);
        public List<String> search(String base) {
            // your code here
            return null;
        // ... other methods ...
        public String toString() {
            StringBuilder strb = new StringBuilder();
            for(Map.Entry< String, List<String >> e : dictionary.entrySet()) {
                strb.append(e.getKey());
                strb.append(" : ");
                strb.append(e.getValue());
                strb.append('\n');
            return strb.toString();
        public static void main(String[] args) {
            Dictionary dict = new Dictionary();
            dict.addWord("Abode", "Home");
            dict.addWord("Abode", "House");
            dict.addWord("Car", "Automobile");
            dict.addWord("Car", "Vehicle");
            System.out.println(dict);
    }Good luck.

  • Reterving Details using hash map

    Hi everyone,
    I have a class called 'Property rule' which i use to store details into a Hashmap. I now want to access this class from a different class so i can retrieve the details, how can this be done.
    My code to add details is:
    class PropertyRule {
    private HashMap ruleList = new HashMap();
    private String propName;
    PropertyRule(String propName) {
    this.propName = propName;
    public void addRule(String propName, String value) {
    ruleList.put(propName, value);
    public void display() {
    Iterator it = ruleList.entrySet().iterator();
    System.out.println("\nPROPERTY=" + propName);
    while (it.hasNext()) {
    System.out.println("\t" + it.next());
    }

    Hey Tre by making your class public like
    public class A{
    }HTH-
    Cheers,
    Gaurav

  • How to store  double  variable in hash map

    i need to store double variable using hash map, but i cant able to store it
    my jsp coding contains
    double et=24,j=5;
    hm.put("stm",st);
    hm.put("etm",et);
    Generated servlet error
    C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\exam\org\apache\jsp\availability_jsp.java:752:
    put(java.lang.Object,java.lang.Object) in java.util.Map cannot be applied to (java.lang.String,double)
    hm.put("stm",st);
    ^
    how to overcome this problem
    thank u in advance

    double etme;
    etme = hm.getDouble("etm");
    i'm getting this error
    C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\exam\org\apache\jsp\editdel_jsp.java:85: cannot resolve symbol
    symbol : method getDouble (java.lang.String)
    location: interface java.util.Map
    etme = hm.getDouble("etm");
    ^
    how to solve it
    plz help me

  • How to find Regular Expressions in a Hash Map

    Hi,
    I Have a hash map with some keys. The Keys are like this(Java.util.regex, Javax.swing.table, javax.swing.text, Java.util.jar, Java.text etc). Suppose if the user gives the search pattern as "text", the o/p should be javax.swing.text and java.text.. How to do it using regular Expressions

    // Sample code...
    import java.util.regex.*;
    public class TestRegex {
        public static void main(String[] args) {
            String test1 = "java.util.regex";
            String test2 = "javax.swing.text";
            String test3 = "java.util.jar";
            String test4 = "java.text";
            Pattern pat = Pattern.compile(".*text.*");
            Matcher mt1 = pat.matcher(test1);
            System.out.println("1> " +mt1.matches());
            Matcher mt2 = pat.matcher(test2);
            System.out.println("2> " +mt2.matches());
            Matcher mt3 = pat.matcher(test3);
            System.out.println("3> " +mt3.matches());
            Matcher mt4 = pat.matcher(test4);
            System.out.println("4> " +mt4.matches());
    }

  • Sorting in List Hash Map

    Hi All ,
    I i have the data in below format:
    Name     Age     Skill     Company
    Vass     21     Java     Zylog
    Samy     24     PB     HP
    Lee     18     ADF     CTS
    Reng     16     Java     Info
    I converted this data into java collections List<Hash Map> like this.
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    public class HashMapDemo {
        public static void main(String[] args) {
            // Create a hash map
            List<HashMap> list = new ArrayList<HashMap>();
            HashMap hm = new HashMap();
            hm.put("Name", new String("Vass"));
            hm.put("Age", new Integer(21));
            hm.put("Company", new String("Zylog"));
            hm.put("skill", new String("Java"));
            list.add(hm);
            HashMap hm1 = new HashMap();
            hm1.put("Name", new String("Samy"));
            hm1.put("Age", new Integer(24));
            hm1.put("Company", new String("HP"));
            hm1.put("skill", new String("PB"));
            list.add(hm1);
            HashMap hm2 = new HashMap();
            hm2.put("Name", new String("Lee"));
            hm2.put("Age", new Integer(18));
            hm2.put("Company", new String("CTS"));
            hm2.put("skill", new String("ADF"));
            list.add(hm2);
            HashMap hm3 = new HashMap();
            hm3.put("Name", new String("Reng"));
            hm3.put("Age", new Integer(16));
            hm3.put("Company", new String("Info"));
            hm3.put("skill", new String("Java"));
            list.add(hm3);
            Iterator i = list.iterator();
            while (i.hasNext()) {
                System.out.println(i.next());
    } As per data (table format) i want to sort the data in Column level
    how can i to achieve ?.
    List<HashMap> is type of collection is help to me?
    Any idea?
    Thanks,
    Vass Lee

    Check out Comparator, and use Google to find examples on how to use it.
    http://download.oracle.com/javase/6/docs/api/java/util/Comparator.html
    Still sorting a collection of hashmaps is a bit odd design though. I think you want to create a simple class with properties in stead of a HashMap, then implement Comparable on that class.

  • How to sor hash map

    Hi,
    [b] how to sort hash map contains integer objects,
    HashMap map=new HashMap();
    map.put("1",new Integer("1"));
    map.put("2",new Ineger("2")):
    map.put("3",new Ineger("3")):
    map.put(4,new Integer("4"));
    map.put("10",new Integer("10"));
    map.put("11",new Integer("11"));
    map.put("12",new Integer("12"));
    map.put("20",new Integer("20"));
    map.put(30,new Integer("30"));
    if i am using treemap
    TreeMap map1=new TreeMap(map);
    System.out.println(map1);
    answer is
    1 ,10,11,12,2,20,3,30,4
    but i am accepting the results
    1,2,3,4,10,11,12,20,30.
    what to do
    with regardsshannu sarma[/b]

    This is usual behaviour of a Map. You cannot sort a Set or Map. You can sort a List only using Collections.sort().
    If you want to use a Map which adds all elements according to the order you've added it, then use LinkedHashMap.
    And if you want to capture the keys, use Map.values() which returns an ordered Collection (and after converting it to List, you can sort it).
    Another approach is to create a wrapper object and put it in a List. Then you can use the java.util.Comparator to sort objects in a List.

  • Is it possible to insert a value in a hash map at a perticular index ?

    Hello Techies,
    I have a drop down list which is used to select a contry name. I also contains " All " option which is used to select all countries.
    I am using HashMap for this drop down list for getting corresponding value.
    Now, my requirement is the " All " should display as the first in the drop down list. Hash map didn't support bcoz it is in sorted order,
    Can any one help me with alternative solution ?
    Thanks in advance.
    Javed

    Now, my requirement is the " All " should display as the first in the drop down
    list. Hash map didn't support bcoz it is in sorted order,Rather HashMap doesn't support this because according to the first paragraph
    of its API documentation "This class makes no guarantees as to the order of the
    map; in particular, it does not guarantee that the order will remain constant over
    time".
    Is the drop down list a JComboBox? If so how are you using the hash map?
    And can't you just create an array from the hash map's keys, putting "All" at the
    start?

  • While loop in a Hash Map

    My while loop doesnt seem to work in a hash map, it works fine when I loop an array list.
    It compiles but it doesnt seem to find any employees, should I use another loop?
    {code
    public Employee find(String id)
    Employee foundEmployee = null;
    int index = 0;
    boolean found = false;
    while(index < register.size() && !found){
    Employee right = register.get(index);
    String namn = right.getName();
    if (namn.equals(id)){
    foundEmployee = right;
    found = true;
    index++;
    return foundEmployee;

A: while loop in a Hash Map

what if you looped on the key set?
  public Employee find(String id)
    Employee foundEmployee = null;
    Set<Integer> keySet = register.keySet();
    for (Integer key : keySet)
      Employee right = register.get(key);
      if (right.getName().equals(id))
        foundEmployee = right;
        break;
    return foundEmployee;
  }

what if you looped on the key set?
  public Employee find(String id)
    Employee foundEmployee = null;
    Set<Integer> keySet = register.keySet();
    for (Integer key : keySet)
      Employee right = register.get(key);
      if (right.getName().equals(id))
        foundEmployee = right;
        break;
    return foundEmployee;
  }

  • How do I use Port Mapping?

    b How do I use Port Mapping?
    (This document will assume that you are using and ABS/AEBS/AX as an internet router and have DHCP & NAT turned on.)
    Sometime you may want to offer access to a computer on your AirPort network to users on the internet, whether it be a web site, or for file sharing, or just remote access for yourself when traveling. If any of these sound like something you want to do, then you need to understand how Port Mapping works.
    b AirPort as Firewall
    Most of the time your AirPort base station will not let any traffic into your network which did not originate from your network. It will let everything out and replies to your traffic back in, but it will not let sessions initiated on the internet side of the base station in to your network. This is what is referred to as the "NAT firewall" capability of the base station and it provides effective protection for your network from the internet. What Port Mapping does is poke a hole in this wall to allow certain type(s) of traffic into the network and direct this traffic to a specific computer on the network. In the firewall world this is commonly referred to as an "inbound proxy" or "inbound translation" rule or "PAT" (Port Address Translation) in the router world.
    b The Need for Manual Addressing
    Since a Port Mapping entry in the base station configuration requires an inside private IP address to be specified, the computer to which to mapping entry applies should always have the IP address specified in the mapping entry. Thus, DHCP should not be used for a computer offering services on the internet as the Port Mapping entry will no longer work if the target computer's IP address changes. In general, an Apple base station's DHCP server will try to assign IP addresses in the 10.0.1.2 to 10.0.1.200 range. IP addresses above 10.0.1.200 can be Manually assigned to computers and other devices on the network up to 10.0.1.254. 10.0.1.255 is reserved (it is the broadcast address for the 10.0.1 subnet). To Manually set up the TCP/IP information for a Macintosh running Mac OS X, go to System Preferences -> Network and "Show" the appropriate interface (Ethernet or AirPort) and click on the TCP/IP tab. Select "Configure Manually" and enter the following information:
    IP address : 10.0.1.201 (or whatever address you decide to use)
    Subnet mask : 255.255.255.0
    Router IP : 10.0.1.1 (the AirPort base station LAN IP)
    DNS server : 10.0.1.1, or whatever DNS server IP your ISP uses
    After making these changes verify that your computer can still access the internet and local resources on the LAN before continuing.
    b Port Mapping a service
    In our example we will be hosting a web site on a computer which we have given an IP address of 10.0.1.201. Basic web sites are accessed using the HyperText Transport Protocol (HTTP) and this protocol typically uses port 80 to communicate. In order for others to see the web site, we must configure a Port Mapping entry in the base station configuration to not only allow the web browsers in, but to tell the base station what IP address the web server is using. The Port Mapping entry has three parts: Public Port, Private IP, and Private Port. In this case you would use the following values:
    Public Port : 80
    Private IP : 10.0.1.201 (this is the computer hosting the web site)
    Private Port : 80
    In order to access the web site from the internet, users must reference the base station's WAN port public IP (determined by looking at the base station configuration summary page in the AirPort Admin Utility). Since this address may change over time, you might want to use a Dynamic DNS service to simplify connecting for your users.
    Sometimes the port you wish to use may be blocked by the ISP. In this case, use a different non-standard Public Port number for the service, but keep the Private Port standard. In the above example, if the ISP was blocking port 80, you could potentially use 8080 instead, so:
    Public Port :

    Public Port : 8080
    Private IP : 10.0.1.201
    Private Port : 80
    Your users would then have to enter "http://<publicIP>:8080/" (where <publicIP> is the public IP address of the AirPort base station) to access the web site.
    b Internal Access
    It should be noted that when accessing these services from within the network you cannot reference the Public IP/Public Port, but rather you must use the Private IP/Private Port. Thus, "http://10.0.1.201:80/" in the above example.
    b Limits and Options
    There is a maximum of 20 Port Mapping entries that can be made in an Apple base station configuration. If you use an AirPort Extreme or AirPort Express base station there is an option which can be helpful in the case where you need many ports opened to a single computer. This is the "Default Host" option. When using this it is not necessary to use Port Mapping at all as all ports will be opened to the specified "Default Host". This is found in "Base Station Options". The default IP address for the "Default Host" is 10.0.1.253. You may change this IP address. The target computer must be Manually configured as specified above with the same IP address. Since all ports are now open to this computer, you should enable and configure the Mac OS X firewall on the default host computer to protect it from intruders.
    b Useful Related Links
    <a href="http://docs.info.apple.com/article.html?artnum=52002>"Designing AirPort Extreme Networks: Manuals</a>
    "Well Known" TCP and UDP Ports Used By Apple Software Products
    IANA Port Number Assignments

  • Using Google Maps Street View on My MacBook Pro

    Recently, I have been getting "Plug-In Missing" in the place of a video in a blog post. Also, when I try to use Street View in Google Maps I get a box that tells me I need to install Adobe Flash Player 10 or later to be able to use Street View. Should I try to download and install Adobe Flash Player? What could I use to be able to use Google Maps Street View and/or to play videos?

    I found my reply elsewhere - you have to uncheck "contacts" and then recheck it. Kinda idiotic, but there you have it!

  • Using iPhone Maps App abroad

    I should start by saying that I have no problems with Maps on my iPhone 3G.
    In fact I seem to have a magic iPhone judging by the posts and questions elsewhere.
    My iPhone, with data roaming turned off - wifi off - 3G off, and with only a very basic 2G network with O2 in rural Wales and with only 1 to 2 bars of signal, quickly uploads the Google maps to the highest resolution (less than 1 minute). And no, the maps were not already cached on my iPhone unless someone stole it, went the remote location, used the maps app, then sneakily returned the phone. Neither do I suffer from memory lapses, nor Alzheimer's (though my children might disagree).
    My questions are;
    1) does this (marvellous) service actually operate over a bog standard mobile network connection?
    2) will it work as well anywhere in the world with a bog standard mobile network connection?
    3) will it cost me 'roaming' minutes to use the Maps app abroad.
    Many thanks,

    Thanks for the info. I take it then that you don't need Wifi, Edge, or 3G for Maps to work which is good news, and contrary to many of the posts I have read.
    Bog standard means 'basic' thought to be a corruption of 'box standard' as in 'vanilla', 'no frills' etc. Usually used in a slightly derogatory manner, as in "my kids have ended up in a 'bog standard' school".
    Thanks again.

  • How to Count schedule lines in IDoc ORDERS05 using XSLT Mapping

    Hi Experts,
    In a Scenario where we are sending Purchase order (ORDERS05) to SAP SNC using XSLT Mapping,
    where in we need to count the no. of schedule lines against the Purchase Order line.
    As in Schedule line segment there is no such provision, so it needs to be handle in XSLT mapping to count the schedule lines.
    Can you all please guide me how to go about the same.
    Regards,
    Nitin P

    Hi Satish,
    Thank you very much for the reply,
    as there is only Quantity and other information is maintained against schedule lines how we can count the schedule line repeatation against PO line ? Is that very simple as you suggested or some other consideration also needs to be taken.
    Please clarify the same and let me also know if there are some standard documents also for the same.
    Regards,
    Nitin P

  • Is there a way to roll back ios6? I upgraded before I knew about the new mapping. I used google maps all the time and absolutely hate the new one. Just about everything I've looked for is wrong or missing.

    I hate the mapping in IOS6. Is there a way to roll back to the older OS? When I upgraded I didn't know about the mapping change. I used google maps a lot, for a variety of things. In just a few searches I've found more errors or missing landmarks than anything else. I really hate this app!

    I agree. How can I roll back to the previous OS? Developers, please post a procedure for reverting.

  • When using a personal dropbox account, is it possible to use the mapped drive without downloading all my dropbox files to my hard drive as well?

    When using a personal (not business) dropbox account on my mac book pro, is it possible to use the mapped drive only?  Currently I have dropbox mapped to my computer but it is also saving a redundant copy of the data on my hard drive.  My goal is to pull, edit and save files on the mapped drive only without having a redundant back up of all my dropbox files on my computer wasting valuable storage space.   Any advice out there?  Thanks in advance for your help!

    I don't believe that is accurate, but maybe I am missing something (I am not a Windows user).
    Looking at 2 explanation of how to map a Dropbox as a drive…
    http://lkeng.org/wp/?p=147
    https://www.youtube.com/watch?v=5d8uvseVr5A
    Both of those are linking to the 'local copy' of Dropbox. It is not a true 'network' drive as far as I can see (it still requires the local copy on the PC).
    Another video suggests using another service to mount the Dropbox, but that is not using Dropbox anymore, it is going through Otixo…
    https://www.youtube.com/watch?v=FAIpm2qmglU
    Dropbox does not provide easy to use URL's to mount as a network disk so I don't think this is possible on OS X.
    Dropbox's forum has this too…
    https://www.dropboxforum.com/hc/communities/public/questions/202133795-How-to-ma p-dropbox-as-a-network-drive-on-Mac?loca…
    You may need to examine the Windows computers carefully - if they have Dropbox installed & running then all the files are stored on the local machine in addition to the 'mapped drive'.
    It looks to me like people are 'redirecting' the local copy to appear like a remote disk just so they can address that path on other machines - each PC is being setup the same. You could create aliases on OS X, but it doesn't help move files off the machine.
    Hopefully someone else will contradict me, but I don't think this is how Dropbox works.

  • Maybe you are looking for