小编use*_*267的帖子

如何在Java 8 Stream中处理异常?

我有一个方法,我遍历List并创建List.在这样做时,我调用一个方法(createResult)来给一个Result也抛出CustomException,我将它包装为ResultClassException.但我一直收到一个错误,指出未处理的异常.

我的代码:

 private  List<Result> getResultList(List<String> results) throws ResultClassException {
    List<Result> resultList = new ArrayList<>();
        results.forEach(
                (resultName) -> {
                    if (!resultRepository.contains(resultName)) {
                       try {
                           final Result result = createResult(resultName);
                           resultList.add(result);
                       } catch (CustomException e) {
                           throw new ResultClassException("Error",e);
                       }

                    } else {
                        resultList.add(resultRepository.get(resultName));
                        log.info("Result {} already exists.", resultName);
                    }
                }
        );
        return  Collections.unmodifiableList(resultList);
    }
Run Code Online (Sandbox Code Playgroud)

有人能说出我做错了什么吗?

java exception throws java-8 java-stream

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

如何避免Java中的方法重载以避免重复?

我有2个方法,它执行80%相同的工作,但结果处理不同.我在做 :

 private <T> T getResponse(final RestURI query, final Class<T> responseClass) throws IOException {
        T response = null;

        final RestResponse<Record> tempResponse = getResponseFromDataPath(query);

        if (isResponseOK(tempResponse, query)) {
            final CustomReader reader = createCustomReaderFromResponse(tempResponse);
            response = objectMapper.readValue(reader, responseClass);
                                                          ^
            // DIFFERENCE --------------------------------|
        }

        return response;
    }

    private <T> T getResponse(final RestURI query, final TypeReference valueTypeRef) throws IOException {
        T response = null;

        final RestResponse<Record> tempResponse = getResponseFromDataPath(query);

        if (isResponseOK(tempResponse, query)) {
            final CustomReader reader = createCustomReaderFromResponse(tempResponse);
            response = objectMapper.readValue(reader, valueTypeRef);
                                                          ^
            // …
Run Code Online (Sandbox Code Playgroud)

java design-patterns coding-style

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

如何使用Java8 Stream创建Map?

如何使用java 8流更新Map?至于现在我在做:

        Map<String, Integer> testMap = Maps.newHashMap();

        for(Map.Entry<String,Integer> testEntrySet : testCounts.entrySet()) {
            String name = Utils.cleanName(testEntrySet.getKey());

            if(testMap.containsKey(name)) {
                testMap.put(name, testMap.get(name) +
                        testCounts.get(testEntrySet.getKey()));
            } else {
                testMap.put(name, testCounts.get(testEntrySet.getKey()));
            }

        }
        return testMap;
    }
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

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

设置&lt;对象&gt; vs 列表&lt;对象&gt;?

我有一个对象,其中包含一个 id 和该 id 的一些属性。

public class Data {
    private String id;
    private String name;
    private String value;
}
Run Code Online (Sandbox Code Playgroud)

目前我已在请求对象中将其定义为:

public class Request {
    private List<Data> dataList;
}
Run Code Online (Sandbox Code Playgroud)

如果我知道我可能永远不会在请求中遇到相同的 id(内容),那么 List 与 Set 是否会有任何区别。

总的来说,对于这个用例,哪个更好?列表 vs 集合?

java arrays list object set

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

如何在AngularJS中将查询字符串附加到url?

我是棱角分明的新手.我有带输入文本框的搜索页面来搜索customerId,但我想添加一个功能,我可以根据url字符串进行搜索.

例如,我的网址是:

http://localhost:8080/#/search
Run Code Online (Sandbox Code Playgroud)

但我需要类似的东西

http://localhost:8080/#/search?ACC34ff 
Run Code Online (Sandbox Code Playgroud)

http:// localhost:8080 /#/ search?CustomerId以便我可以在url中基于customerId进行搜索

有人可以告诉我如何添加查询字符串?

app.config(['$routeProvider',
function($routeProvider) {
    $routeProvider
    .when('/home', {
        templateUrl: 'partials/home.html',
        controller: 'homePageCtrl',
        reloadOnSearch: true
    }).when('/features', {
        templateUrl: 'partials/features.html',
        controller: 'featuresCtrl',
        reloadOnSearch: false
    }).when('/search', {
        templateUrl: 'partials/search.html',
        controller: 'searchCtrl',
        reloadOnSearch: false
    }).otherwise({
        redirectTo: '/home'
    });
}]);
Run Code Online (Sandbox Code Playgroud)

控制器:

appControllers.controller('searchCtrl', ['$scope','$route','$filter', '$http', '$location','$window','$timeout',
    function($scope, $route, $filter, $http, $location, $window, $timeout) {

        $scope.searchCustomerFeatures = function () {

            if($scope.customerId != null) {

            $http.get('/getCustomerFeatures.do', {params: {'customerId': $scope.customerId}}).success(function (data) {

                $scope.featuresJson = angular.toJson(data, true);
                });
            }
        }
    }]); …
Run Code Online (Sandbox Code Playgroud)

javascript url location query-string angularjs

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