急!!!怎样合并List中重复的数据,而且合并的List比原先的List要多一条字段数据????

阿刚_1998 2011-02-18 03:14:17
Distinct d1 = new Distinct("东莞", "长安沙头南区");
Distinct d2 = new Distinct("东莞", "长安沙头南区");
Distinct d3 = new Distinct("东莞", "桥头");
Distinct d4 = new Distinct("东莞", "桥头");
Distinct d5 = new Distinct("东莞", "桥头");
Distinct d6 = new Distinct("东莞", "长安镇步步高");




假如这就是数据,将其中address即“长安沙头南区”、“桥头”、“长安镇步步高”数据相同的合并为一条,且合并后
List还要增加一段“重复条数”数据,如下

东莞 长安沙头南区 2
东莞 桥头 3
东莞 长安镇步步高 1


java代码写了一半就卡壳了,哪位大侠能贴段清晰的java代码?
...全文
1089 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
蛰伏神兽 2011-02-21
  • 打赏
  • 举报
回复
Distinct 这个类里面加个几个get属性的方法就行啦.
然后拿出来做点比较.
阿刚_1998 2011-02-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 mycdsnstudy 的回复:]
Java code

package org.programmer.test.entity;

public class Distinct {

private String city;

private String address;

private int number;

public Distinct(String city, St……
[/Quote]


怎么我运行的结果成这样了 不对啊 有的数据没循环到啊


大家帮我看下 哪儿出问题了


oldList size: 6
newList size: 3
City:东莞 Address:长安沙头南区 Number:1
City:东莞 Address:桥头 Number:2
City:东莞 Address:长安镇步步高 Number:0

zn85600301 2011-02-18
  • 打赏
  • 举报
回复
新建一个数据对象 加上出现次数这个属性
然后以将以上数据存往一个MAP中
例: 以第一个字段 "东莞"为KEY value为 d1
每次往MAP里面存的时候先通过KEY 判断是否存在 如果存在就把 出现次数+1
magong 2011-02-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wspff01 的回复:]

这是最简单 真正的数据库中,相同数据只是一部分的局部数据,即
address为:查询条件(所属地区:惠州;地址或号码:烈山商业街;时间:2010-09-10 21:51:02)
提取其中的“烈山商业街”进行重复累计,sql语句没法写的
[/Quote]
就这个还是好写的,如
select count(*) from table1 group by substr( indexof(address, '地址或号码'), indexof(address, ';时间') - indexof(address, '地址或号码'))

呵呵。
不说啦,只当是闲聊,你忙去吧。
阿刚_1998 2011-02-18
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 shine333 的回复:]
稍微有个小bug。

Java code

Distinct d1 = new Distinct("东莞", "长安沙头南区");
Distinct d2 = new Distinct("东莞", "长安沙头南区");
Distinct d3 = new Distinct("东莞", "桥头");
Distinct d4……
[/Quote]

忘说了一点,相同的数据前面的city是一样的
阿刚_1998 2011-02-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yuanyue0540 的回复:]
引用 4 楼 magong 的回复:

这本该是数据库的活,一条SQL指令就解决了,非拿到Java中做干嘛呢?


同意
[/Quote]


这是最简单 真正的数据库中,相同数据只是一部分的局部数据,即
address为:查询条件(所属地区:惠州;地址或号码:烈山商业街;时间:2010-09-10 21:51:02)
提取其中的“烈山商业街”进行重复累计,sql语句没法写的
shine333 2011-02-18
  • 打赏
  • 举报
回复
稍微有个小bug。

Distinct d1 = new Distinct("东莞", "长安沙头南区");
Distinct d2 = new Distinct("东莞", "长安沙头南区");
Distinct d3 = new Distinct("东莞", "桥头");
Distinct d4 = new Distinct("东莞", "桥头");
Distinct d5 = new Distinct("东莞", "桥头");
Distinct d6 = new Distinct("东莞", "长安镇步步高");

假如都是东莞的就没问题,如果来了一个
Distinct d5 = new Distinct("西莞", "桥头");
就错了
小龙在线 2011-02-18
  • 打赏
  • 举报
回复

package org.programmer.test.entity;

