What's the difference between the following two cases
before all, I defined a simple class
static class Person {
String a;
public Person(String a) {
this.a = a;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(a, person.a);
}
}
In first case, i use interner to assign the person,so person and person1 will point to the same object
Interner<Person> interner = Interners.newWeakInterner();
person = new Person("cloudgu");
Person person1 = interner.intern(person);
In second case, i assign person to person1 directly,i can get the same result like first case
person = new Person("cloudgu");
Person person1 = person;
I know MapMakerInternalMap will store the Person Object as key and a DummyValue as value,but it is different from the String Type
when I use String type,if the value of string is the same,MapMakerInternalMap will return the former object,so i can reduce memory usage
Interner<String> interner = Interners.newWeakInterner();
List<String> list = new ArrayList<>();
String s = null;
for (int i = 0; i < 40000000; i++) {
s = new String("cloudgu");
String s1 = interner.intern(s);
list.add(s1);
if (i % 100000 == 0) {
System.out.println(i);
}
}
but if I use Person type,when i use new to create a Person object,even if the value of person1 equals person2,the map will see those as two different objects and store them seperately.
in this case,i can't see the benefits of using interner
Interner<Person> interner = Interners.newWeakInterner();
List<Person> list = new ArrayList<>();
Person person = null;
for (int i = 0; i < 40000000; i++) {
person = new Person("cloudgu");
Person person1 = interner.intern(person);
list.add(person1);
if (i % 100000 == 0) {
System.out.println(i);
}
}
To conclude,I was confused about in what kind of situation will developers use the other types rather than String type
in other words,How can I achieve the same good results in Person case like String case
--
guava-...@googlegroups.com
Project site: https://github.com/google/guava
This group: http://groups.google.com/group/guava-discuss
This list is for general discussion.
To report an issue: https://github.com/google/guava/issues/new
To get help: http://stackoverflow.com/questions/ask?tags=guava
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/guava-discuss/45C6A7E7-0E04-487C-A40D-E300FF2DC02C%40tencent.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/guava-discuss/CA%2BtXMXMAjvszhEo6CWS8UOKL9iRJwXgcQiwdYBsKta%3DFNY7JXw%40mail.gmail.com.
The point Louis was making was that you need to define Person.hashCode
to meet the general contract that you are referring to.
It could be as simple as
@Override public int hashCode() {
return a == null ? 0 : a.hashCode();
}
If you don’t provide something like this, then your class violates the contract, because:
Person a = new Person("a");
Person b = new Person("b");
assert a.equals(b); // true
assert a.hashCode() == b.hashCode(); // fails
—tim
To view this discussion on the web visit https://groups.google.com/d/msgid/guava-discuss/CAFF4x5J%3DDEYJTwjQOAE%3DxGDtSsgU6nwQyJeOpUNc0tzxLOaErA%40mail.gmail.com.