简体   繁体   English

Java泛型列表

[英]Java Generics List

Following is an example of Generics with wild card 以下是带有通配符的泛型示例

public static void printListItems(List<object> list) {
        for (Object listItem : list)
            System.out.println(listItem);
    }

In this example we want to print list items of any type but it can't print List<Integer> , List<String> etc. because they are not subtypes of List<Object> . 在此示例中,我们希望打印任何类型的列表项,但不能打印List<Integer>List<String>等,因为它们不是List<Object>子类型。 This problem can be solved using unbounded wildcard. 使用无界通配符可以解决此问题。

public static void printListItems(List<?> list) {
    for (Object listItem : list)
        System.out.println(listItem);
}

I read this above code in Java tutorial. 我在Java教程中阅读了以上代码。 For the first example , it says it cannot work because List<String> is not sublass of List<Object> . 对于第一个示例,它说它不起作用,因为List<String>不是List<Object>

Then why it is so that in the second example the for loop is working with taking listItem as dataType of Object and iterating through List<String> elements. 那么为什么要这样,在第二个示例中,for循环正在使用listItem作为Object dataType并遍历List<String>元素进行工作。

You are running into a false contradiction because you are equating the relationship between List<String> and List<Object> to the relationship between String and Object . 您正在遇到一个错误的矛盾,因为您将List<String>List<Object>之间的关系等同于StringObject之间的关系。

In the first example, the compiler will yell at you if you attempt to call printListItem s with a List<String> , because List<String> does not extend List<Object> . 在第一个示例中,如果您尝试使用List<String>调用printListItem ,则编译器会大吼大叫,因为List<String>不扩展List<Object>

In other words: A list of strings cannot be treated as a list of objects, because (according to the type system) a list of strings is not a list of objects. 换句话说:字符串列表不能被视为对象列表,因为(根据类型系统)字符串列表不是对象列表。

In the second example, the compiler will not yell at you, because you are treating the elements of the List , which are String s, as Object s. 在第二个示例中,编译器不会对您大吼大叫,因为您将ListString元素视为Object元素。 Unlike the case with the lists, String does extend Object . 与列表不同, String确实扩展了Object

In other words: A string can be treated as an object, because (according to the type system) a string is an object. 换句话说:字符串可以被视为对象,因为(根据类型系统)字符串对象。

when the type-declaration of generic class contain wildcard character , its subclass type can be extend in two dimension. generic class类型声明包含wildcard character ,其子类类型可以在二维中扩展。


Eg Collection<?extends Number> 例如Collection <?扩展数字>

  • its subclass type can be extend in dimension Collection , for List and Set are all subclass type of Collection so List<? extends Number> and Set<? extends Number> 它的子类类型可以在维Collection中扩展,因为List and Set都是Collection子类类型,所以List<? extends Number> and Set<? extends Number> List<? extends Number> and Set<? extends Number> List<? extends Number> and Set<? extends Number> are all subclass type of Collection<? extends Number> List<? extends Number> and Set<? extends Number>都是Collection<? extends Number>所有子类类型 Collection<? extends Number> Collection<? extends Number>
  • its subclass type can be extend in dimension Number , because Integer 、Double are all subclass type of Number so Collection<Double> and Collection<Integer> are all subclass of Collection<? 它的子类类型可以在Number维度中扩展,因为Integer 、Double都是Number子类类型,因此Collection<Double> and Collection<Integer>都是Collection <?的子类 extends Number> 扩展Number>

about your second, as Andreas explained in comment , List is equal to List<? 关于您的第二个内容,正如Andreas 在评论中解释的那样,List等于List <? extend Object> and based on the second dimension extend List<String> is **subclass** of List<?> 扩展Object>并基于第二维,扩展List <String>是List <?>的**子类**

What you've done is not generic. 您所做的不是通用的。 This is a generic function 这是一个通用功能

public static <T> void printListItems(List<T> list) {
    for (T listItem : list) {
        System.out.println(listItem);
    }
}

Example: 例:

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {
    public static <T> void printListItems(List<T> list) {
        for (T listItem : list) {
            System.out.println(listItem);
        }
    }

    // arguments are passed using the text field below this editor
    public static void main(String[] args) {
        List<Integer> l = new ArrayList<>(Arrays.asList(1,2,3,4,5));
        List<String> s = new ArrayList<>(Arrays.asList("Hello", "World"));
        printListItems(l);
        printListItems(s);
    }
}

Output 产量

1
2
3
4
5
Hello
World

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM