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

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

问题描述

我想让它工作相当一段时间,但只是似乎不能得到它。我有对象塔建造的块的。我已经使它使用数组,但我想学习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后,它包含更多有趣的对象。如果您还不熟悉 equals hashCode 合同,现在是开始的好时机。

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 both或none; (非常重要,因为它必须满足属性: a.equals(b)== true - > a.hashCode()== b.hashCode / code>


    • 改为使用 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.

      • reflexive: x.equals(x)

      • symmetric: / code> if and only if y.equals(x)

      • transitive:if x .equals(y)& y.equals(z),then 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

      • 一致:返回相同的数字,除非发生变异

      • code> equals :if x.equals(y),then x.hashCode .hashCode()


        • 严格来说,对象不等式不需要哈希码不等式

        • 但哈希码不等式必然需要对象不等式

        • consistent: return the same number unless mutation happened
        • consistent with equals: if x.equals(y), then x.hashCode() == y.hashCode()
          • strictly speaking, object inequality does not require hash code inequality
          • but hash code inequality necessarily requires object inequality

          接下来,你可能要对对象进行排序。您可以使用类型实现 Comparable ,或提供单独的 Comparator

          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.sort Collections.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:

          • Overriding equals and hashCode in Java
          • When to use Comparable vs Comparator

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

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