有1个list,结构如下,利用对象数组取其值,并把有相同id的opr,以“,”连接起来
id opr
1 1
1 2
2 1
打印输出结果如下:
id opr
1 1,2
2 1
Object[] obj = null;
for (Iterator it = list.iterator(); it.hasNext();) {
obj = (Object[]) it.next();
}
java list 分组
答案:5 悬赏:70 手机版
解决时间 2021-04-05 10:55
- 提问者网友:树红树绿
- 2021-04-04 14:37
最佳答案
- 五星知识达人网友:时间的尘埃
- 2021-04-04 15:44
public class Dog {
private String name;
private String id;
public Dog(){
}
public Dog(String id,String name){
this.id= id;
this.name = name;
}
public boolean equals(Object o){
if(this.id == ((Dog)o).id){
return true;
}
return false;
}
public String toString(){
return "id="+this.getId()+",name='"+ this.getName()+"'";
}
}
public class Test {
public static void main(String[] args){
Dog dog1 = new Dog("1","1");
Dog dog2 = new Dog("1","2");
Dog dog3 = new Dog("2","1");
List l = new ArrayList();
l.add(dog1);
l.add(dog2);
l.add(dog3);
List l2 = new ArrayList();
boolean a = true;
for(Dog dog : l){
a = true;
for(Dog dog_temp : l2){
if(dog_temp.equals(dog)){
dog_temp.setName(dog_temp.getName()+","+dog.getName());
a = false;
}
}
if(a == true){
l2.add(dog);
}
}
System.out.println(l2);
}
}
打印结果[id=1,name='1,2', id=2,name='1'] 这个是你想要的么??
private String name;
private String id;
public Dog(){
}
public Dog(String id,String name){
this.id= id;
this.name = name;
}
public boolean equals(Object o){
if(this.id == ((Dog)o).id){
return true;
}
return false;
}
public String toString(){
return "id="+this.getId()+",name='"+ this.getName()+"'";
}
}
public class Test {
public static void main(String[] args){
Dog dog1 = new Dog("1","1");
Dog dog2 = new Dog("1","2");
Dog dog3 = new Dog("2","1");
List
l.add(dog1);
l.add(dog2);
l.add(dog3);
List
boolean a = true;
for(Dog dog : l){
a = true;
for(Dog dog_temp : l2){
if(dog_temp.equals(dog)){
dog_temp.setName(dog_temp.getName()+","+dog.getName());
a = false;
}
}
if(a == true){
l2.add(dog);
}
}
System.out.println(l2);
}
}
打印结果[id=1,name='1,2', id=2,name='1'] 这个是你想要的么??
全部回答
- 1楼网友:你哪知我潦倒为你
- 2021-04-04 20:11
使用set集合啊,set集合是不能放重复的数据的。
set hashset = new hashset(list);
然后在转过来就行了,set去除重复是调用了equals方法,所以你在sh里面要重写equals方法,自定义比较的规则,例如:
public boolean equals(object obj) {
if(obj==null) return false;
if(obj==this) return true;
sh sh = (sh)obj;
if(sh.gethm()==this.gethm()){
return true;
}
return false;
}
- 2楼网友:孤独的牧羊人
- 2021-04-04 19:31
你好,我想问一下,List里可以有重复的索引吗?你里面有了2个id = 1 的,这样可以吗,这个我不清楚,问下
- 3楼网友:街头电车
- 2021-04-04 18:17
1.需要用到两个for循环
2.拿外层for循环取出来的值和内层for循环取出来的id依次对比,如果相同,咋把后一个值放到opr中
代码你自己动手吧
- 4楼网友:毛毛
- 2021-04-04 17:22
public static String[] compareArray(String[] strs_a,String[] strs_b){
if(strs_a == null) return strs_b;
if(strs_b == null) return strs_a;
if(Arrays.equals(strs_a, strs_b)){
return null;
}
String tempStr = "";
if(strs_a[0].equals(strs_b[0])){
for(int i=0;i getSortResult(List list){
List resultList = new ArrayList();
if(list!=null && list.size()>0){
for(int i=0;i list = new ArrayList();
list.add(new String[]{"1","1"});
list.add(new String[]{"1","2"});
list.add(new String[]{"2","1"});
List rlist = getSortResult(list);
for(String[] items:rlist){
System.out.println();
for(int i=0;i
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