Java Hashmap: How to get key from value?

If I have the value "foo" , and a HashMap<String> ftw for which ftw.containsValue("foo") returns true , how can I get the corresponding key? Do I have to loop through the hashmap? What is the best way to do that? If you choose to use the Commons Collections library instead of the standard Java Collections API, you can achieve this with ease. The BidiMap interface i

Java Hashmap:如何从价值中获取密钥?

如果我的值为"foo" ,并且ftw.containsValue("foo")返回true的HashMap<String> ftw ,我怎样才能获得相应的键? 我必须循环散列图吗? 什么是最好的方式来做到这一点? 如果您选择使用Commons Collections库而不是标准Java Collections API,则可以轻松实现此目的。 集合库中的BidiMap接口是双向映射,允许您将键映射到值(如法线贴图),还可以将值映射到键,从而允许您在两个方向上执行查找。

Swagger doc for api response (type List)

I am using swagger to generate the documentation of my REST API. However I am having problems specifying the responses of some API calls. This is my code: @GET @Path("/self/activities") @Produces(MediaType.APPLICATION_JSON) @ApiOperation(value = "Get all activities created by this user", notes = "Returns the list that the authenticated user (JWT) has created", respon

用于api响应的Swagger doc(类型列表)

我正在使用swagger来生成我的REST API的文档。 不过,我在指定某些API调用的响应时遇到问题。 这是我的代码: @GET @Path("/self/activities") @Produces(MediaType.APPLICATION_JSON) @ApiOperation(value = "Get all activities created by this user", notes = "Returns the list that the authenticated user (JWT) has created", response = Activity.class, responseContainer =

Running compiled java code at runtime

I want to run code compiled before. I compiled anyway it is not important how to compile but running the code is problem. My code.java public class code{ public static void main(String[] args) { System.out.println("Hello, World"); } } Then I compiled this code and code.class (in the D:// directory) was generated. Now I want to run this compiled file. My code is : import j

在运行时运行编译的Java代码

我想运行之前编译的代码。 无论如何,我编译并不重要,如何编译但运行代码是问题。 我的code.java public class code{ public static void main(String[] args) { System.out.println("Hello, World"); } } 然后我编译了这段代码并code.class (在D://目录中)。 现在我想运行这个编译的文件。 我的代码是: import java.io.IOException; import java.io.InputStream; public class compiler { publ

How to avoid repetition within custom java exception classes

I like creating Exception classes whose names indicate the application-specific problems being noticed and thrown. To define them, generally a new class is defined whose super-class is some Exception type. Due to the multiple common constructors in the parent Exception class, generally the sub-class looks something like this: package com.example.exception; /** * MyException is thrown when

如何避免自定义Java异常类中的重复

我喜欢创建Exception类,这些类的名称表示应用程序特定问题被注意和抛出。 为了定义它们,通常定义一个新的class ,它的超类是一些Exception类型。 由于父Exception类中有多个常见构造函数,通常子类看起来像这样: package com.example.exception; /** * MyException is thrown when some application-level expectation is not met. */ public class MyException extends Exception { public MyException() {

How to validate overridden methods parameters with Hibernate Validator?

Regarding to this doc I understand that if I have my GroupService which implements GroupManager and overrides its methods, then I cannot annotate with validation constraints since Hibernate Validator doesn't allow it (which turns out to be known as the Liskov substitution principle). I mean doing something like public class GroupService implements GroupManager{ @Override public Lis

如何用Hibernate Validator验证重写的方法参数?

关于这个文档,我明白如果我的GroupService实现了GroupManager并覆盖了它的方法,那么我不能用验证约束来注释,因为Hibernate验证器不允许它(这被证明是Liskov替换原理)。 我的意思是做类似的事情 public class GroupService implements GroupManager{ @Override public List<String> findUsersInGroup(@NotNull String groupName) { ... } } 然后会引发ConstraintDeclarationException ,对吧

Return multiple values from void and boolean methods

I have the following problem: Having a boolean static method that computes similarity between two integers, I am asked to return 4 results: without changing the return type of the method, it should stay boolean. without updating/using the values of external variables and objects This is what I've done so far (I can't change return value from boolean to something else, such as an int

从void和boolean方法返回多个值

我有以下问题:有一个计算两个整数之间相似性的布尔型静态方法,我被要求返回4个结果: 而不改变方法的返回类型,它应该保持布尔值。 而无需更新/使用外部变量和对象的值 这是我迄今为止所做的(我不能将返回值从布尔值更改为其他值,例如int,我只能使用布尔值): public static boolean isSimilar(int a, int b) { int abs=Math.abs(a-b); if (abs==0) { return true; } else if (abs>10) { ret

Get approximate square root

I'm implementing Babylonian method to approximate the square root of number n using following formula : nextGuess = (lastGuess + n / lastGuess) / 2; So when nextGuess and lasGuess are almost identical, nextGuess is the approximated square root. What am doing is checking if nextGuess and lastGuess is less than very small number such as 0.0001 then i can claim that nextGuess is the approxim

获取近似平方根

我正在实施巴比伦方法来使用以下公式逼近n平方根: nextGuess = (lastGuess + n / lastGuess) / 2; 所以当nextGuess和lasGuess几乎相同时, nextGuess就是近似的平方根。 我们正在做的是检查nextGuess和lastGuess是否小于0.0001等非常小的数字,然后我可以声称nextGuess是n的近似平方根。 如果不是nextGuess变成lastGuess 。 那么我该如何以正确的方式实施呢? 我现在的代码: public static void getApproximatedSquar

*Methods* calling a boolean

This question already has an answer here: Compare class objects 6 answers 你需要在这里调用方法: Car car1; ... if(equals(car1)){ ... }

*方法*调用布尔值

这个问题在这里已经有了答案: 比较类对象6个答案 你需要在这里调用方法: Car car1; ... if(equals(car1)){ ... }

java programming error: actual and formal argument lists differ in length

This question already has an answer here: Java Error - Actual and formal argument lists differ in length 2 answers You should remove the parameter from the displayMenu method. Use public static int displayMenu() instead of public static int displayMenu(int userSelection) 这个错误告诉你到底发生了什么错误:你在没有参数的情况下调用displayMenu,并且它需要一个int值。

java编程错误:实际和形式参数列表的长度不同

这个问题在这里已经有了答案: Java错误 - 实际和正式参数列表的长度在2个答案中有所不同 您应该从displayMenu方法中删除参数。 使用 public static int displayMenu() 代替 public static int displayMenu(int userSelection) 这个错误告诉你到底发生了什么错误:你在没有参数的情况下调用displayMenu,并且它需要一个int值。

Design a class that tells whether a number is prime or not

My homework is to Design a class named MyInteger with the following conditions: An int data field named value that stores the int value of an integer. A constructor that creates a MyInteger object for the specified int value. A get method that returns the int value. A method, isPrime() that returns true if the value is a prime number. See -section 4.10 of the text for Java code that dete

设计一个能够告诉数字是否为素数的类

我的作业是使用以下条件设计一个名为MyInteger的类: 一个名为value的int数据字段,用于存储整数的int值。 为指定的int值创建MyInteger对象的构造函数。 一个返回int值的get方法。 isPrime()方法,如果该值是质数,则返回true。 检查素数的Java代码文本的第4.10节(这可能会因您所使用的版本而异)。 静态isPrime(MyInteger),如果该值是质数,则返回true。 请注意,此方法将对象引用变量(而不是值)作为参数。