如何使用java 8流将HashMap的值设置为自定义Java对象?



我有一个HashMap,我想将其转换为自定义对象Response。我不确定如何将值(80、190、900、95(从HashMap设置为自定义对象?如何在响应对象中编写一个单独的函数,该函数设置价格字段或设置两个参数(键和值(来fromString函数。

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;
}
}

在类Response创建一个构造函数,该构造函数接受两个参数并初始化相应的属性

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];
}
}

现在使用streammap转换为Response对象

List<Response> list = map.entrySet().stream().map(entry -> new Response(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());

我只会使用方法Map#entrySet()而不是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;
}
}

我不得不修复代码中语法不正确的一些内容。

  1. 长整型需要加L后缀
  2. 您需要在 put 语句后使用分号
  3. 我从您的课程中删除了多余的大括号

我添加了

  1. 构造函数
  2. 用于提取名称和城市或提供默认城市名称的实用程序例程。
  3. 以及一个toString方法,可让您查看 Response 类的内容。

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;
}
}

最新更新