小编Ash*_*ish的帖子

用于包含的 Java 8 过滤器

我是 Java 8 的新手,当我尝试为所有包含一个字母的城市设置过滤器时。它对我不起作用。但是,当我使用旧方法运行它时,它会起作用。

    List<String> cityList = new ArrayList<>();
    cityList.add("Noida");
    cityList.add("Gurgaon");
    cityList.add("Denver");
    cityList.add("London");
    cityList.add("Utah");
    cityList.add("New Delhi");

    System.out.println(cityList);

/* Prior to Java 8 Approach */      
    for (String city : cityList) {
            if(city.contains("a")){
                System.out.println(city + " contains letter a");
            }
        }
/* Java 8 Approach */           
    System.out.println(Stream.of(cityList).filter(str -> str.contains("a")).collect(Collectors.toList()));
Run Code Online (Sandbox Code Playgroud)

这是输出

Noida contains letter a
Gurgaon contains letter a
Utah contains letter a
[]
Run Code Online (Sandbox Code Playgroud)

你能解释一下我在哪里犯了错误。

提前致谢 !

java lambda java-8 java-stream

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

处理 RestClientException 和 HttpClientErrorException

我通过向第 3 方进行 RESTFul 调用(Spring RestTemplate)来处理一些请求。在代码中,我试图处理以下条件。

     catch (final Exception ex) {
  if (ex instanceof HttpClientErrorException) {
      HttpClientErrorException hcee = (HttpClientErrorException)ex;
      if(hcee.getStatusCode() == NOT_FOUND) {
          throw new MyRecordNotFoundException(hcee);
      }
  }else {
      handleRestClientException(ex, Constants.MYAPP);
  }
Run Code Online (Sandbox Code Playgroud)

这是handleRestClientException的实现

    protected Exception handleRestClientException(Exception ex, String serviceName) throws Exception{
  if (ex instanceof RestClientResponseException) {
      RestClientResponseException rcre = (RestClientResponseException) ex;
      throw new RestClientResponseException(serviceName, rcre.getRawStatusCode(),
              rcre.getStatusText(), rcre.getResponseHeaders(), rcre.getResponseBodyAsByteArray(), null);
  } else {
      throw new Exception(serviceName, ex);
  }
Run Code Online (Sandbox Code Playgroud)

但所有 org.springframework.web.client.RestTemplate.getForObject(String url, Class responseType, Map urlVariables) 都会抛出 RestClientException

这是 HttpClientErrorException 的父级 …

java exception

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

标签 统计

java ×2

exception ×1

java-8 ×1

java-stream ×1

lambda ×1