我可以将一个单词列表收集到一个包中(也称为多组):
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内置的东西吗?
我有以下控制器:
@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) 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?
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)
和我的测试: …
当 REST 应用程序收到对不存在资源的请求时,它是否应该始终返回404 Not Found
?
如果它返回一个不同的状态对于任何的HTTP方法 GET
,HEAD
,POST
,PUT
,DELETE
,OPTIONS
或TRACE
?
Spring 返回一个404
for GET
and HEAD
、一个200 OK
forOPTIONS
和一个405 Method Not Supported
for 其他人。错了吗?
例如,这个 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) 给定日期时间和时间,
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)
但我找不到任何东西。是否存在类似的方法?
我经常看到带有onchange属性的html元素,它指定javascript作为语言,例如:
onchange="javascript:updateModel()"
Run Code Online (Sandbox Code Playgroud)
如果我删除javascript它仍然有效:
onchange="updateModel()"
Run Code Online (Sandbox Code Playgroud)
移除它是否安全?是否有一些需要它的浏览器(可能是旧版本)?
java ×5
spring ×2
spring-mvc ×2
autowired ×1
collections ×1
constructor ×1
html ×1
http ×1
http-headers ×1
java-8 ×1
java-stream ×1
javascript ×1
jodatime ×1
kubernetes ×1
rbac ×1
rest ×1