A simple question: exit the program below Java

public class abc1 { private String s; public abc1(String s){this.s=s;} public static void main(String args[]) { HashSet<Object> hs=new HashSet<Object>(); abc1 a1= new abc1("abc"); abc1 a2= new abc1("abc"); String s1= new String("abc"); String s2= new String("abc"); hs.add(a1); hs.add(a2); hs.add(s1); hs.add(s2); System.out.println(hs.size()); } } 

Why over software exit 3?

Edit

Seeing the comments below, I expand my question:

System.out.println (s1 == s2);

Are s1 and s2 a reference to the same object? If then the above statement should print true, but its output will be false.

Are they similar in terms of hashcode but are still different?

+4
source share
8 answers

There are two unequal instances of abc1 (note that it does not override equals or hashCode ) and one line in the set. Look at the four add calls:

 hs.add(a1); 

The set is initially empty - so obviously this will add value.

 hs.add(a2); 

This will also add value to the set, as they are different objects, and the default implementation of equals / hashCode is basically a reference identifier.

 hs.add(s1); 

This will add the value to the set as the row is not equal to any of the current values ​​(which are not strings).

 hs.add(s2); 

This will not add anything to the set, as the second line is equal to the first. (String overrides equals / hashCode .)

The result is a set of three elements.

+10
source

Since the set structure (note that your hash file is supported by the set) does not allow you to save two identical objects. This is how they behave.

Now you might be fooled into thinking that both a1 and a2 are equal, but if they do not override equals or hashCode , then for Java they are not equal. However, with your s1 and s2 strings, they are really equal, because the String implementation already overrides the equals and hashCode methods. Try making s1.equals(s2) and you will get the result true . If you execute a1.equals(a2) , you will get false .

At the end, your hashset contains a1, a2, and s1.

You have expanded your question to answer this question ...

s1 and s2 do not belong to the same object, they are two different String objects, but both represent the same character set. Since they are not the same object, System.out.println(s1 == s2) prints false . They are equal() , but not the same object.

+2
source

Here is the problem:

 String s1= new String("abc"); String s2= new String("abc"); 

You may try:

 System.out.println (s1 == s2); 

and you will see true . A collection cannot contain the same objects, so only s2 is stored there.

+1
source

Since you add three unique objects - two abc1 objects (you will need to implement the equals and hashCode methods to determine the equality) and String (only one is added because he implemented these methods, and the data is compared in String objects).

0
source

HashSet implements the Set interface:

 public interface Set<E> extends Collection<E> 

From http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html :

A collection that does not contain duplicate elements. More formally, the sets do not contain a pair of elements e1 and e2 such that e1.equals (e2) and no more than one zero element. As can be seen from his name, this interface models a mathematical abstract abstraction.

Answer:

I believe that s1 and s2 will be considered equal, and both of them cannot be contained in Set. It is not possible to determine that the objects abc1 are "equal." So both are added.

0
source

Set interface does not allow duplicate elements. This is the reason you get 3. as a result.

 String s1= new String("abc"); String s2= new String("abc"); 

Despite the fact that the above creates two objects, but they are identical.

0
source

Considering

  abc1 a1= new abc1("abc"); abc1 a2= new abc1("abc"); 

a1! = a2 since you did not provide overridden equals() , which would indicate that a1 and a2 are the same if the string they contain is the same.

.. and ..

HashSet will contain only unique objects.

0
source

Source: https://habr.com/ru/post/1308266/


All Articles