[Q#2490178][A#2490486] How to use java.Set
https://stackoverflow.com/q/2490178
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: Someone gave me some code, but I constantly get errors when trying to use it : How to implement it ?
Answer
https://stackoverflow.com/a/2490486
The first thing you need to study is the java.util.Set API. Here's a small example of how to use its methods: 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: 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:
APIzation
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]"
package com.stackoverflow.api;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
public class Human2490486 {
public static void useSet() {
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]"
}
}
package com.stackoverflow.api;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
/**
* How to use java.Set
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/2490486">https://stackoverflow.com/a/2490486</a>
*/
public class APIzator2490486 {
public static void useJava(Set<Integer> numbers, int sum)
throws Exception {
// "[2, 5]"
System.out.println(numbers);
// "false"
System.out.println(numbers.contains(7));
// "false"
System.out.println(numbers.add(5));
// "2"
System.out.println(numbers.size());
for (int n : numbers) {
sum += n;
}
// "Sum = 7"
System.out.println("Sum = " + sum);
numbers.addAll(Arrays.asList(1, 2, 3, 4, 5));
// "[1, 2, 3, 4, 5]"
System.out.println(numbers);
numbers.removeAll(Arrays.asList(4, 5, 6, 7));
// "[1, 2, 3]"
System.out.println(numbers);
numbers.retainAll(Arrays.asList(2, 3, 4, 5));
// "[2, 3]"
System.out.println(numbers);
}
}