Jul 07

Let’s have a look what happens in case another than the proof-value of an instance (and therefore the instance itself) is changed:

 

import java.util.Map;

import java. util.HashMap;

class Employee{

Employee(int PersNr, String department) {

this.PersNr=PersNr;

this.department=department;

}

public int PersNr;

public String department;

public static void main(String[] args) {

Map < Employee, Integer> x = new HashMap < Employee, Integer>();


Employee a = new  Employee (1, “department A”);

Employee b = new Employee(2, “department B”);

Employee  c = new  Employee (3, “department C”);


x.put(a,a. PersNr);

x.put(b, b.PersNr);

x.put(c, c.PersNr);


a.department=”new”;


System.out.println(x.get(a));

System.out.println(x.get(b));

System.out.println(x.get(c));

}

public int hashCode(){ return PersNr*10; }

public boolean equals( Employee c){

if (c.PersNr==this.PersNr){ return true; }

else{ return false; }

}

}

Here the result of System.out.println(x.get(a)); remains 1; since hashCode() and equals(Obj) implemented another value for proof and return. This means the only value we really have to take care of is the one for comparison. In case of change this value will turn to null. In case another value than the comparison value is changed the compiler does not care.

Tagged with:
Jul 04

I already explained some methods of map:

a.put(“Meier”, 12345); // Hinzufügen des Keys “Meier” mit dem Wert 12345
a.get(“Meier”); // returning the corresponding value to “Meier” -> 12345
a.size(); // map-size; here: 1
a.remove(“Meier”); // deletion of the corresponding value to “Meier”

Today I will give some hints in relation to maps. Let’s have a look on the following example:

import java.util.Map;
import java.util.HashMap;

class Employee{

Employee(int PersNr) {

this.PersNr=PersNr;

}

public int PersNr;

public static void main(String[] args) {

Map <Employee, Integer> x = new HashMap <Employee, Integer>();

Employee a = new Employee (1);

Employee b = new Employee (2);

Employee c = new Employee (3);


x.put(a,1);

x.put(b, b.PersNr);

x.put(c, c.PersNr);

a.PersNr=5;


System.out.println(x.get(a));

System.out.println(x.get(b));

System.out.println(x.get(c));

}

public int hashCode(){

return PersNr*10;

}

public boolean equals(Employee c){

if (c.PersNr==this.PersNr){

return true;

}

else{

return false;

}

}

}

So it seems everything is as already known; hashCode() and equals(Obj) are overwritten, only the subsequent change of a.PersNr is uncommon. We are confusing the JVM a little bit since the key (on whose base we are comparing) has changed. Since this key is not known to the map null will be returned.

Tagged with:
May 15

HashMap has the following abilities:

  • unsorted and unordered
  • repetition of elements not in order of insertion
  • insertion of keys (and their depending value) in dependency of HashCode
  • the better hashCode()-implementation;the better is the performance
  • should be used in case a Map is needed and the order does not matter
  • null-keys and multiple null-values are not allowed

Responsible class is  java.util.HashMap.

Definition/ initialization:

HashMap a;
a = new HashMap <String, Integer>();

HashMap <Long, Angestellter> b = new HashMap <Long, Angestellter>();

 

How to use:

a.put(“Meier”, 12345); // Adding key “Meier” with the value 12345
a.get(“Meier”); // returning the value which is connected to Meier

a.size(); // map-size; here:1

a.remove(“Meier”); // deletion of the value connected to Meier inclusive the key itself

Tagged with:
preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs