java分组统计问题
有一个Price对象publicclassPrice{privateintpriceId;privateintprice1;privateintprice2;privat...
有一个Price对象
public class Price{
private int priceId;
private int price1;
private int price2;
private int price3;
}
现有一个List<price>集合,想把集合里的Price对象先按price1分组,再按price2分组,最后按price3分组,把同一组的priceId相同,比如价格分别是:
第一组:
[1, 2, 3]
[1, 2, 3]
第二组:
[1, 3, 5]
[1, 3, 5]
第三组:
[2, 4, 4]
[2, 4, 4]
第四组:
[2, 4, 5]
第五组:
[2, 5, 6]
所以每个price对象最后的priceId分别应为
1,1,2,2,3,3,4,5,
以上过程需用java实现。希望有高手解答。 展开
public class Price{
private int priceId;
private int price1;
private int price2;
private int price3;
}
现有一个List<price>集合,想把集合里的Price对象先按price1分组,再按price2分组,最后按price3分组,把同一组的priceId相同,比如价格分别是:
第一组:
[1, 2, 3]
[1, 2, 3]
第二组:
[1, 3, 5]
[1, 3, 5]
第三组:
[2, 4, 4]
[2, 4, 4]
第四组:
[2, 4, 5]
第五组:
[2, 5, 6]
所以每个price对象最后的priceId分别应为
1,1,2,2,3,3,4,5,
以上过程需用java实现。希望有高手解答。 展开
2个回答
展开全部
public class zhidao1 {
public static void main(String agrs[]) {
List priceList = new ArrayList();
priceList.add(new Price(1,2,3));
priceList.add(new Price(4,3,3));
priceList.add(new Price(2,5,4));
priceList.add(new Price(3,2,3));
priceList.add(new Price(3,2,3));
//printList(priceList);
Collections.sort(priceList);
//printList(priceList);
int id=0;
((Price)priceList.get(0)).setPriceId(0);
for(int i=0; i<priceList.size()-1; i++){
if(( (Price)priceList.get(i)).compareTo( (Price)priceList.get(i+1) )!=0){
id++;
}
((Price)priceList.get(i+1)).setPriceId(id);
}
printList(priceList);
}
public static void printList(List priceList) {
for(int i=0; i<priceList.size(); i++) {
Price price = (Price)priceList.get(i);
if(i!=0){
if( price.compareTo( (Price)priceList.get(i-1) )!=0){
System.out.println();
}
}
System.out.println("price1="+price.getPrice1()+
" price2="+price.getPrice2()+
" price3="+price.getPrice3()+
" priceId="+price.getPriceId()
);
}
System.out.println();
}
}
class Price implements Comparable{
private int priceId;
private int price1;
private int price2;
private int price3;
public Price(){}
public Price(int price1,int price2,int price3) {
this.price1 = price1;
this.price2 = price2;
this.price3 = price3;
}
public int compare(Object arg0,Object arg1) {
Price p1 = (Price)arg0;
Price p2 = (Price)arg1;
if(p1.getPrice1() > p2.getPrice1())
return 1;
else if(p1.getPrice1() <p2.getPrice1())
return -1;
else{
if(p1.getPrice2() > p2.getPrice2())
return 1;
else if(p1.getPrice2() <p2.getPrice2())
return -1;
else{
if(p1.getPrice3() > p2.getPrice3())
return 1;
else if(p1.getPrice3() <p2.getPrice3())
return -1;
else
return 0;
}
}
}
public int getPriceId() {
return priceId;
}
public void setPriceId(int priceId) {
this.priceId = priceId;
}
public int getPrice1() {
return price1;
}
public void setPrice1(int price1) {
this.price1 = price1;
}
public int getPrice2() {
return price2;
}
public void setPrice2(int price2) {
this.price2 = price2;
}
public int getPrice3() {
return price3;
}
public void setPrice3(int price3) {
this.price3 = price3;
}
@Override
public int compareTo(Object o) {
Price p = (Price)o;
if(p.getPrice1()>price1)
return -1;
else if(p.getPrice1()<price1)
return 1;
else{
if(p.getPrice2()>price2)
return -1;
else if(p.getPrice2()<price2)
return 1;
else{
if(p.getPrice3()>price3)
return -1;
else if(p.getPrice3()<price3)
return 1;
else
return 0;
}
}
}
}
public static void main(String agrs[]) {
List priceList = new ArrayList();
priceList.add(new Price(1,2,3));
priceList.add(new Price(4,3,3));
priceList.add(new Price(2,5,4));
priceList.add(new Price(3,2,3));
priceList.add(new Price(3,2,3));
//printList(priceList);
Collections.sort(priceList);
//printList(priceList);
int id=0;
((Price)priceList.get(0)).setPriceId(0);
for(int i=0; i<priceList.size()-1; i++){
if(( (Price)priceList.get(i)).compareTo( (Price)priceList.get(i+1) )!=0){
id++;
}
((Price)priceList.get(i+1)).setPriceId(id);
}
printList(priceList);
}
public static void printList(List priceList) {
for(int i=0; i<priceList.size(); i++) {
Price price = (Price)priceList.get(i);
if(i!=0){
if( price.compareTo( (Price)priceList.get(i-1) )!=0){
System.out.println();
}
}
System.out.println("price1="+price.getPrice1()+
" price2="+price.getPrice2()+
" price3="+price.getPrice3()+
" priceId="+price.getPriceId()
);
}
System.out.println();
}
}
class Price implements Comparable{
private int priceId;
private int price1;
private int price2;
private int price3;
public Price(){}
public Price(int price1,int price2,int price3) {
this.price1 = price1;
this.price2 = price2;
this.price3 = price3;
}
public int compare(Object arg0,Object arg1) {
Price p1 = (Price)arg0;
Price p2 = (Price)arg1;
if(p1.getPrice1() > p2.getPrice1())
return 1;
else if(p1.getPrice1() <p2.getPrice1())
return -1;
else{
if(p1.getPrice2() > p2.getPrice2())
return 1;
else if(p1.getPrice2() <p2.getPrice2())
return -1;
else{
if(p1.getPrice3() > p2.getPrice3())
return 1;
else if(p1.getPrice3() <p2.getPrice3())
return -1;
else
return 0;
}
}
}
public int getPriceId() {
return priceId;
}
public void setPriceId(int priceId) {
this.priceId = priceId;
}
public int getPrice1() {
return price1;
}
public void setPrice1(int price1) {
this.price1 = price1;
}
public int getPrice2() {
return price2;
}
public void setPrice2(int price2) {
this.price2 = price2;
}
public int getPrice3() {
return price3;
}
public void setPrice3(int price3) {
this.price3 = price3;
}
@Override
public int compareTo(Object o) {
Price p = (Price)o;
if(p.getPrice1()>price1)
return -1;
else if(p.getPrice1()<price1)
return 1;
else{
if(p.getPrice2()>price2)
return -1;
else if(p.getPrice2()<price2)
return 1;
else{
if(p.getPrice3()>price3)
return -1;
else if(p.getPrice3()<price3)
return 1;
else
return 0;
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package dispatch;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Price {
private int priceId;
private int price1;
private int price2;
private int price3;
public Price(int price1,int price2,int price3){
this.price1=price1;
this.price2=price2;
this.price3=price3;
}
public int getPriceId() {
return priceId;
}
public int getPrice1() {
return price1;
}
public int getPrice2() {
return price2;
}
public int getPrice3() {
return price3;
}
public void setPriceId(int priceId){
this.priceId = priceId;
}
public static int compare(Price price1, Price price2){
if(price1.price1 - price2.price1 != 0){
return price1.price1 - price2.price1;
}
if(price1.price2 - price2.price2 != 0){
return price1.price2 - price2.price2;
}
if(price1.price3 - price2.price3 != 0){
return price1.price3 - price2.price3;
}
return 0;
}
private void printSelf() {
System.out.println("priceId="+priceId+"["+price1+", "+price2+", "+price3+"]");
}
public static void main(String[] args){
List<Price> prices = new ArrayList<Price>();
Price prices1 = new Price(1, 2, 3);
Price prices2 = new Price(1, 2, 3);
Price prices3 = new Price(1, 3, 5);
Price prices4 = new Price(1, 3, 5);
Price prices5 = new Price(2, 4, 4);
Price prices6 = new Price(2, 4, 4);
Price prices7 = new Price(2, 4, 5);
Price prices8 = new Price(2, 5, 6);
prices.add(prices1);
prices.add(prices2);
prices.add(prices4);
prices.add(prices5);
prices.add(prices3);
prices.add(prices6);
prices.add(prices7);
prices.add(prices8);
Collections.sort(prices, new Comparator<Price>(){
public int compare(Price price1, Price price2){
return Price.compare(price1, price2);
}
public boolean equals(Object o){
return this==o;
}
});
int count=1;
prices.get(0).setPriceId(count);
Price priceTemp1 = prices.get(0);
for(int i = 1; i<prices.size(); i++){
Price priceTemp2 = prices.get(i);
if(compare(priceTemp1, priceTemp2)!=0){
priceTemp1 = priceTemp2;
count++;
}
prices.get(i).setPriceId(count);
}
for(int i = 0; i<prices.size(); i++){
prices.get(i).printSelf();
}
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Price {
private int priceId;
private int price1;
private int price2;
private int price3;
public Price(int price1,int price2,int price3){
this.price1=price1;
this.price2=price2;
this.price3=price3;
}
public int getPriceId() {
return priceId;
}
public int getPrice1() {
return price1;
}
public int getPrice2() {
return price2;
}
public int getPrice3() {
return price3;
}
public void setPriceId(int priceId){
this.priceId = priceId;
}
public static int compare(Price price1, Price price2){
if(price1.price1 - price2.price1 != 0){
return price1.price1 - price2.price1;
}
if(price1.price2 - price2.price2 != 0){
return price1.price2 - price2.price2;
}
if(price1.price3 - price2.price3 != 0){
return price1.price3 - price2.price3;
}
return 0;
}
private void printSelf() {
System.out.println("priceId="+priceId+"["+price1+", "+price2+", "+price3+"]");
}
public static void main(String[] args){
List<Price> prices = new ArrayList<Price>();
Price prices1 = new Price(1, 2, 3);
Price prices2 = new Price(1, 2, 3);
Price prices3 = new Price(1, 3, 5);
Price prices4 = new Price(1, 3, 5);
Price prices5 = new Price(2, 4, 4);
Price prices6 = new Price(2, 4, 4);
Price prices7 = new Price(2, 4, 5);
Price prices8 = new Price(2, 5, 6);
prices.add(prices1);
prices.add(prices2);
prices.add(prices4);
prices.add(prices5);
prices.add(prices3);
prices.add(prices6);
prices.add(prices7);
prices.add(prices8);
Collections.sort(prices, new Comparator<Price>(){
public int compare(Price price1, Price price2){
return Price.compare(price1, price2);
}
public boolean equals(Object o){
return this==o;
}
});
int count=1;
prices.get(0).setPriceId(count);
Price priceTemp1 = prices.get(0);
for(int i = 1; i<prices.size(); i++){
Price priceTemp2 = prices.get(i);
if(compare(priceTemp1, priceTemp2)!=0){
priceTemp1 = priceTemp2;
count++;
}
prices.get(i).setPriceId(count);
}
for(int i = 0; i<prices.size(); i++){
prices.get(i).printSelf();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询