定义没有特定类型的集合

发布于 2024-12-04 07:26:03 字数 342 浏览 0 评论 0原文

    public static void main(String[] args) {
       Set vals = new TreeSet();
       vals.add("one");
       vals.add(1);
       vals.add("two");
       System.out.println(vals);
}

(i)-定义一个集合而不给它一个类型意味着什么?出于什么目的 它是为了什么?

(ii)-我可以以任何方式向集合添加不同类型吗?

这是一个例子-尽管它警告我,但没有编译错误。

但是,正如例外,存在运行时错误。

    public static void main(String[] args) {
       Set vals = new TreeSet();
       vals.add("one");
       vals.add(1);
       vals.add("two");
       System.out.println(vals);
}

(i)-What does it mean to define a collection without giving it a type?For what purpose
does it made for?

(ii)-Can I add different type to the collection any way?

Here's an example- There's no compilation error, though it's warning me.

But, as excepted, there's a run time error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

GRAY°灰色天空 2024-12-11 07:26:03
  1. 定义集合(或任何类型化类)而不指定类型参数称为使用原始类型。它的存在是为了向后兼容,不应在新代码中使用。如果有效地消除了泛型对类型的所有影响。
  2. 一般来说,您可以将您的集合定义为接受Object:然后它将接受所有值:Set; vals = new HashSet()。 但是这不适用于TreeSet,因为它需要其值为相互比较如果您想向同一个TreeSet添加任意类型(这通常是一些架构问题的标志,即设计味道),那么您需要实现您的自己的 比较器可以比较任意元素并将其传递给 相应的 TreeSet 构造函数。
  1. Defining a collection (or any typed class) without specifying a type argument is called using a raw type. It exists only for backwards compatibility and should not be used in new code. If effectively removes all effects of generics on the type.
  2. Generally, you could define your set to accept Object: it would then accept all values: Set<Object> vals = new HashSet<Object>(). However this won't work for TreeSet, because it needs its values to be Comparable with each other. If you want to add arbitrary types to the same TreeSet (which is usually a sign of some architecture problems, i.e. a design smell), then you'll need to implement your own Comparator that can compare arbitrary elements and pass that into the appropriate TreeSet constructor.
救赎№ 2024-12-11 07:26:03

i) 这意味着它可以有任何对象。如果你想添加任何类型。 (通常是设计问题)

ii)可以使用类型擦除。

但是,例外的是,存在运行时错误。

在编译时得到错误通常比得到运行时错误要好。 (更容易查找和修复)

i) It means it can have any object. If you want to add any type. (Generally a design problem)

ii) Using type erasure you can.

But, as excepted, there's a run time error.

Getting an error at compile time is usually better than getting a runtime error. (Easier to find and fix)

痴情换悲伤 2024-12-11 07:26:03

Java 1.4 之前还没有泛型。所有集合只能包含Object,并且在将对象从集合中取出后使用该对象时,必须对该对象进行类转换。因此,您应该这样做,

Set vals = new Treeset();
String s = (String)vals.get(0);

而不是

Set<String> vals = new Treeset<String>();
String s = vals.get(0);

放入没有共享接口或超类的不同类型的对象,这是非常糟糕的做法,因为当您将对象从集合中取出时,您将不知道如何处理该对象。

Upto Java 1.4 there were no Generics. All collections could only contain Object and you would have to classcast the object when using the object after taking it out of the collection. So you would do

Set vals = new Treeset();
String s = (String)vals.get(0);

instead of

Set<String> vals = new Treeset<String>();
String s = vals.get(0);

Putting different types of objects that have no shared interface or superclass is very bad practice since you wont know how to handle the object when you take it out of the collection.

谁许谁一生繁华 2024-12-11 07:26:03
private Set<JPanel> s1;

public void run() {
    aMethod(s1);
}

/* line 7 */ public void aMethod(Set panels) {

}

如果通过更改第 7 行来重构上面的代码,

public void aMethod(Set<Object> panels) {

它将不再编译。如果您重构该行,

public void aMethod(Set<?> panels) {

它仍然会像以前一样编译,并且作为额外的好处,您将不再有“Set is raw...”警告

private Set<JPanel> s1;

public void run() {
    aMethod(s1);
}

/* line 7 */ public void aMethod(Set panels) {

}

If you refactor the above code by changing the 7th line to

public void aMethod(Set<Object> panels) {

it will no longer compile. If you refactor that line to

public void aMethod(Set<?> panels) {

it will still compile as before, and as an extra bonus you will no longer have the "Set is raw..." warning

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文