java - 如何比较行并在java中找到常见值和不常见值(使用逗号)

标签 java jakarta-ee

这是我的 table

表A

Id      Town     City     Country  
1        a         b         c  
1        a         b         d  
1        a         b         e  
2        f         g         h  
2        f         g         i  
2        f         g         j  
3        k         l         m  

在我的 jsp 页面中,这就是我需要显示表中数据的方式

          Title

Id      Town     City     Country  
1        a         b       c,d,e  
2        f         g       h,i,j  
3        k         l         m  

我运行查询(从表 A 中选择 ID、城镇、城市、国家/地区)。然后

public List<Place> getAddressChangeView () 
{
    List <Place> countriesList = new ArrayList < Place> ();

    List <Object[]> z = query.getResultList();

    List<String> countries = new ArrayList String;
    String delim ="," ;

    Place places = null;
    for (Object[] j: z)
    {

        places = new Place();
        int id = ((String) obj[0]);
        String town=((String) obj1);
        String city=((String) obj[2]);
        String country=((String) obj[3]);

        if (! id.equals(id1) && ! town.equals(town1) && ! city.equals(city1) && ! country.equals(country1)) 
        {
            id1= id;
            town1= town;
            city1=city;
            country1=country;

            places.setId(id);
            places.setTown(town);
            places.setCity(city);

        }

        countries.add(country);

        StringBuilder sb = new StringBuilder();

        for (String s: countries)
        {
            sb.append(s).append(delim);

        }
        places.setCountry(sb.toString());

        countriesList.add(places);

    }

    return countriesList;

}

最佳答案

实现分组操作的常用方法是使用 Map。换句话说,您创建一个从每个组到该组中对象列表的映射。然后您可以遍历映射以处理每个组。在您的情况下,您似乎只是按 ID 分组(尽管这可能只是您提供的样本数据)。

使用 java 8 流它看起来像:

List<Place> places = table.stream()
    .map(Place::new).collect(Collectors.toList());
Map<String, List<Place>> grouping = places.stream()
    .collect(Collectors.groupingBy(Place::getID));

然后您可以轻松地遍历这些组,对它们执行任何您需要的操作,包括以逗号分隔的国家/地区:

grouping.entrySet().stream().forEach(entry -> {
    String id = entry.getKey();
    List<Place> places = entry.getValue();
    String countries = places.stream()
        .map(Place::getCountry)
        .collect(Collectors.joining(","));
    ...
};

如果您按更复杂的事物进行分组(例如同时按城镇和城市分组),那么您可以通过创建分组类来扩展此解决方案:

class Group {
    private final String town;
    private final String city;
    public static Group forPlace(Place place) {
        return new Group(place.getTown(), place.getCity());
    }
}

那么分组语句就变成了:

Map<Group, List<Place>> grouping = places.stream()
    .collect(Collectors.groupingBy(Group::forPlace));

由于 map 的键现在是 Group,您可以在遍历条目集时获取该组的 Town 和 City。

关于java - 如何比较行并在java中找到常见值和不常见值(使用逗号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28225964/

相关文章:

java - 如何检测文本是否包含 [FSI]*[PDI]

java - 我可以为我的 Java Enterprise 项目使用多个框架吗?

java - 加载时从 jsp 调用不返回任何内容的 servlet

java - 当软键盘可见时,CollapsingToolbarLayout 不折叠

java - Spring @Autowired变量在@Component内部为空

java - 为什么 hello world 在 Spring 中不显示并出现异常?

java - 将远程 Glassfish 服务器添加到 Netbeans

java - 优雅地处理 EJB/JPA 环境中的约束违规?

c# - 带有 EMF 和 RCP 的 Java 与 C#

java - Java中如何比较两个HashMap