数据操作高手来

a75386666 2009-05-07 09:24:26
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test {
public void test()
{
List list = new ArrayList();
Map map = new HashMap();
Map map1 = new HashMap();
Map map2 = new HashMap();
Map map3 = new HashMap();
Map map4 = new HashMap();
Map map5 = new HashMap();
Map map6 = new HashMap();
Map map7 = new HashMap();
Map map8 = new HashMap();
Map map9 = new HashMap();
map1.put("zuid", "7");
map1.put("gongsiid", "4");
map1.put("zuname", "新建测试组");
map1.put("renyuanid", "5");
map1.put("renname", "旖旎");
map1.put("fatherid", "0");

list.add(map1);
map2.put("zuid", "7");
map2.put("gongsiid", "4");
map2.put("zuname", "新建测试组");
map2.put("renyuanid", "6");
map2.put("renname", "旖旎");
map2.put("fatherid", "0");
list.add(map2);
map3.put("zuid", "7");
map3.put("gongsiid", "4");
map3.put("zuname", "新建测试组");
map3.put("renyuanid", "3");
map3.put("renname", "旖旎");
map3.put("fatherid", "0");
list.add(map3);
map.put("zuid", "7");
map.put("gongsiid", "4");
map.put("zuname", "新建测试组");
map.put("renyuanid", "2");
map.put("renname", "旖旎");
map.put("fatherid", "0");
list.add(map);
map4.put("zuid", "7");
map4.put("gongsiid", "4");
map4.put("zuname", "新建测试组");
map4.put("renyuanid", "1");
map4.put("renname", "旖旎");
map4.put("fatherid", "0");
list.add(map4);
//第二个组

map5.put("zuid", "4");
map5.put("gongsiid", "4");
map5.put("zuname", "测试组");
map5.put("renyuanid", "5");
map5.put("renname", "旖旎");
map5.put("fatherid", "0");
list.add(map5);
map6.put("zuid", "4");
map6.put("gongsiid", "4");
map6.put("zuname", "测试组");
map6.put("renyuanid", "6");
map6.put("renname", "旖旎");
map6.put("fatherid", "0");
list.add(map6);
map7.put("zuid", "4");
map7.put("gongsiid", "4");
map7.put("zuname", "测试组");
map7.put("renyuanid", "3");
map7.put("renname", "旖旎");
map7.put("fatherid", "0");
list.add(map7);
map8.put("zuid", "4");
map8.put("gongsiid", "4");
map8.put("zuname", "测试组");
map8.put("renyuanid", "2");
map8.put("renname", "旖旎");
map8.put("fatherid", "0");
list.add(map8);
map9.put("zuid", "4");
map9.put("gongsiid", "4");
map9.put("zuname", "测试组");
map9.put("renyuanid", "1");
map9.put("renname", "旖旎");
map9.put("fatherid", "0");
list.add(map9);

System.out.println("list中共有"+list.size()+"条数据");

List<Map> fatherlist = new ArrayList();
List<Map> childlist =new ArrayList();
int k=list.size();
for(int s=0;s<k;s++)
{
Map fathermap= new HashMap();
fathermap.clear();
fathermap.put("fathernodeid", 0);
//System.out.println("fathermap里的fathernodeid是"+fathermap.get("fathernodeid"));
fathermap.put("zuid", ((Map)list.get(s)).get("zuid"));
// System.out.println("fathermap里的zuid是"+fathermap.get("zuid"));
fathermap.put("zuname", ((Map)list.get(s)).get("zuname"));
//System.out.println("fathermap里的zuname是"+fathermap.get("zuname"));
//System.out.println("准备向list里添加map");

if(s==0)
{
fatherlist.add(fathermap);

System.out.println("第一次循环后fatherlist的长度"+fatherlist.size());
continue;
}else if(s>0){
for(int p=0; p<fatherlist.size();p++)
{
if(((fathermap.get("zuname")).toString()).trim().equals((((fatherlist.get(p).get("zuname")).toString()).trim()))==true)
{
// System.out.println("取到map里的的zuname是"+(fathermap.get("zuname")).toString());
// System.out.println("取到list里的的zuname是"+(fatherlist.get(p).get("zuname")).toString());
break;
}else
{
//System.out.println("比对后相等添加记录");
fatherlist.add(fathermap);
}
}
}

}
for(int t=0;t<fatherlist.size();t++)
{
System.out.println(fatherlist.get(t));
}
for(int s=0;s<k;s++)
{
Map childmap= new HashMap();
childmap.put("renyuanid", ((Map)list.get(s)).get("renyuanid"));
childmap.put("zuid", ((Map)list.get(s)).get("zuid"));
childmap.put("renname", ((Map)list.get(s)).get("renname"));
childlist.add(childmap);
}
}
public static void main(String[] args) {
Test test = new Test();
test.test();
}
}
以上代码是写在一个测试javabean里的 意图是把相同的数据只取一条 现在的集合是写死的 是按照在数据库中查出来的样子写的
运行结果是这样的
list中共有10条数据
第一次循环后fatherlist的长度1
{fathernodeid=0, zuname=新建测试组, zuid=7}
{fathernodeid=0, zuname=测试组, zuid=4}
{fathernodeid=0, zuname=测试组, zuid=4}
{fathernodeid=0, zuname=测试组, zuid=4}
{fathernodeid=0, zuname=测试组, zuid=4}
{fathernodeid=0, zuname=测试组, zuid=4} 求达人解决一下这个问题,,,,
...全文
123 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinxfei 2009-05-07
  • 打赏
  • 举报
