简体   繁体   中英

What is the difference between List<Object[]> and List<Object>?

When a method is taking List of Object[] as input, even at the run-time the actual input is List of Object, it is still okay for the method. Question is, for generics in Java, what does List<something> really mean and what is the difference between List<Object[]> and List<Object> ?


/*
 * if name is "A", then the input is a List<Object[]>
 * if name is "B", then the input actually is a List<Object>
 */
public List<String> convert(List<Object[]> objectList, String name) {

    List<String> resultList = new ArrayList<String>();

    if (StringUtils.equals(name, "A")) {
        for (Object[] object : objectList) {
            String code = StringUtils.join((String) object[0], (String) object[1]);
            resultList.add(code);
        }
        return resultList;
    } 

    else if(StringUtils.equals(name, "B")) {
        for (Object object : objectList) {
            String code = (String) object;
            resultList.add(code);
        }
        return resultList;
    }
}

Actually during the run-time, this method works fine and takes both List<Object[]> and List<Object> .

Just wondering anyone could give some explanation of how this could work.

So to answer in order:

  1. For generics in Java, what does List<something> really mean? The <> bracket notation allows you to specify what specific type of a generic class you want. Basically, a List can be a List<A> , List<B> , etc.: its contents can be anything. Writing List<Q> says that it is a List which contains things of type Q .

  2. What is the difference between List<Object[]> and List<Object> ? [] is the array operator. A List<Object[]> is a list containing arrays of Object . A List<Object> is a list containing Object s. In Java, almost everything is an Object , including arrays!

There's another thing to note here: Java generics are complicated (and interesting), and there's a thing called type erasure which means that, at run time, List<Object> and List<XYZ> both become List - no specific type! The things in the brackets are only used at compile time - basically, to check that what you've written is correct. See What does angle brackets T mean in Java and Java generics type erasure when and what happens


Why does your code run? Well, in the "A" case it's straightforward. At runtime, the parameter is just a List , but your code expects to find Object[] s in that list, and it does. In the "B" case it's more complicated: the parameter is just a List , and you iterate over its contents treating them as Object . This is fine because as we said above (almost) everything is an Object . You then take those objects and cast them to String - this is only weird if we don't take into account that at runtime the input List is untyped. As far as Java is concerned, there could be anything in that list, and you tell it that there are String s. Because the input was in fact a list of String , it all works out.

The interesting bit (which you didn't include in your code) is how you're calling a method that takes List<Object[]> with a List<String> without Java complaining at compile time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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