• JAVA List合并集合


    import java.util.ArrayList;
    import java.util.List;
    
    public class test {
          public static void main(String[] args) throws Exception {
              /*测试合并两个类型相同的list*/
              List<String> list1 = new ArrayList<String>();
              List<String> list2 = new ArrayList<String>();
              //给list1赋值
              list1.add("测");
              list1.add("试");
              list1.add("一");
              list1.add("下");
              //给list2赋值
              list2.add("合");
              list2.add("并");
              list2.add("列");
              list2.add("表");
              //将list1.list2合并
              list1.addAll(list2);
              //循环输出list1 看看结果
              for (String s : list1) {
                  System.out.print(s);
              }
          }
    }
    需求:两个字符串合并(如果想去重复,参考下一篇--数组去重复及记录重复个数)
    //方法一 Arrays类
        String[] a = {"A","B","C"};
        String[] b = {"D","E"};
        // List<String> list = Arrays.asList(a);   --OK
        // List<String> list = Arrays.asList("A","B","C");  --OK
        // list.add("F");  --UnsupportedOperationException
        // list.remove("A"); --UnsupportedOperationException
        // list.set(1,"javaee");--OK (因为是把数组转为集合,其本质还是数组,数组长度固定不变,但内容可以改变)
        // 结论:虽然可以把数组转为集合,但是集合长度不能改变
    
        List list = new ArrayList(Arrays.asList(a));
        list.addAll(Arrays.asList(b));
        String[] str = new String[list.size()];
        list.toArray(str);    
        for(int x=0;x<str.length;x++){
            System.out.print(str[x] + " ");
        }
    //方法二  循环遍历
    // 两个数组合并
        String[] str1 = {"Hello","world","java"};
        String[] str2 = {"Veriable","syntax","interator"};
        String[] newStr = new String[str1.length+str2.length];
        //newStr = str1;数组是引用类型
        for(int x=0;x<str1.length;x++){
            newStr[x] = str1[x];
        }        
        for(int y=0;y<str2.length;y++){
            newStr[str1.length+y]=str2[y];
        }
        for(int y=0;y<newStr.length;y++){
            System.out.println(newStr[y] + " ");
        }
    // 方法三
        String[] str1 = {"Hello","world","java"};
        String[] str2 = {"Veriable","syntax","interator"};
        int str1Length = str1.length;
        int str2length = str2.length;
            
        str1 = Arrays.copyOf(str1, str1Length+str2length);//数组扩容
        System.arraycopy(str2, 0, str1, str1Length, str2length);
        System.out.println(Arrays.toString(str1));
    java求两个集合的交集和并集
    
            java如何求两个集合的交集和并集呢??其实java的API中已经封装了方法。今天写个简单的例子测试一下:(例子中以java.util.LinkedList为例)
    
            求连个集合的交集:
    
    import java.util.ArrayList;
    import java.util.List;
    public class TestCollection {
        public static void main(String[] args) {
            List<String> strList = new ArrayList<String>();
            List<String> strList2 = new ArrayList<String>();
            for(int i = 0; i < 10; i ++) {
                strList.add("aaa>>" + i);
                strList2.add("aaa>>" + (10 - i));
            }
             
            //求出交集
            strList2.retainAll(strList);
            System.out.println("交集大小:" + strList2.size());
             
            for(int i = 0; i < strList2.size(); i++) {
                System.out.println(strList2.get(i));
            }       
        }
    }
    
    
    
    
    求两个集合的并集:
    import java.util.ArrayList;
    import java.util.List;
    public class TestCollection {
        public static void main(String[] args) {
            List<String> strList = new ArrayList<String>();
            List<String> strList2 = new ArrayList<String>();
            for(int i = 0; i < 10; i ++) {
                strList.add("aaa>>" + i);
                strList2.add("aaa>>" + (10 - i));
            }
            //求出并集
            strList2.removeAll(strList);
            strList2.addAll(strList);
            System.out.println("并集大小:" + strList2.size());      
             
            for(int i = 0; i < strList2.size(); i++) {
                System.out.println(strList2.get(i));
            }       
        }
    }
    java 使用map返回多个对象组装
    
     Object json=JSONObject.fromObject("{}");
    List<Object> list = new ArrayList<Object>();
    list.add("1");
    list.add("2");
    list.add("3");
    list.add("4");
    list.add("5");
    list.add("6");
    List<Object> result = new ArrayList<Object>();
    Map<String, Object> map = new HashMap<String, Object>();
    for(Object str :list){
        map.put("name", str);
        map.put("age", str);
        result.add(map);
    }
    JSONArray arr=JSONArray.fromObject(result);
    if(arr!=null){
        json=arr;
    }
    System.out.println(arr);
      输出值格式为:[{"age":"6","name":"6"},{"age":"6","name":"6"},{"age":"6","name":"6"},{"age":"6","name":"6"},{"age":"6","name":"6"},{"age":"6","name":"6"}]
  • 相关阅读:
    zhaopin001
    js中获取当前时间毫秒数
    td标签设置background属性时,最好选择在style中设置
    只选择年份的下来菜单
    a标签中的href调用js对话框
    VS2010引用Microsoft.Office.Interop生成时出错
    VS调试“附加到进程”时有多个相同w3wp.exe时不知选择哪一个的解决方法
    使用BAT批处理执行sql
    Multiple outputs from T4 made easy revisited (T4 输出多个文件)
    PowerDesigner 反向工程生成ER图
  • 原文地址:https://www.cnblogs.com/Luouy/p/6482733.html
Copyright © 2020-2023  润新知