如何在java中比较行并找到常用值和不常用值(使用逗号)
这是我的桌子 表如何在java中比较行并找到常用值和不常用值(使用逗号),java,jakarta-ee,Java,Jakarta Ee,这是我的桌子 表 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
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
我运行查询,从表中选择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;
}
实现分组操作的常用方法是使用映射。换句话说,您可以创建从每个组到该组中对象列表的映射。然后,您可以遍历映射来处理每个组。在您的情况下,看起来您只是按照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));
然后分组语句变为:
Map<Group, List<Place>> grouping = places.stream()
.collect(Collectors.groupingBy(Group::forPlace));
由于地图的键是now Group,您可以在遍历条目集时获得该组的城镇和城市。您可以在SQL中使用CTEs one recursive和rank函数在查询中这样做:
with Countries(Id, Town, City, Country, Seq) as (
select Id, Town, City, Country,
row_number() over(partition by Id order by Country)
from TableA
), Maxes(Id, Seq) as (
select Id, max(Seq) from Countries group by Id
), Recursed(Id, Town, City, Country, Seq) as (
select Maxes.Id, Town, City, Country, Maxes.Seq
from Countries, Maxes
where Countries.Id = Maxes.Id and Countries.Seq = Maxes.Seq
union all
select c.Id, c.Town, c.City, c.Country || ',' || r.Country, c.Seq
from Countries c, Recursed r
where c.Id = r.Id and c.Seq = r.Seq - 1
)
select Id, Town, City, Country
from Recursed
where Seq = 1
order by Id
假设字符串串联运算符| |,这将产生:
Id Town City Country
1 a b c,d,e
2 f g h,i,j
3 k l m
with Countries(Id, Town, City, Country, Seq) as (
select Id, Town, City, Country,
row_number() over(partition by Id order by Country)
from TableA
), Maxes(Id, Seq) as (
select Id, max(Seq) from Countries group by Id
), Recursed(Id, Town, City, Country, Seq) as (
select Maxes.Id, Town, City, Country, Maxes.Seq
from Countries, Maxes
where Countries.Id = Maxes.Id and Countries.Seq = Maxes.Seq
union all
select c.Id, c.Town, c.City, c.Country || ',' || r.Country, c.Seq
from Countries c, Recursed r
where c.Id = r.Id and c.Seq = r.Seq - 1
)
select Id, Town, City, Country
from Recursed
where Seq = 1
order by Id
Id Town City Country
1 a b c,d,e
2 f g h,i,j
3 k l m