gpt4 book ai didi

java - 按用户 ID 对来自 json 文件的消息进行分组

转载 作者:行者123 更新时间:2023-11-30 08:13:16 25 4
gpt4 key购买 nike

我能够读取包含消息及其对应作者列表的 JSON 文件。单条消息结构如下:

JSON

{
"created_at": "Thu Apr 30 10:47:49 +0000 2015",
"id": 593728455901990900,
"user": {
"id": 12,
"name": "GiGi",
"user_date": "Thu May 17 10:47:49 +0000 2010"
}

Author 和 Message 类 (POJO) 包含我们要解析的字段和一个函数 toString() 以字符串形式显示这些字段。

作者

public class Author {

@SerializedName("id")
private static long id;

@SerializedName("created_at")
private static String user_date;

private static String name;

public String toString() {
return name + "\n" + id + "\n" + user_date;
}
....Getters & Setters....
}

留言

public class Message {

@SerializedName("user")
Author author;

@SerializedName("created_at")
String date;

long id;

public String toString() {
return id + "\n" + date;
}

}

Author 是 Message 类的成员类,我需要显示按用户分组的消息,但我似乎没有找到一种优雅的方法。

更新

一个解决方案是创建一个 Map,其中作者的一个属性作为 KEY(在这个代码示例中是作者的 ID),相应的消息列表作为 VALUE。

Map<Long, List<Message>> map = new HashMap<Long, List<Message>>();
for (Message msg : listOfMessages) {
long key = msg.getAuthor().getId();

if (map.get(key) == null) {
map.put(key, new ArrayList<Message>());
}
map.get(key).add(msg);
}

此解决方案可能引发的问题:

  • 如何显示作者 id AND name 及其相应的消息列表?
  • 我不太确定这个解决方案是否优雅,因为 Message 类包含 Author 类,并且 MAP 值中“携带”了很多不必要的信息。

解决方案

在 Author 类中添加 equalshashCode 方法后,我们可以将 Author 实例映射为键。

@Override
public int hashCode() {
int hash = 7;
hash = 23 * hash + (int) (this.id ^ (this.id >>> 32));
return hash;
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Author author = (Author) obj;
if (this.id != author.id) {
return false;
}
return true;
}

在主类中,我们可以遍历消息列表并正确填充映射:

Map<Author, List<Message>> map = new HashMap<Author, List<Message>>();
for (Message message : listOfMessages) {
List<Message> userMessages = map.get(message.getAuthor());
if (userMessages == null) {
userMessages = new ArrayList<Message>();
map.put(message.getAuthor(), userMessages);
}
userMessages.add(message);
}
for (Entry<Author, List<Message>> entry : map.entrySet()) {
System.out.println(entry.getKey().getId());
for (Message message : entry.getValue()) {
System.out.println("\t" + message.getDate());
}
}

输出

3037057186
Thu Apr 30 10:47:52 +0000 2015
1598532858
Thu Apr 30 10:47:51 +0000 2015
67267979
Thu Apr 30 11:47:49 +0000 2015
Thu Apr 30 10:47:49 +0000 2015
Thu Apr 30 10:47:49 +0000 2015
Thu Apr 30 10:47:49 +0000 2015

最佳答案

考虑这个 json 文件 example.json

 [
{
"date":"Thu Apr 30 10:47:49 +0000 2015",
"id":593728455901990912,
"author":{
"id":12,
"name":"GiGi",
"user_date":"Thu May 17 10:47:49 +0000 2010"
}
},

{
"date":"Thu Apr 30 10:47:49 +0000 2013",
"id":593728455901990977,
"author":{
"id":12,
"name":"GiGi",
"user_date":"Thu May 17 10:47:49 +0000 2010"
}
},

{
"date":"Thu Apr 30 10:47:49 +0000 2015",
"id":593728422901330999,
"author":{
"id":13,
"name":"HiHi",
"user_date":"Thu May 17 10:47:49 +0000 2015"
}
}
]

作者类

class Author {    
private long id;
private String name;
private String user_date;
public String toString() {
return name + " " + id + " " + user_date;
}
//getters, setters,...
}

消息类

class Message {
String date;
long id;
Author author;
public String toString() {
return id + " " + date;
}
//getters, setters,...
}

使用 Gson API 解析 json

Gson gson = new Gson();
List<Message> list = gson.fromJson(new BufferedReader(new FileReader(
"example.json")), new TypeToken<List<Message>>() {
}.getType());

显示按用户 id 分组的消息

Map<Long, List<Message>> groupedMap = list.stream().collect(
Collectors.groupingBy(m-> m.getAuthor().getId()));

groupedMap.forEach((k, v) -> System.out.println(k + " => " + v));

输出:

12 => [593728455901990912 Thu Apr 30 10:47:49 +0000 2015, 593728455901990977 Thu Apr 30 10:47:49 +0000 2013]
13 => [593728422901330999 Thu Apr 30 10:47:49 +0000 2015]

关于java - 按用户 ID 对来自 json 文件的消息进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30082103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com