小编whi*_*mot的帖子

Stream.collect(groupingBy(identity(),counting())然后按值对结果进行排序

我可以将一个单词列表收集到一个包中(也称为多组):

Map<String, Long> bag =
        Arrays.asList("one o'clock two o'clock three o'clock rock".split(" "))
        .stream()
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
Run Code Online (Sandbox Code Playgroud)

但是,包的条目不保证按任何特定顺序排列.例如,

{rock=1, o'clock=3, one=1, three=1, two=1}
Run Code Online (Sandbox Code Playgroud)

我可以将它们放入列表中,然后使用我的值比较器实现对它们进行排序:

ArrayList<Entry<String, Long>> list = new ArrayList<>(bag.entrySet());
Comparator<Entry<String, Long>> valueComparator = new Comparator<Entry<String, Long>>() {

    @Override
    public int compare(Entry<String, Long> e1, Entry<String, Long> e2) {
        return e2.getValue().compareTo(e1.getValue());
    }
};
Collections.sort(list, valueComparator);
Run Code Online (Sandbox Code Playgroud)

这给出了期望的结果:

[o'clock=3, rock=1, one=1, three=1, two=1]
Run Code Online (Sandbox Code Playgroud)

有没有更优雅的方式来做到这一点?我敢肯定这是很多人必须解决的问题.我可以使用Java Streams API内置的东西吗?

java java-8 java-stream

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

HTTP状态405 - 不支持请求方法"PUT"

我有以下控制器:

@RestController
public class RestaurantController {
    @Autowired
    RestaurantService restaurantService;
    @RequestMapping(value = "/restaurant/", method = RequestMethod.GET)
    public ResponseEntity<List<Restaurant>> listAllRestaurants() {
        System.out.println("Fetching all restaurants");
        List<Restaurant> restaurants = restaurantService.findAllRestaurants();
        if(restaurants.isEmpty()){
            return new ResponseEntity<List<Restaurant>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND
        }
        return new ResponseEntity<List<Restaurant>>(restaurants, HttpStatus.OK);
    }
    @RequestMapping(value = "/restaurant/{id}", method = RequestMethod.PUT)
    public ResponseEntity<Restaurant> updateRestaurant(@PathVariable("id") int id, @RequestBody Restaurant restaurant) {
        System.out.println("Updating Restaurant " + id);

        Restaurant currentRestaurant = restaurantService.findById(id);

        if (currentRestaurant==null) {
            System.out.println("Restaurant with id " + id + " not found");
            return new …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc

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

Kubernetes 中的 RBAC 规则有哪些 apiGroups 和资源?

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: xi-{{instanceId}}
  name: deployment-creation
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["batch", "extensions"]
  resources: ["jobs"]
  verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,我允许对 pod 和作业进行各种操作。对于 pod,apiGroup 为空。对于作业,apiGroup 可能是批处理或扩展。我在哪里可以找到所有可能的资源,我应该对每个资源使用哪个 apiGroup?

rbac kubernetes

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

部分自动连接Spring原型bean,运行时确定构造函数参数

javadoc ConstructorResolver.autowireConstructor(...)

如果指定了显式构造函数参数值,则还应用,将所有剩余参数与bean工厂中的bean匹配.

但我无法让它发挥作用.我得到一个BeanCreationException:

无法解析匹配的构造函数(提示:为简单参数指定索引/类型/名称参数以避免类型歧义)

在这个例子中,我有一个带有构造函数的bean,它接受Spring bean以及只在运行时知道的a String和a int.

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class BeanWithRuntimeDependencies {

    public final DependencyA dependencyA;
    public final DependencyB dependencyB;
    public final String myString;
    public final int myInt;

    public BeanWithRuntimeDependencies(
            DependencyA dependencyA, DependencyB dependencyB, 
            String myString, int myInt) {
        this.dependencyA = dependencyA;
        this.dependencyB = dependencyB;
        this.myString = myString;
        this.myInt = myInt;
    }

}

@Component
public class DependencyA { /* ... */ }

@Component
public class DependencyB { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

和我的测试: …

java spring constructor dependency-injection autowired

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

对于不同的 http 方法,对于无效 URL 的请求返回哪个状态?

当 REST 应用程序收到对不存在资源的请求时,它是否应该始终返回404 Not Found?

如果它返回一个不同的状态对于任何的HTTP方法 GETHEADPOSTPUTDELETEOPTIONSTRACE

Spring 返回一个404for GETand HEAD、一个200 OKforOPTIONS和一个405 Method Not Supportedfor 其他人。错了吗?

例如,这个 Spring Boot 应用程序显示了对错误输入的 URL 的请求的不同响应(问候语而不是问候语)。

@RestController
@SpringBootApplication
public class Application {

    private static Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

        RestTemplate restTemplate = new RestTemplate();
        String badUrl = …
Run Code Online (Sandbox Code Playgroud)

rest http spring-mvc http-status-codes http-headers

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

包含非整数计数Map <Object,BigDecimal>

我需要对象映射到他们的罪状,我会用一个袋子多层设置,但有一个BigDecimal数,而不是整数.

因此,例如,我可能会添加2.3千克糖,4.5千克盐和另外1.4千克糖.然后,如果II get糖,它将返回3.7.如果我get盐,它将返回4.5.

我可以很容易地编写一个,但可以使用现有的实现吗?什么是这个数据结构?

java collections data-structures

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

从 LocalDateTime 和 LocalTime 创建新的 LocalDateTime

给定日期时间和时间,

    LocalDateTime rightDateWrongTime = new LocalDateTime("2017-03-02T15:23:00.000");
    LocalTime rightTime = new LocalTime("17:30:00");
Run Code Online (Sandbox Code Playgroud)

我可以像这样组合它们:

    LocalDateTime rightDateRightTime = rightDateWrongTime.withTime(
        rightTime.getHourOfDay(), rightTime.getMinuteOfHour(), 
        rightTime.getSecondOfMinute(), rightTime.getMillisOfSecond());
Run Code Online (Sandbox Code Playgroud)

感觉应该有更方便的方法。像这样的东西:

    LocalDateTime rightDateRightTime = rightDateWrongTime.withTime(rightTime);
Run Code Online (Sandbox Code Playgroud)

但我找不到任何东西。是否存在类似的方法?

java jodatime

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

onchange ="javascript:updateModel()"'javascript'这个词有什么作用?

我经常看到带有onchange属性的html元素,它指定javascript作为语言,例如:

onchange="javascript:updateModel()"
Run Code Online (Sandbox Code Playgroud)

如果我删除javascript它仍然有效:

onchange="updateModel()"
Run Code Online (Sandbox Code Playgroud)

移除它是否安全?是否有一些需要它的浏览器(可能是旧版本)?

html javascript

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