Java中的类型列表与类型ArrayList

(1) List<?> myList = new ArrayList<?>();

(2) ArrayList<?> myList = new ArrayList<?>();

我了解使用(1),可以交换List接口的实现。 似乎(1)通常在应用程序中使用,而不需要它(我自己总是使用它)。

我想知道是否有人使用(2)?

另外,这种情况实际上需要多久(并且我可以举个例子),实际上需要使用(1)而不是(2)(即,其中(2)不足以..除了编码接口最佳实践等之外)。


#1楼

如果代码是列表的“所有者”,则使用(2)。 例如,对于仅局部变量,则为true。 没有理由使用抽象类型List而不是ArrayList 。 另一个显示所有权的示例:

public class Test {

    // This object is the owner of strings, so use the concrete type.
    private final ArrayList<String> strings = new ArrayList<>();

    // This object uses the argument but doesn't own it, so use abstract type.
    public void addStrings(List<String> add) {
        strings.addAll(add);
    }

    // Here we return the list but we do not give ownership away, so use abstract type. This also allows to create optionally an unmodifiable list.
    public List<String> getStrings() {
        return Collections.unmodifiableList(strings);
    }

    // Here we create a new list and give ownership to the caller. Use concrete type.
    public ArrayList<String> getStringsCopy() {
        return new ArrayList<>(strings);
    }
}

#2楼

HashSetTreeSet的引用存储在Set类型的变量中被认为一种很好的样式

Set<String> names = new HashSet<String>();

这样,如果您决定改用TreeSet ,则只需更改一行。

另外,对集合进行操作的方法应指定Set类型的参数:

public static void print(Set<String> s)

然后, 该方法可用于所有set实现

从理论上讲,我们应该对链接列表提出相同的建议,即将LinkedList引用保存在List类型的变量中。 但是,在Java库中,List接口是ArrayListLinkedList类所共有的。 特别是,它具有用于随机访问的get和set方法,即使这些方法对于链表来说效率很低。

如果您不知道随机访问是否有效, 就无法编写高效的代码

在标准库中,这显然是一个严重的设计错误,因此,我不建议使用List接口。

要查看该错误的binarySearch程度,请查看Collections类的binarySearch方法的源代码。 该方法采用List参数,但是二进制搜索对于链接列表没有任何意义。 然后,代码笨拙地尝试发现列表是否为链接列表,然后切换到线性搜索!

Set

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值