public class Distinct {

private String city;

private String address;

private int number;

public Distinct(String city, String address) {
this.city = city;
this.address = address;
}

@Override
public String toString() {
return "City:" + city + "\tAddress:" + address + "\tNumber:" + number;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public int getNumber() {
return number;
}

public void setNumber(int number) {
this.number = number;
}

}
=====================================================================================
package org.programmer.test.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.inspur.test.entity.Distinct;

public class Test {
public static void main(String[] args) {
Distinct d1 = new Distinct("东莞", "长安沙头南区");
Distinct d2 = new Distinct("东莞", "长安沙头南区");
Distinct d3 = new Distinct("东莞", "桥头");
Distinct d4 = new Distinct("东莞", "桥头");
Distinct d5 = new Distinct("东莞", "桥头");
Distinct d6 = new Distinct("东莞", "长安镇步步高");
List<Distinct> oldList = new ArrayList<Distinct>();
oldList.add(d1);
oldList.add(d2);
oldList.add(d3);
oldList.add(d4);
oldList.add(d5);
oldList.add(d6);
System.out.println("old:size=" + oldList.size());
List<Distinct> newList = new ArrayList<Distinct>();
Iterator<Distinct> it = oldList.iterator();
while (it.hasNext()) {
Distinct d = it.next();
Iterator<Distinct> temp = newList.iterator();
boolean flag = false;
while (temp.hasNext()) {
Distinct dt = temp.next();
if (d.getCity().equals(dt.getCity())
&& d.getAddress().equals(dt.getAddress())) {
dt.setNumber(dt.getNumber() + 1);
flag = true;
break;
}
}
if (flag == false) {
newList.add(d);
}
}
System.out.println("new:size=" + newList.size());
Iterator<Distinct> temp = newList.iterator();
boolean flag = false;
while (temp.hasNext()) {
Distinct dt = temp.next();
System.out.println(dt);
}

}
}


测试结果:
old:size=6
new:size=3
City:东莞 Address:长安沙头南区 Number:2
City:东莞 Address:桥头 Number:3
City:东莞 Address:长安镇步步高 Number:1
e9876 2011-02-18
  • 打赏
  • 举报
回复
啊,我傻了
应该这样


public class DistinctList extends ArrayList{

private static Map<String,Integer> distinct_map = new HashMap<String,Integer>();
private Integer distinct_int_value = null;

public void show(){
for(String map_key:distinct_map.keySet()){
System.out.println(map_key + " " + distinct_map.get(map_key));
}
}


public boolean add(Distinct distinct){
if(distinct_map.containsKey(distinct.getAddress())){
Integer distinct_int_value = distinct_map.get(distinct.getAddress());
distinct_int_value = distinct_int_value+1;
return true;
}else{
distinct_map.put(distinct.getAddress(), 1);
return super.add(e);
}
}
}
yuanyue0540 2011-02-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 magong 的回复:]

这本该是数据库的活,一条SQL指令就解决了,非拿到Java中做干嘛呢?
[/Quote]

同意
magong 2011-02-18
  • 打赏
  • 举报
回复
这本该是数据库的活,一条SQL指令就解决了,非拿到Java中做干嘛呢?
e9876 2011-02-18
  • 打赏
  • 举报
回复

public class DistinctList extends ArrayList{

private static Map<String,Integer> distinct_map = new HashMap<String,Integer>();
private Integer distinct_int_value = null;

public void show(){
for(String map_key:distinct_map.keySet()){
System.out.println(map_key + " " + distinct_map.get(map_key));
}
}

@Override
public boolean add(Object e){

if(e instanceof Object){
Object distinct = (Object)e;
if(distinct_map.containsKey(distinct.getAddress())){
Integer distinct_int_value = distinct_map.get(distinct.getAddress());
distinct_int_value = distinct_int_value+1;
return true;
}else{
distinct_map.put(distinct.getAddress(), 1);
return super.add(e);
}
}else{
return false;
}

}
}

dongqdonglin 2011-02-18
  • 打赏
  • 举报
回复
你构造一个新的Distinct 属性加上长度 也就是继承Distinct 多加个属性而已
然后你重写equal方法 Distinct 的equal方法,然后比较就好了!!!
G_beginner 2011-02-18
  • 打赏
  • 举报
回复
觉得做法有问题!

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