小编Abh*_*ash的帖子

以下泛型代码有什么问题?

我有一个声明如下的泛型方法

public static <E extends Number> List<E> myListOfElements(List<E> list) {
        return new ArrayList<Integer>();
}
Run Code Online (Sandbox Code Playgroud)

我从中理解的是它期望一个类型的List Number作为输入扩展,List类型Number作为输出扩展.当我尝试返回一个ArrayList<Integer>r时,它会产生编译错误

Type mismatch: cannot convert from ArrayList<Integer> to List<E>
Run Code Online (Sandbox Code Playgroud)

我不明白这里有什么问题.为什么我不能回到ArrayList<Integer>这里?

java generics

6
推荐指数
0
解决办法
71
查看次数

CompletableFuture allOf 方法行为

我有以下一段java代码:

CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3);
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
            return "Result of Future 1";
        });

        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3);
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
            return "Result of Future 2";
        });

        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3);
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
            return "Result of Future 3";
        });

        boolean isdone = CompletableFuture.allOf(future1, future2, future3).isDone();

        if …
Run Code Online (Sandbox Code Playgroud)

java completable-future

6
推荐指数
1
解决办法
7891
查看次数

在 Spring Boot 中将 AWS S3 文件作为流下载

我想公开一个 API,以将 S3 存储桶文件内容作为流下载给其使用者。API URL 就像 /downloadfile/** 这是 GET 请求。

  1. 现在我的返回类型应该是什么我尝试使用 accept header= application/octet-stream 但它不起作用。
  2. 我不想将文件的内容写入任何文件并发送。它应该作为一个流返回,就是这样。

这是我写到现在的控制器伪代码,它一直给我 406 错误。

 @GetMapping(value = "/downloadfile/**", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE })
    public ResponseEntity<Object> downloadFile(HttpServletRequest request) {
       //reads the content from S3 bucket and returns a S3ObjectInputStream
       S3ObjectInputStream object = null;
       object = publishAmazonS3.getObject("12345bucket", "/logs/file1.log").getObjectContent();
       return object
    }
Run Code Online (Sandbox Code Playgroud)

这里有什么关于这样做的建议以及我做错了什么?

java amazon-s3 java-8 spring-boot

3
推荐指数
1
解决办法
6427
查看次数

在Java 8中从列表创建字符串和对象的映射

我有以下课程:

class Data {
        String systemId;
        String fileName;
        int x;
        int y;

        Data(String systemId, String fileName, int x, int y) {
            this.systemId = systemId;
            this.fileName = fileName;
            this.x = x;
            this.y = y;
        }
        public String getSystemId() {
            return systemId;
        }

        public void setSystemId(String systemId) {
            this.systemId = systemId;
        }

        public String getFileName() {
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

2
推荐指数
1
解决办法
100
查看次数

React 路由器未安装该组件

我使用 React Router 路由到不同的路由,如下所示:

     <Router>
        <Switch>
        <Route path="/teams/:teamName/matches" >
          <MatchPage/>
        </Route>  
        <Route path="/teams/:teamName" >
          <TeamPage/>
        </Route>
        </Switch>
      </Router>
Run Code Online (Sandbox Code Playgroud)

现在,在我的TeamPage组件中,我使用调用 API async,然后在渲染方法中调用另一个名为的组件MatchDetailCard

class TeamPage extends Component {
    constructor(props) {
        console.log('const called')
        super(props)
        this.state = { 
            team: [],
            teamName:null
        }
    }

    async componentDidMount() {
        console.log(this.props.match.params.teamName);
        const teamName = this.props.match.params.teamName;
        const response = await fetch(`http://localhost:8080/team/${teamName}`);
        const json = await response.json();
        console.log(json);
        this.setState({team:json, teamName: teamName});
    }

    componentDidUpdate () {
        console.log('updated')
    }

    render() {
        if (!this.state.team || !this.state.team.teamName) {
            return <h1>Team …
Run Code Online (Sandbox Code Playgroud)

reactjs react-router

1
推荐指数
1
解决办法
1296
查看次数