如何使用 java.Set [英] How to use java.Set

查看:11
本文介绍了如何使用 java.Set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让它工作很长一段时间,但似乎无法得到它.我有用 Block 建造的对象塔.我已经使用数组让它工作了,但我想学习 Set 的.我想获得与此类似的功能:

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.");
}

如何实现?

推荐答案

首先需要学习的是java.util.Set API.

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]"

一旦您熟悉了 API,您就可以使用它来包含更多有趣的对象.如果您不熟悉 equalshashCode 合约,现在是开始的好时机.

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.

简而言之:

  • @Override 两者皆有或无;(非常重要,因为它必须满足以下属性:a.equals(b) == true --> a.hashCode() == b.hashCode()
    • 小心写boolean equals(Thing other);这不是一个正确的 @Override.
    • @Override both or none; never just one. (very important, because it must satisfied property: a.equals(b) == true --> a.hashCode() == b.hashCode()
      • Be careful with writing boolean equals(Thing other) instead; this is not a proper @Override.
      • 反身:x.equals(x).
      • 对称:x.equals(y) 当且仅当 y.equals(x)
      • 传递:if x.equals(y) &&y.equals(z),然后 x.equals(z)
      • 一致:x.equals(y) 除非对象发生变异,否则不得更改
      • x.equals(null) == false
      • reflexive: x.equals(x).
      • symmetric: x.equals(y) if and only if y.equals(x)
      • transitive: if x.equals(y) && y.equals(z), then x.equals(z)
      • consistent: x.equals(y) must not change unless the objects have mutated
      • x.equals(null) == false
      • 一致:除非发生变化,否则返回相同的数字
      • equals一致:如果x.equals(y),则x.hashCode() == y.hashCode()
        • 严格来说,对象不等式不需要哈希码不等式
        • 但哈希码不等式必然需要对象不等式

        接下来,您可能希望对对象进行排序.您可以通过使您的类型实现 Comparable 来做到这一点,或通过提供单独的比较器.

        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.

        两者都可以轻松地对对象进行排序(Arrays.sortCollections.sort(List)).它还允许您使用SortedSet,例如TreeSet.

        Having either makes it easy to sort your objects (Arrays.sort, Collections.sort(List)). It also allows you to use SortedSet, such as TreeSet.

        关于 stackoverflow 的进一步阅读:

        Further readings on stackoverflow:

        这篇关于如何使用 java.Set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