Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create HashSetPuzzle.java #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions HashSetPuzzle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package technopark;

import java.util.HashSet;
import java.util.Set;

/**
* Какое число будет выведено последней строчкой метода main()? Что будет находиться в коллекции?
* Какие вы тут видите проблемы и как их можно исправить?
* Created by eshubin on 02/03/2017.
*/
public class HashSetPuzzle {
private static final class SomeClass {
private final int id;
private final String value;

public SomeClass(int id, String value) {
this.id = id;
this.value = value;
}

public int getId() {
return id;
}

public String getValue() {
return value;
}
}

public static void main(String[] args) {
final Set<SomeClass> set = new HashSet<>();

for (int i = 0; i < 10; i++) {
set.add(new SomeClass(i, "value " + i));
}

for (int i = 0; i < 10; i++) {
final SomeClass newValue = new SomeClass(i, "new value " + i);
if (!set.contains(newValue)) {
set.add(newValue);
}
}

System.out.println(set.size());
}
}