2015-01-29 13 views
2

에서 (쉼표를 사용하여) 드문 값을 찾기 위해이어떻게 행을 비교하고 공통의 가치와 자바

TableA의

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 

(TableA에서 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; 

} 

답변

1

일반적으로 그룹화 작업을 구현하는 방법은 Map입니다. 즉, 각 그룹에서 해당 그룹의 개체 목록에 대한 매핑을 만듭니다. 그런 다음지도를 반복하여 각 그룹을 처리 할 수 ​​있습니다. 귀하의 경우에는 ID로 그룹화하는 것처럼 보입니다 (다만 샘플 데이터 일 수도 있음). 자바 (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(",")); 
    ... 
}; 

당신이 만약 (도시와 도시별로 동시에 그룹화하는 것과 같이) 좀 더 복잡하게 그룹화하는 경우 그룹화 클래스를 만들어이 솔루션을 확장하십시오.

그런 다음 그룹화 문이 될 : 맵의 키가 지금 Group입니다

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

당신이 항목의 설정을 통해 반복 당신이 그룹의 마을과 도시를 얻을 수 있습니다.

0

당신이 사용 열팽창 계수 (하나의 재귀)와 같은 쿼리에서 그것을 할 수와 SQL에서 순위 기능 :

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 
관련 문제