回复
没看明白。

a75386666 2009-05-07
  • 打赏
  • 举报
回复
解决 分2次查第一次查出有多少个组 第二次查人都是在哪个组的 分别装在2个集合里 分别迭代 搞定!!!
a75386666 2009-05-07
  • 打赏
  • 举报
回复
数据库里查出来的数据是

{zuname=新建测试组, renyuanid=5, zuid=7, renname=旖旎, gongsiid=4, fatherid=0}
{zuname=新建测试组, renyuanid=6, zuid=7, renname=旖旎, gongsiid=4, fatherid=0}
{zuname=新建测试组, renyuanid=3, zuid=7, renname=旖旎, gongsiid=4, fatherid=0}
{zuname=新建测试组, renyuanid=2, zuid=7, renname=旖旎, gongsiid=4, fatherid=0}
{zuname=新建测试组, renyuanid=1, zuid=7, renname=旖旎, gongsiid=4, fatherid=0}
{zuname=测试组, renyuanid=5, zuid=4, renname=旖旎, gongsiid=4, fatherid=0}
{zuname=测试组, renyuanid=6, zuid=4, renname=旖旎, gongsiid=4, fatherid=0}
{zuname=测试组, renyuanid=3, zuid=4, renname=旖旎, gongsiid=4, fatherid=0}
{zuname=测试组, renyuanid=2, zuid=4, renname=旖旎, gongsiid=4, fatherid=0}
{zuname=测试组, renyuanid=1, zuid=4, renname=旖旎, gongsiid=4, fatherid=0}

不可能整条记录都过滤掉 我要用这些数据生成一个树 结果和QQ好友列表差不多的 一个组有多少人 一共有多少个组

<script language="javascript">
d = new dTree('d','<%=basePath%>','testForm');
d.add('0','-1','我的自定义组');
d.add('7','0','手写测试组');
d.add('2','0','手写测试组2');
d.add('3','7','旖旎');
d.add('4','2','旖旎');
d.add('5','7','旖旎');
d.add('6','2','旖旎');
d.add('3','7','旖旎');
d.add('4','2','旖旎');
d.add('5','7','旖旎');
d.add('6','2','旖旎');
//红色部分是需要用迭代器迭代出来的 怎么也不能一个组里就一个人啊

document.write(d);
</script>
jabeginner 2009-05-07
  • 打赏
  • 举报
回复
我认为也是应该把数据存入到数据库中,然后再进行读取,通过查询就可以自动去掉重复记录
a75386666 2009-05-07
  • 打赏
  • 举报
回复
是啊 搞不清楚这个逻辑了
xnjnmn 2009-05-07
  • 打赏
  • 举报
