[Java] Why i can use the class IntStream even the package java.util.stream not loaded ?


  • 0

    When i used Java 8 stream API, for example, to get max of 3 int number. I tried to execute this at OJ:

    IntStream.of(1, 2, 3]).max().getAsInt();
    

    As the official document indicates, the class IntStream is from the package java.util.stream.
    Just for curiosity, i listed all loaded packages:

    Package[] ps = Package.getPackages();
    Arrays.sort(ps, (p1, p2) -> p1.getName().compareTo(p2.getName()));
    for (Package p : ps) {
      System.out.println(p.getName());
    }
    

    I got a list which i trust it is of all the loaded packages:

    com.eclipsesource.json
    com.sun.management
    java.io
    java.lang
    java.lang.annotation
    java.lang.invoke
    java.lang.management
    java.lang.ref
    java.lang.reflect
    java.net
    java.nio
    java.nio.charset
    java.nio.charset.spi
    java.nio.file
    java.security
    java.security.cert
    java.util
    java.util.concurrent
    java.util.concurrent.atomic
    java.util.concurrent.locks
    java.util.function
    java.util.jar
    java.util.regex
    java.util.zip
    sun.launcher
    sun.management
    sun.misc
    sun.net.util
    sun.net.www
    sun.net.www.protocol.file
    sun.net.www.protocol.jar
    sun.nio
    sun.nio.ch
    sun.nio.cs
    sun.reflect
    sun.reflect.annotation
    sun.reflect.generics.repository
    sun.reflect.misc
    sun.security.action
    sun.security.provider
    sun.security.util
    sun.usagetracker
    sun.util
    sun.util.locale
    

    But java.util.stream didn't appear ! So that's my question: why i can used class IntStream even the package java.util.stream is not loaded ?

    For more question, i checked the IntStream from which package:

    System.out.println(IntStream.of(0).getClass().getPackage());
    

    And i get this:

    package java.util.stream, Java Platform API Specification, version 1.8
    

    Could someone more familiar to java explain this to me ? i will appreciate it.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.