问题
I have a HashMap
which I want to convert to custom object Response
. I am not sure how to set the values (80, 190, 900, 95) from HashMap
to the custom object? How to write a separate function in Response object which sets price fields or set two parameters (key and value) to fromString
function.
class Converter{
public static void main(String[] args) {
Map<String, Long> map = new HashMap<String, Long>();
map.put("111", 80) // first
map.put("1A9-ppp", 190) // second
map.put("98U-6765", 900) // third
map.put("999-aa", 95) // fourth
List<Response> list =
map.keySet().stream().map(ent-> Response.fromString(ent.getKey(), ent.getValue())).collect(Collectors.toList());
// how to set price field from value of the key
}
}
class Response{}{
String name;
Long code;
String city;
Long price;
public static Response fromString(String key, Long value){
Response res = new Response();
String[] keys = key.split("//-");
// some logic to set name and city
res.name = keys[0];
if(keys.length == 2) {
if(keys[1].matches("-?\\d+(\\.\\d+)?") // check if string is numeric
{
res.code = keys[1]
}
else{
res.city = keys[1]
}
}
res.price = value;
return res;
}
}
回答1:
I had to fix some things in your code that were syntactically incorrect.
- Longs need to be suffixed with an
L
- You needed semicolons after your put statements
- I removed the extra braces from your class
I added
- A constructor
- A utility routine to extract the name and city or provide a default city name.
- And a
toString
method to let you see the contents of the Response class.
public class Converter {
public static void main(String[] args) {
Map<String, Long> map = new HashMap<>();
map.put("111", 80L); // first
map.put("1A9-ppp", 190L); // second
map.put("98U-6765", 900L); // third
map.put("999-aa", 95L); // fourth
List<Response> list = map.entrySet().stream().map(
ent -> Response.instanceOf(ent.getKey(), ent.getValue())).collect(
Collectors.toList());
System.out.println(list);
}
}
class Response {
String name;
String city;
long price;
public Response(String name, String city, long price) {
this.name = name;
this.city = city;
this.price = price;
}
public static Response instanceOf(String str, long price) {
if (str.indexOf("-") == -1) {
str += "-NoName";
}
String[] items = str.split("-");
return new Response(items[0], items[1], price);
}
public String toString() {
return "name = " + name + ", city = " + city + ", " + "price = " + price;
}
}
回答2:
Create a constructor in Response
class that accepts two parameters and initialize the corresponding properties
class Response {
String name;
String city;
Long price;
public Response(String key, Long price) {
this.price=price;
String[] keys = key.split("-");
//check conditions and set values to properties
this.name = keys[0];
this.city = keys[1];
}
}
Now use stream
and map
to convert into Response
object
List<Response> list = map.entrySet().stream().map(entry -> new Response(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
回答3:
I would just use the Method Map#entrySet()
instead of keySet()
.
class Converter{
public static void main(String[] args) {
Map<String, Long> map = new HashMap<String, Long>();
map.put("111", 80); // first
map.put("1A9-ppp", 190); // second
map.put("98U-6765", 900); // third.
map.put("999-aa", 95) // fourth
List<Response> list = map.entrySet().stream().map(Response::fromEntry).collect(Collectors.toList());
}
}
class Response{}{
String name;
String city;
Long price;
public static Response fromEntry(Map.Entry<String,Long> entry){
String key=entry.getKey();
Long price=entry.getValue();
Response res = new Response();
String[] keys = key.split("//-");
// some logic to set name and city
res.name = keys[0];
if(keys.length > 1) {
res.city = keys[1];
}
//set price
price=value;
return res;
}
}
来源:https://stackoverflow.com/questions/59021628/how-to-set-value-of-hashmap-into-a-custom-java-object-using-java-8-stream