java 中的 Collections.checkedList() 调用是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1161176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:26:14  来源:igfitidea点击:

What is the Collections.checkedList() call for in java?

javagenericscollections

提问by Jay R.

I just want to know for what java.util.Collections.checkedList()is actually used.

我只想知道java.util.Collections.checkedList()实际使用了什么。

I have some code that I know is returning me a List<String>but it's being passed through a chain of messaging calls and returned to me as a java.io.Serializable. Is that checkedList call good for me to turn my Serializableinto a List<String>? I know I can cast it to a java.util.List, but I'd rather not have to check each element and I'm not comfortable with assuming each element is a String.

我有一些我知道正在返回给我的代码,List<String>但它正在通过一系列消息传递调用并作为java.io.Serializable. 那个checkedList 调用对我把mySerializable变成a有好处List<String>吗?我知道我可以将它转换为 a java.util.List,但我宁愿不必检查每个元素,而且我不喜欢假设每个元素都是String.

回答by Yishai

It is used in part as a debugging tool to find where code inserts a class of the wrong type, in case you see that happening, but can't figure out where.

它部分用作调试工具来查找代码在哪里插入错误类型的类,以防您看到这种情况发生,但无法弄清楚在哪里。

You could use it as part of a public API that provides a collection and you want to ensure the collection doesn't get anything in it of the wrong type (if for example the client erases the generics).

您可以将它用作提供集合的公共 API 的一部分,并且您希望确保集合中不会包含任何错误类型的内容(例如,如果客户端擦除了泛型)。

The way you could use it in your case is:

你可以在你的情况下使用它的方式是:

 Collections.checkedList(
      new ArrayList<String>(uncertainList.size()), String.class)
      .addAll(uncertainList);

If that doesn't throw an exception, then you know you are good. That isn't exactly a performance optimized piece of code, but if the list contents are reasonably small, it should be fine.

如果那没有抛出异常,那么你就知道你很好。这并不完全是一段性能优化的代码,但如果列表内容相当小,应该没问题。

回答by daveb

Not quite:

不完全的:

Collections.checkedListwill only decorate the list to prevent any future inserts with objects of the wrong class, it won't check all the elements that are already in the list.

Collections.checkedList只会修饰列表以防止将来插入错误类的对象,它不会检查列表中已经存在的所有元素。

However, you could make a new checkedList, and then call addAlland pass in the list you are unsure about - rather than writing the loop yourself.

但是,您可以创建一个新的checkedList,然后调用addAll并传入您不确定的列表 - 而不是自己编写循环。

回答by Ben Lings

A discussion of what checkedListcould be used for is available in the documentation for checkedCollection. The reasons given are:

checkedListcheckedCollection的文档中可以找到可以用于什么的讨论。给出的理由是:

  • as a debugging aid (if someone has used an unchecked cast)
  • to ensure safety when passing a collection to be populated by third-party code.
  • 作为调试帮助(如果有人使用了未经检查的强制转换)
  • 以确保传递要由第三方代码填充的集合时的安全性。

You could use the following from google collectionsto check that the list does only contain strings:

您可以使用google 集合中的以下内容来检查列表是否仅包含字符串:

Iterables.all(list, Predicates.instanceOf(String.class))