回复


//循环嵌套有问题想想当s=6的时候p=0,1,2,...fatherlist.size();
//下面的equals判断有意义吗?新建测试组equals测试组
//当然执行esle然后把测试组加了进入了(fatherlist.add(fathermap);)
//还有就是你构造数据出了问题 多用用debug吧 清晰明了
for(int s=0;s <k;s++)

for(int p=0; p <fatherlist.size();p++)
{
//逻辑有问题
if(((fathermap.get("zuname")).toString()).trim().equals((((fatherlist.get(p).get("zuname")).toString()).trim()))==true)
{
// System.out.println("取到map里的的zuname是"+(fathermap.get("zuname")).toString());
// System.out.println("取到list里的的zuname是"+(fatherlist.get(p).get("zuname")).toString());
break;
}else
{
//System.out.println("比对后相等添加记录");
//map 5,6,7,8,9都符合判断 输出了这个咯{fathernodeid=0, zuname=测试组, zuid=4}
fatherlist.add(fathermap);
}
a75386666 2009-05-07
  • 打赏
  • 举报
回复
select a.id as zuid,a.company_id as gongsiid,a.group_name as zuname,b.employee_id as renyuanid,c.employee_name as renname from hrp_custom_group as a inner join hrp_custom_group_details as b on a.id = b.group_id inner join hrp_company_employee_info as c on b.employee_id = c.id where a.company_id=4 这个是sql语句 你看看就知道了
a75386666 2009-05-07
  • 打赏
  • 举报
回复
关键我这是3个表联合查出来的数据 第一个表 里是 id(对应第二个表的组id) 公司id 第二个表里查出来 组id 和人员id(问题就出在这里 只要这个组里有一个人就会读出来一条数据 有十个人就会读出10条数据) 第三个表是是一个人员明细表 根据第二个表的人员id去查第3个表里的人名


我现在想要的结果就是
{fathernodeid=0, zuname=新建测试组, zuid=7}
{fathernodeid=0, zuname=测试组, zuid=4} 有几个组就读出来几个组


{
renyuanid=5, zuid=7, renname=旖旎,
renyuanid=6, zuid=7, renname=旖旎,
renyuanid=3, zuid=7, renname=旖旎,
renyuanid=2, zuid=7, renname=旖旎,
renyuanid=1, zuid=7, renname=旖旎,
renyuanid=5, zuid=4, renname=旖旎,
renyuanid=6, zuid=4, renname=旖旎,
renyuanid=3, zuid=4, renname=旖旎,
renyuanid=2, zuid=4, renname=旖旎,
renyuanid=1, zuid=4, renname=旖旎,
}这是另外一个集合下面这个好说 只要在所有的数据中取3个字段就好了 现在就是取组这里 这里我现在怎么都过滤不掉重复的值
mumu_java 2009-05-07
  • 打赏
  • 举报
回复
lz既然是在模拟数据库中的数据,为什么不把这个问题放到数据库中做呢?我这有个数据库中操作此类问题的例子,你看看。

-- Create table
create table THINK_TEST
(
ID VARCHAR2(50),
NAME VARCHAR2(20),
EMAILADDRESS VARCHAR2(50),
LASTLOGON VARCHAR2(50)
)
-- insert data
insert into think_test values('100','test4','test4@yahoo.cn','2007-11-25 16:31:26 ');
insert into think_test values('13','test1','test1@yahoo.cn','2007-3-22 16:27:07');
insert into think_test values('19','test1','test1@yahoo.cn','2007-10-25 14:13:46');
insert into think_test values('42','test1','test1@yahoo.cn','2007-11-20 14:20:10');
insert into think_test values('45','test2','test2@yahoo.cn','2007-4-25 14:17:39');
insert into think_test values('49','test2','test2@yahoo.cn','2007-5-25 14:22:36 ');
--preview date
select * from think_test;

ID NAME EMAILADDRESS LASTLOGON
-------------------------------------------------- -------------------- -------------------------------------------------- --------------------------------------------------
100 test4 test4@yahoo.cn 2007-11-25 16:31:26
13 test1 test1@yahoo.cn 2007-3-22 16:27:07
19 test1 test1@yahoo.cn 2007-10-25 14:13:46
42 test1 test1@yahoo.cn 2007-11-20 14:20:10
45 test2 test2@yahoo.cn 2007-4-25 14:17:39
49 test2 test2@yahoo.cn 2007-5-25 14:22:36
--execute sql
select *
from(
select t.*,RANK() OVER (PARTITION BY t.name order by rownum desc) Rnk
from think_test t
) tt
where tt.rnk<2;

ID NAME EMAILADDRESS LASTLOGON RNK
-------------------------------------------------- -------------------- -------------------------------------------------- -------------------------------------------------- ----------
42 test1 test1@yahoo.cn 2007-11-20 14:20:10 1
49 test2 test2@yahoo.cn 2007-5-25 14:22:36 1
100


drop table think_test;
a75386666 2009-05-07
  • 打赏
  • 举报
回复
	public List getgroupNameById(String gid) {
int newgid=Integer.parseInt(gid);
String sql ="select a.id as zuid, a.company_id as cid, a.group_name as zuname, a.state as stat from hrp_custom_group as a where `company_id`="+newgid;
List list =jdbctemplate.queryForList(sql);
return list;
}
这是数据层的方法 获得所有组的名称


public List getAllById(String gid) {
// TODO Auto-generated method stub
int ngid = Integer.parseInt(gid);
String sql ="select a.id as zuid,a.group_name as zuname,b.employee_id as renyuanid,c.employee_name as renname from hrp_custom_group as a inner join hrp_custom_group_details as b on a.id = b.group_id inner join hrp_company_employee_info as c on b.employee_id = c.id where a.company_id="+ngid;
List gdlist = jdbctemplate.queryForList(sql);
if(gdlist.size()>0){
return gdlist;
}else{
return null;
}
}


这个是获得所有组里成员的方法
a75386666 2009-05-07
  • 打赏
  • 举报
回复
	public String getall(){		
String gid ="4";
allgds=gds.getAllById(gid);
int k =allgds.size();
groupnamelist = gds.getName(gid);
int w =groupnamelist.size();
List<Map> fatherlist = new ArrayList();
List<Map> childlist =new ArrayList();
for(int s=0;s<w;s++)
{
Map fathermap= new HashMap();
fathermap.clear();
fathermap.put("fathernodeid", 0);
fathermap.put("zuid", ((Map)groupnamelist.get(s)).get("zuid"));
fathermap.put("zuname", ((Map)groupnamelist.get(s)).get("zuname"));
fatherlist.add(fathermap);
}
for(int f=0;f<k;f++)
{
Map childmap= new HashMap();
childmap.put("renyuanid", ((Map)allgds.get(f)).get("renyuanid"));
childmap.put("zuid", ((Map)allgds.get(f)).get("zuid"));
childmap.put("renname", ((Map)allgds.get(f)).get("renname"));
childlist.add(childmap);
}
newfatherlist = fatherlist;
newchildlist = childlist;
if(allgds.size()>0){
return "getallok";
}else{
return "getallno";
}

}
这是action里的代码
a75386666 2009-05-07
  • 打赏
  • 举报
回复
<script language="javascript">
d = new dTree('d','http://localhost:8080/StrutsTest/','testForm');
d.add('0','-1','我的自定义组');

d.add(4,0,'测试组');
d.add(5,0,'测试组');
d.add(7,0,'新建组测试');//上面的3条是第一个迭代出来的

d.add(1,7,'旖旎');//下面这些是第2个循环迭代出来的 查询这2个集合都是根据一个id进行查询而且每个表都是有关联的 所以分2次查询就不回有重复的数据了
d.add(2,7,'可人');
d.add(34,7,'小玩子');
d.add(33,7,'小燕子');
d.add(32,7,'刘洋');
d.add(1,4,'旖旎');
d.add(2,4,'可人');
d.add(3,4,'aa');
d.add(9,4,'小曹');
d.add(10,4,'小张');
document.write(d);
</script>

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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