精华内容
下载资源
问答
  • 需求陈述 ... 根据判断对象中的username字段和village字段是否重复,将其取出分类存在不同的集合中。 具体实现 import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import

    需求陈述

    在一个list集合中 如: ArrayList< AgreementRecord > arrayList = new ArrayList<>();根据判断对象中的username字段和village字段是否重复,将其取出分类存在不同的集合中。

    具体实现

    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.UUID;
    
    import com.immigrant.model.AgreementRecord;
    
    public class FindDuplicate {
        public static void main(String[] args) {
    
            ArrayList<AgreementRecord> arrayList = new ArrayList<>();
            for (int i = 0; i < 2; i++) {
                AgreementRecord agreementRecord = new AgreementRecord("网易", "农村");
                arrayList.add(agreementRecord);
            }
            for (int i = 0; i < 3; i++) {
                AgreementRecord agreementRecord = new AgreementRecord("百度", "北上广");
                arrayList.add(agreementRecord);
            }
            AgreementRecord agreementRecord = new AgreementRecord("新浪", "居委会");
            AgreementRecord agreementRecord111 = new AgreementRecord("新浪", "居委会");
            AgreementRecord agreementRecord10 = new AgreementRecord("新浪", "居");
            AgreementRecord agreementRecord2 = new AgreementRecord("新浪", "会");
            arrayList.add(agreementRecord);
            arrayList.add(agreementRecord111);
            arrayList.add(agreementRecord10);
            arrayList.add(agreementRecord2);
            int k = 0;
            for (AgreementRecord agreementRecord11 : arrayList) {
    
                System.out.println(agreementRecord11 + "==========" + k);
                k++;
            }
    
            Map<String, List<AgreementRecord>> map = new HashMap<String, List<AgreementRecord>>();
            for (AgreementRecord agreementRecord1 : arrayList) {
                if (map.containsKey(agreementRecord1.getUsername())) {
                    List<AgreementRecord> rList = map.get(agreementRecord1.getUsername());
                    rList.add(agreementRecord1);
                } else {
                    List<AgreementRecord> rList = new ArrayList<AgreementRecord>();
                    rList.add(agreementRecord1);
                    map.put(agreementRecord1.getUsername(), rList);
                }
            }
            //System.out.println(map.toString());
            Map<String, List<AgreementRecord>> mm = new HashMap<String, List<AgreementRecord>>();
            for (String key : map.keySet()) {
                Map<String, List<AgreementRecord>> m = new HashMap<String, List<AgreementRecord>>();
                for (AgreementRecord agreementRecord1 : map.get(key)) {
                    if (m.containsKey(agreementRecord1.getVillage())) {
                        List<AgreementRecord> rList = m.get(agreementRecord1.getVillage());
                        rList.add(agreementRecord1);
                    } else {
                        List<AgreementRecord> rList = new ArrayList<AgreementRecord>();
                        rList.add(agreementRecord1);
                        m.put(agreementRecord1.getVillage(), rList);
                    }
                }
                for (String agreementRecord1 : m.keySet()) {
                    UUID uuid = UUID.randomUUID();
                    String str = uuid.toString();
                    String uuidStr = str.replace("-", "");
                    mm.put(uuidStr, m.get(agreementRecord1));
                    //System.out.println(agreementRecord1+":             "+m.get(agreementRecord1));
                }
            }
    
            for (String agreementRecord1 : mm.keySet()) {
                System.out.println(agreementRecord1+":             "+mm.get(agreementRecord1));
    
            }
        }
    }
    

    控制台输出:

    AgreementRecord [username=网易, village=农村]==========0
    AgreementRecord [username=网易, village=农村]==========1
    AgreementRecord [username=百度, village=北上广]==========2
    AgreementRecord [username=百度, village=北上广]==========3
    AgreementRecord [username=百度, village=北上广]==========4
    AgreementRecord [username=新浪, village=居委会]==========5
    AgreementRecord [username=新浪, village=居委会]==========6
    AgreementRecord [username=新浪, village=居]==========7
    AgreementRecord [username=新浪, village=会]==========8
    
    d3b4252d5aa7451da6573a11436c625d:   
    [AgreementRecord [username=新浪, village=居委会], 
    AgreementRecord [username=新浪, village=居委会]]
    
    61bc3c9a1e6f4f198a2cd5ae47bce839:             
    [AgreementRecord [username=网易, village=农村], 
    AgreementRecord [username=网易, village=农村]]
    
    c7d1865166ec41779c979428bd06c552:             
    [AgreementRecord [username=新浪, village=会]]
    
    0c058e210649411590820e27d51e0659:             
    [AgreementRecord [username=百度, village=北上广], 
    AgreementRecord [username=百度, village=北上广], 
    AgreementRecord [username=百度, village=北上广]]
    
    e670ecde43b440aa930c732b84c9f2d0:             
    [AgreementRecord [username=新浪, village=居]]

    遗留问题

    目前代码仅仅只是实现了需求,但是代码写的不够简洁,如果涉及判断更多的字段的话那么冗余的代码就会更多。所以在此希望诸位高手能帮忙提出更好的方法。

    后期优化

    package com.immigrant.util;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    
    import com.immigrant.model.AgreementRecord;
    
    public class demo {
        public static void main(String[] args) {
            ArrayList<AgreementRecord> arrayList = new ArrayList<>();
            for (int i = 0; i < 2; i++) {
                AgreementRecord agreementRecord = new AgreementRecord("网易", "农村");
                arrayList.add(agreementRecord);
            }
            for (int i = 0; i < 3; i++) {
                AgreementRecord agreementRecord = new AgreementRecord("百度", "北上广");
                arrayList.add(agreementRecord);
            }
            AgreementRecord agreementRecord = new AgreementRecord("新浪", "居委会");
            AgreementRecord agreementRecord111 = new AgreementRecord("新浪", "居委会");
            AgreementRecord agreementRecord10 = new AgreementRecord("新浪", "居");
            AgreementRecord agreementRecord2 = new AgreementRecord("新浪", "会");
            arrayList.add(agreementRecord);
            arrayList.add(agreementRecord111);
            arrayList.add(agreementRecord10);
            arrayList.add(agreementRecord2);
    
            Collections.sort(arrayList, new Comparator<AgreementRecord>(){
                @Override
                public int compare(AgreementRecord o1, AgreementRecord o2) {
                    int village = o1.getVillage().hashCode() - o2.getVillage().hashCode();
                    int username = o1.getUsername().hashCode() - o2.getUsername().hashCode();
                    return village+username;
                }
            });
            ArrayList<AgreementRecord> tList = new ArrayList<>();
            Map<String, List<AgreementRecord>> usernameMap = new HashMap<String, List<AgreementRecord>>();
            for(AgreementRecord a:arrayList){
                tList.add(a);
                if(!tList.get(0).getUsername().equals(a.getUsername()) || !tList.get(0).getVillage().equals(a.getVillage())){
                    tList.remove(a);
                    usernameMap.put(tList.get(0).getVillage(), tList);
                    tList = new ArrayList<AgreementRecord>();
                    tList.add(a);
                }
                usernameMap.put(tList.get(0).getVillage(), tList);
            }
            //遍历map集合输出元素
            for (String agreementRecord1 : usernameMap.keySet()) {
                System.out.println(agreementRecord1+":             "+usernameMap.get(agreementRecord1));
            }
    
        }
    }
    
    展开全文
  • 部分代码: // 分页 PageHelper.startPage(tracePageDTO.getPageNum(),tracePageDTO....List<TraceGoodsVO> traceGoodsVOS = traceMapper.getTraceGoodsList(tracePageDTO); SimpleDateFormat sdf = n...

    部分代码:

    // 分页
    PageHelper.startPage(tracePageDTO.getPageNum(),tracePageDTO.getPageSize());
    List<TraceGoodsVO> traceGoodsVOS = traceMapper.getTraceGoodsList(tracePageDTO);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Map<String,List<TraceGoodsVO>> map = new HashMap<>();
    for (TraceGoodsVO traceGoodsVO :traceGoodsVOS){
        // 将日期转换为年月日格式
        String time = sdf.format(DateUtil.convert2Date(traceGoodsVO.getCreateTime(),"yyyy-MM-dd"));
        // 判断当前值是否已经存在map中
        if(map.containsKey(time)){
            List<TraceGoodsVO> goodsVOS = map.get(time);
            // 对象拷贝
            TraceGoodsVO goodsVO = new TraceGoodsVO();
            BeanUtils.copyProperties(traceGoodsVO,goodsVO);
            goodsVOS.add(goodsVO);
            map.put(time,goodsVOS);
        }else {
            List<TraceGoodsVO> newGoodsVOS = new ArrayList<TraceGoodsVO>();
            TraceGoodsVO goodsVOTarget = new TraceGoodsVO();
            BeanUtil.copyProperties(traceGoodsVO,goodsVOTarget);
           newGoodsVOS.add(goodsVOTarget);
            map.put(time,newGoodsVOS);
        }
    }
    //取出所有的重复的数据值,存入到maps中
    List<Map<String,List<TraceGoodsVO>>> maps = new ArrayList<>();
    for (Map.Entry<String, List<TraceGoodsVO>> entry : map.entrySet()) {
        Map<String,List<TraceGoodsVO>> map1 = new HashMap<>();
        map1.put(entry.getKey(),entry.getValue());
        maps.add(map1);
    }
    PageInfo<Map<String,List<TraceGoodsVO>>> info = new PageInfo<Map<String,List<TraceGoodsVO>>>(maps);
    return PageUtils.getPageResult(info);

     

    展开全文
  • 今天在做金碟系统与我们系统的对接的时候需要做一个客户同步 在同步时,需要比较对象,对查询出的数据库的数据进行比较 for(int i=0;... //把对应的对象加入到返回的结果集List中去就可以了 ...

    今天在做金碟系统与我们系统的对接的时候需要做一个客户同步

    在同步时,需要比较对象,对查询出的数据库的数据进行比较

    for(int i=0;i<list2.size();i++){
                if(! list1.contains(list2.get(i))){
                    //把对应的对象加入到返回的结果集List中去就可以了
                    List l = new ArrayList();
                    l.add(list2);
                    System.out.println(l.toString());
                 }
            }

    但是返回值总是false,在网上看到答案:需要在entity中重写equals方法:

        ArrayList的contains方法
                java
    今天在用ArrayList类的caontains方法是遇到了问题,我写了一个存放Student类的ArrayList  但在调用list.contains(user)时总是返回false。 去看了下ArrayList的源码,源码如下:
            Java代码
    public boolean contains(Object o) {
            return indexOf(o) >= 0;
            }
    
    
    public int indexOf(Object o) {
            if (o == null) {
            for (int i = 0; i < size; i++)
            if (elementData[i]==null)
            return i;
            } else {
            for (int i = 0; i < size; i++)
            if (o.equals(elementData[i]))
            return i;
            }
            return -1;
            }
            [java] view plaincopy
    public boolean contains(Object o) {
            return indexOf(o) >= 0;
            }
    
    
    public int indexOf(Object o) {
            if (o == null) {
            for (int i = 0; i < size; i++)
            if (elementData[i]==null)
            return i;
            } else {
            for (int i = 0; i < size; i++)
            if (o.equals(elementData[i]))
            return i;
            }
            return -1;
            }
    
    
            发现在contains方法会调用 o.equals(elementData[i])方法,其中elementData[i]是个object类的实例。也就是说我在调用list.contains(user)时实际上比较的是user.equals(object) 这当然会返回false。
    
            正确的方法,在Student类中改写equals方法。 改写的代码如下:
            Java代码
    public boolean equals(Object obj) {
            if (obj instanceof Student) {
            Student u = (Student) obj;
            return this.name.equals(u.name)
            && this.id.equals(id);
            }
            return super.equals(obj);

     

    将需要对比的数据写到equals方法中,没写进去不比较,其他字段不同contains还是返回true,像这样:

    Entity:

    public class Student {
        private Integer id;
        private String name;
        private Integer isBack;
    // ......
        public boolean equals(Object obj) {   
            if (obj instanceof Student) {   
                Student u = (Student) obj;   
                return this.name.equals(u.name)   
                        && this.id.equals(id);   
            }   
            return super.equals(obj);
            }
    }
        public static void main(String[] args) {
            List<Student> list1 = new ArrayList<Student>();
            
            Student s = new Student();
            s.setId(1);
            s.setName("1");
            list1.add(s);
            
            List<Student> list2 = new ArrayList<Student>();
            Student student2 = new Student();
            student2.setId(1);
            student2.setName("1");
            student2.setIsBack(1);
            list2.add(student2);
            
            Student student3 = new Student();
            student3.setId(1);
            student3.setName("22");
            student3.setIsBack(213123123);
            list2.add(student3);
            
            for(int i=0;i<list2.size();i++){
                if(! list1.contains(list2.get(i))){
                    //把对应的对象加入到返回的结果集List中去就可以了
                    List l = new ArrayList();
                    l.add(list2);
                    System.out.println(l.toString());
                 }
            }
        }

     但是在重写equals方法的时候,如果一个值为null,就会报空指针异常,所以可以在重写equals方法的时候将equals改为 == 

    public boolean equals(Object obj) {   
            if (obj instanceof Student) {   
                Student u = (Student) obj;   
                return this.name==(u.name)   
                        && this.id==(id);   
            }   
            return super.equals(obj);
            }

     执行效果:

    public static void main(String[] args) {
            List<Student> list1 = new ArrayList<Student>();
            
            Student s = new Student();
            s.setId(1);
            s.setName("1");
            list1.add(s);
            
            List<Student> list2 = new ArrayList<Student>();
            Student student2 = new Student();
            student2.setId(1);
            student2.setIsBack(1);
            list2.add(student2);
            
            Student student3 = new Student();
            student3.setId(1);
            student3.setName("111");
            student3.setIsBack(213123123);
            list2.add(student3);
            
            for(int i=0;i<list2.size();i++){
                if(! list1.contains(list2.get(i))){
                    //把对应的对象加入到返回的结果集List中去就可以了
                    List l = new ArrayList();
                    l.add(list2);
                    System.out.println(l.toString());
                 }
            }
        }

     

    转载于:https://www.cnblogs.com/huanghuanghui/p/9524810.html

    展开全文
  • //查出對象List集合 List groups = getGroupByUserId(contrastUser); //放進String-List集合中 List groupIds = groups.stream().map(GroupDO::getId).collect(Collectors.toList());

    //查出對象的List集合
    List groups = getGroupByUserId(contrastUser);
    //放進String-List集合中
    List groupIds = groups.stream().map(GroupDO::getId).collect(Collectors.toList());

    展开全文
  • 昨天开发的时候遇到一个需求,需要在两个不同的List对象取出所有不同的对象集合,大家看的时候可能会觉得绕口,下面简单举例说明: 对象 Student  属性 id,name 两个集合 1、List lists1 = new ArrayList...
  • EmployeeMapper mapper = sqlSession.getMapper... //取出上面集合中对象Employee值Empid List<Integer> empids = emps.stream().map(Employee -> Employee.getEmpid()).collect(Collectors.
  • 实际场景中,我们有时需要根据自定义规则对List集合中的元素进行排序,怎么实现呢? 我们可以通过实现Comparator接口来自定义排序规则.实现它的public int compara(Object o1, Object o2)方法,然后使用Conllections的...
  • public List<DifferenceWapper> comparePosInfoVo(List<PosInfoVo> first,List<PosInfoVo> second, List<DifferenceWapper> list){ HashMap, Integer> firstHashMap = new HashMap(); if (null!=first &&!first....
  • toList ( ) ) ; 贴上代码 package com . cmai . lambda ; import com . alibaba . fastjson . JSON ; import java . util . ArrayList ; import java . util . List ; import java . util . ...
  • Java 8 工作中常用到list的基本应用 group by 分组 max public void test(){ List<Student>inquiryFlowSheetDistributor=new ArrayList<Student>(); /** 根据id分组 **/ Map<String, ...
  • 先说下Contains(List的一个方法) public abstract booleancontains(Objectobject) 测试它是否List包含指定的对象...那我们就很容易想到使用contains来对两个List集合进行判断 例如: public void addList...
  • 今天遇到个业务代码中需要将一个集合对象中的某个属性抽出来成为一个集合,并取出最大值。(看代码) 这里用到几个api java.util.Collections java.util.stream.Collectors List<LdbhdjDTO> dtoList = ...
  • List对象集合中取出某个字段集合 list.stream().map(p -> p.getName()).collect(Collectors.toList()); spring util 判空 CollectionUtils.isEmpty() # 判断list是否为空 ObjectUtils.isEmpty() # 判断...
  • List<Student> list = Arrays.asList(new Student(1l, "one"), new Student(2l, "two"), new Student(3l, ... //从list集合中取出字段name列表 List<String> names = list.stream().map(p -> ...
  • public class FeildTest { public static void main(String[] args) { //定义list集合 List<P> list = Arrays.asList(new P(1,... //从list集合中取出字段name列表 List<String> names = list.str
  • eg:取出OrgStripe中的orgId //查询出条线机构关联数据 List<OrgStripe> byStripeId = orgStripeService.findByStripeId(stripe.getId()); if (CollectionUtils.isNotEmpty(byStripeId)){ //得到所有与该条线...
  • EL表达式取出dto对象中的集合

    千次阅读 2012-01-04 15:51:59
    用El表达式将对象里的集合中的对象取出来。 首先java类代码如下: public class TestDto { private String name; private List testList; private Map testMap; public String getName() { return name; }...
  • import java.util.Arrays; import java.util.List; import java.util.stream.Collectors;... //定义list集合 List<Dome> list = Arrays.asList(new Dome(1, "老大"), new Dome(2, "老二"), new Dome(3, "老三..
  • 在开发过程中 我们经常会遇到需要从一个List中取出一个属性的List集合 比如 id的集合 所以就会用到JDK8中的方法, 有个疑问,当我们取值的时候 是否会碰到空指针呢 ? 答案是:不会 但是也会有另外一个问题 上例子 ...
  • Map 需要注意是: toMap 如果集合对象有重复key,会报错Duplicate key .... apple1,apple12id都为1。 可以用 (k1,k2)->k1 来设置,如果有重复key,则保留key1,舍弃key2 List<Entity> list = new ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 501
精华内容 200
关键字:

取出list集合中的对象