按指定顺序从 LinkedHashMap 中检索 KeySet
Retrieving KeySet from LinkedHashMap in specified order
我正在编写一个从 List of LinkedHashMap
中提取密钥的程序。然后提取的键将用逗号连接并打印为 CSV 文件中的 headers 列。
flatJson
的示例内容如下所示:
{
Submitted_Date=03-11-2016 01:19:41,
Comments=,
Rating=3,
details.name=Willy,
details.personalId=S123456A,
details.mobileNo=11111111,
details.email=willy@email.com,
Channel=desktop,
Referral_Code=,
discount.promoCode=,
discount.referral=,
policyPlan.grossPremium=788.64,
policyPlan.name=Standard,
policyPlan.netPremium=788.64
}
代码:
private TreeSet<String> collectHeaders(List<LinkedHashMap<String, String>> flatJson) {
TreeSet<String> headers = new TreeSet<String>();
for (LinkedHashMap<String, String> linkedMap : flatJson) {
headers.addAll(linkedMap.keySet());
}
return headers;
}
我从这个程序中得到的 headers 的顺序是:
Channel
Comments
Rating
Referral_Code
Submitted_Date
details.email
details.personalId
details.mobileNo
details.name
discount.promoCode
discount.referral
policyPlan.grossPremium
policyPlan.name
policyPlan.netPremium
虽然预期输出与 flatJson
相同:
Submitted_Date
Comments
Rating
details.name
details.personalId
details.mobileNo
details.email
Channel
Referral_code
discount.promoCode
discount.referral
policyPlan.grossPremium
policyPlan.name
policyPlan.netPremium
以前,我使用的 HashMap
和 Set
不支持排序。我已经将它们更改为 LinkedHashMap
和 TreeSet
但仍然无法获得正确的顺序。
TreeSet 与保留插入顺序无关。它是关于排序它的元素。
换句话说:如果您将 String 对象添加到 TreeSet,它们将得到 ordered,就像您 排序那个列表!
因此,如果 order 是您最关心的问题,您应该在创建 TreeSet 时使用 LinkedHashSet instead of TreeSet! Alternatively, you could provide a custom Comparator ...它以某种方式为您提供了您正在寻找的顺序- 这可能行得通,但对我来说感觉像是变通方法。
我正在编写一个从 List of LinkedHashMap
中提取密钥的程序。然后提取的键将用逗号连接并打印为 CSV 文件中的 headers 列。
flatJson
的示例内容如下所示:
{
Submitted_Date=03-11-2016 01:19:41,
Comments=,
Rating=3,
details.name=Willy,
details.personalId=S123456A,
details.mobileNo=11111111,
details.email=willy@email.com,
Channel=desktop,
Referral_Code=,
discount.promoCode=,
discount.referral=,
policyPlan.grossPremium=788.64,
policyPlan.name=Standard,
policyPlan.netPremium=788.64
}
代码:
private TreeSet<String> collectHeaders(List<LinkedHashMap<String, String>> flatJson) {
TreeSet<String> headers = new TreeSet<String>();
for (LinkedHashMap<String, String> linkedMap : flatJson) {
headers.addAll(linkedMap.keySet());
}
return headers;
}
我从这个程序中得到的 headers 的顺序是:
Channel
Comments
Rating
Referral_Code
Submitted_Date
details.email
details.personalId
details.mobileNo
details.name
discount.promoCode
discount.referral
policyPlan.grossPremium
policyPlan.name
policyPlan.netPremium
虽然预期输出与 flatJson
相同:
Submitted_Date
Comments
Rating
details.name
details.personalId
details.mobileNo
details.email
Channel
Referral_code
discount.promoCode
discount.referral
policyPlan.grossPremium
policyPlan.name
policyPlan.netPremium
以前,我使用的 HashMap
和 Set
不支持排序。我已经将它们更改为 LinkedHashMap
和 TreeSet
但仍然无法获得正确的顺序。
TreeSet 与保留插入顺序无关。它是关于排序它的元素。
换句话说:如果您将 String 对象添加到 TreeSet,它们将得到 ordered,就像您 排序那个列表!
因此,如果 order 是您最关心的问题,您应该在创建 TreeSet 时使用 LinkedHashSet instead of TreeSet! Alternatively, you could provide a custom Comparator ...它以某种方式为您提供了您正在寻找的顺序- 这可能行得通,但对我来说感觉像是变通方法。