Skip to main content

Confused about How does hashCode from usingwork with a HashMap in Java

I currently have this piece of code:

    Map<Site, LinkedList<Site.Hosts.Host>> map = new HashMap<Site, LinkedList<Site.Hosts.Host>>();
    
    for (PerformanceCounter element : pc) {
        Site s = new Site();
        
        s.id = Short.parseShort(element.getSite_id());
        s.name = element.getSite_name();
        s.location = element.getSite_location();

    Site.Hosts.Host h = new Site.Hosts.Host();
    h.id = Short.parseShort(element.getHost_id());

    if (!map.containsKey(s)) {
         map.put(s, new LinkedList<Site.Hosts.Host>());
         } else {
               map.get(s).add(h);
         }
}

The list of PerformanceCounters that I am reading is:

1, C-01, New York, 1001
1, C-01, New York, 1002
1, C-01, New York, 1003

Where 101 is the id, C-01 is the name, and New York is the location.

Unfortunately, my code creates 3 key values for my Map.

I'm not able to generate 1 key with 3 values in the LinkedListLinkedList.

My if(!map.containsKey(s))if( !map.containsKey(s) ) part isn't working correctly and I'm not sure how to fix this.

I currently have this piece of code:

    Map<Site, LinkedList<Site.Hosts.Host>> map = new HashMap<Site, LinkedList<Site.Hosts.Host>>();
    
    for (PerformanceCounter element : pc) {
        Site s = new Site();
        
        s.id = Short.parseShort(element.getSite_id());
        s.name = element.getSite_name();
        s.location = element.getSite_location();

    Site.Hosts.Host h = new Site.Hosts.Host();
    h.id = Short.parseShort(element.getHost_id());

    if (!map.containsKey(s)) {
         map.put(s, new LinkedList<Site.Hosts.Host>());
         } else {
               map.get(s).add(h);
         }

The list of PerformanceCounters that I am reading is:

1, C-01, New York, 1001
1, C-01, New York, 1002
1, C-01, New York, 1003

Where 101 is the id, C-01 is the name, and New York is the location.

Unfortunately, my code creates 3 key values for my Map.

I'm not able to generate 1 key with 3 values in the LinkedList.

My if(!map.containsKey(s)) part isn't working correctly and I'm not sure how to fix this.

I currently have this piece of code:

Map<Site, LinkedList<Site.Hosts.Host>> map = new HashMap<Site, LinkedList<Site.Hosts.Host>>();
    
for (PerformanceCounter element : pc) {
  Site s = new Site();
        
  s.id = Short.parseShort(element.getSite_id());
  s.name = element.getSite_name();
  s.location = element.getSite_location();

  Site.Hosts.Host h = new Site.Hosts.Host();
  h.id = Short.parseShort(element.getHost_id());

  if (!map.containsKey(s)) {
    map.put(s, new LinkedList<Site.Hosts.Host>());
  } else {
    map.get(s).add(h);
  }
}

The list of PerformanceCounters that I am reading is:

1, C-01, New York, 1001
1, C-01, New York, 1002
1, C-01, New York, 1003

Where 101 is the id, C-01 is the name, and New York is the location.

Unfortunately, my code creates 3 key values for my Map.

I'm not able to generate 1 key with 3 values in the LinkedList.

My if( !map.containsKey(s) ) part isn't working correctly and I'm not sure how to fix this.

Source Link
user1413969
  • 1.3k
  • 2
  • 14
  • 26

Confused about hashCode from using HashMap in Java

I currently have this piece of code:

    Map<Site, LinkedList<Site.Hosts.Host>> map = new HashMap<Site, LinkedList<Site.Hosts.Host>>();
    
    for (PerformanceCounter element : pc) {
        Site s = new Site();
        
        s.id = Short.parseShort(element.getSite_id());
        s.name = element.getSite_name();
        s.location = element.getSite_location();

    Site.Hosts.Host h = new Site.Hosts.Host();
    h.id = Short.parseShort(element.getHost_id());

    if (!map.containsKey(s)) {
         map.put(s, new LinkedList<Site.Hosts.Host>());
         } else {
               map.get(s).add(h);
         }

The list of PerformanceCounters that I am reading is:

1, C-01, New York, 1001
1, C-01, New York, 1002
1, C-01, New York, 1003

Where 101 is the id, C-01 is the name, and New York is the location.

Unfortunately, my code creates 3 key values for my Map.

I'm not able to generate 1 key with 3 values in the LinkedList.

My if(!map.containsKey(s)) part isn't working correctly and I'm not sure how to fix this.