Reputation: 3628
I'm trying to make it working for quite some time,but just can't seem to get it. I have object Tower built of Block's. I've already made it working using arrays, but I wanted to learn Set's. I'd like to get similar functionality to this:
public class Tower {
public Tower(){
}
public Tower add(Block k1){
//(...)
//if block already in tower, return "Block already in tower"
}
public Tower delete(Block k1){
//(...)
//if block already dleted, show "No such block in tower"
}
}
Someone gave me some code, but I constantly get errors when trying to use it :
Set<Block> tower = new HashSet<Block>();
boolean added = tower.add( k1 );
if( added ) {
System.out.println("Added 1 block.");
} else {
System.out.println("Tower already contains this block.");
}
How to implement it ?
Upvotes: 15
Views: 123889
Reputation: 1
If You have to take pair of strings separated by white space and counting unique string present in each level then following logic will help you.
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Set<String> st=new HashSet<>();
int t = s.nextInt();
String [] pair_left = new String[t];
String [] pair_right = new String[t];
for (int i = 0; i < t; i++) {
pair_left[i] = s.next();
pair_right[i] = s.next();
}
for(int i=0;i<t;i++)
{
st.add(pair_left[i]+" "+pair_right[i]);
System.out.println(st.size());
}
//Write your code here
}
}
OutPut: 3 A B A B C D 1 1 2
Upvotes: 0
Reputation: 1021
Since it is a HashSet you will need to override hashCode and equals methods. http://preciselyconcise.com/java/collections/d_set.php has an example explaining how to implement hashCode and equals methods
Upvotes: 0
Reputation: 383876
The first thing you need to study is the java.util.Set
API.
Here's a small example of how to use its methods:
Set<Integer> numbers = new TreeSet<Integer>();
numbers.add(2);
numbers.add(5);
System.out.println(numbers); // "[2, 5]"
System.out.println(numbers.contains(7)); // "false"
System.out.println(numbers.add(5)); // "false"
System.out.println(numbers.size()); // "2"
int sum = 0;
for (int n : numbers) {
sum += n;
}
System.out.println("Sum = " + sum); // "Sum = 7"
numbers.addAll(Arrays.asList(1,2,3,4,5));
System.out.println(numbers); // "[1, 2, 3, 4, 5]"
numbers.removeAll(Arrays.asList(4,5,6,7));
System.out.println(numbers); // "[1, 2, 3]"
numbers.retainAll(Arrays.asList(2,3,4,5));
System.out.println(numbers); // "[2, 3]"
Once you're familiar with the API, you can use it to contain more interesting objects. If you haven't familiarized yourself with the equals
and hashCode
contract, already, now is a good time to start.
In a nutshell:
@Override
both or none; never just one. (very important, because it must satisfied property: a.equals(b) == true --> a.hashCode() == b.hashCode()
boolean equals(Thing other)
instead; this is not a proper @Override
.x, y, z
, equals
must be:
x.equals(x)
.x.equals(y)
if and only if y.equals(x)
x.equals(y) && y.equals(z)
, then x.equals(z)
x.equals(y)
must not change unless the objects have mutatedx.equals(null) == false
hashCode
is:
equals
: if x.equals(y)
, then x.hashCode() == y.hashCode()
equals
and hashCode
.Next, you may want to impose an ordering of your objects. You can do this by making your type implements Comparable
, or by providing a separate Comparator
.
Having either makes it easy to sort your objects (Arrays.sort
, Collections.sort(List)
). It also allows you to use SortedSet
, such as TreeSet
.
Further readings on stackoverflow:
Upvotes: 56
Reputation:
It's difficult to answer this question with the information given. Nothing looks particularly wrong with how you are using HashSet.
Well, I'll hazard a guess that it's not a compilation issue and, when you say "getting errors," you mean "not getting the behavior [you] want."
I'll also go out on a limb and suggest that maybe your Block's equals an hashCode methods are not properly overridden.
Upvotes: 1
Reputation: 61536
Did you override equals and hashCode in the Block class?
EDIT:
I assumed you mean it doesn't work at runtime... did you mean that or at compile time? If compile time what is the error message? If it crashes at runtime what is the stack trace? If it compiles and runs but doesn't work right then the equals and hashCode are the likely issue.
Upvotes: 2