List的一些用法

来源:互联网 发布:零起点学通c语言iso 编辑:程序博客网 时间:2024/06/14 08:22

1.从一个List取出另一个List不包含的元素;

demo如下:

public class Test {    public static void main(String[] args) {        List<String> blanklist = new ArrayList<>();        blanklist.add("1");        blanklist.add("2");        blanklist.add("3");        blanklist.add("4");        blanklist.add("5");        List<String> newTelephones = new ArrayList<>();        newTelephones.add("2");        newTelephones.add("3");        newTelephones.add("6");        newTelephones.add("7");        newTelephones.add("8");        newTelephones.add("9");        newTelephones.removeAll(blanklist);        System.out.println(newTelephones);    }}

运行结果:

[6, 7, 8, 9]


2.取出两个List的交集和两个List不同的元素;

demo如下:

public class Test {    public static void main(String[] args) {        Long[] id ={1L,2L,3L,4L,5L,6L};        Long[] ids ={1L,2L,7L,8L,9L,10L};        //Arrays.asList(id)把数组转出List集合        List<Long> oldUserId = new ArrayList<>(Arrays.asList(id));        List<Long> newUserId = new ArrayList<>(Arrays.asList(ids));        List<Long> intersectionUserId = new ArrayList<>();        intersectionUserId.addAll(oldUserId);        //1.2 取出交集        intersectionUserId.retainAll(newUserId);        System.out.println("交集"+intersectionUserId);        oldUserId.removeAll(intersectionUserId);  //需要删除的信息        System.out.println("oldUserId去除交集元素的集合"+oldUserId);        newUserId.removeAll(intersectionUserId);  //需要添加的数据        System.out.println("newUserId去除交集元素的集合"+newUserId);    }}
运行结果:
交集[1, 2]oldUserId去除交集元素的集合[3, 4, 5, 6]newUserId去除交集元素的集合[7, 8, 9, 10]
3.判断List是否为空
3.1)直接创建一个数组
public class Test3 {    public static void main(String[] args) {        List<Long> list = new LinkedList<>();        System.out.println(list);        System.out.println(list.size());        System.out.println(list.isEmpty());        System.out.println(list == null);    }}
运行结果:
[]0truefalse
3.2)从数据库查询出一个数组
数据库数据:
请求路径:(GET请求)
http://localhost:8080/test-api/test/select
代码:
@Controller@RequestMapping("test-api/test")public class TestController {    private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);    @Autowired    private CommentaryProjectService commentaryProjectService;    @Autowired    private CommentaryUserPapersService commentaryUserPapersService;    @RequestMapping(value = "select", method = RequestMethod.GET)    @ResponseBody    public ResultTO select() {        Map<String,Object> map = new HashMap<>();        List<CommentaryProject> list = commentaryProjectService.selectObjectList(map);        try {            System.out.println(list);        } catch (Exception e) {            return ResultTO.newFailResultTO("查询失败", null);        }        return ResultTO.newSuccessResultTO("查询成功", list);    }
}
运行结果:
[]
@Controller@RequestMapping("test-api/test")public class TestController {    private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);    @Autowired    private CommentaryProjectService commentaryProjectService;    @Autowired    private CommentaryUserPapersService commentaryUserPapersService;    @RequestMapping(value = "select", method = RequestMethod.GET)    @ResponseBody    public ResultTO select() {        Map<String,Object> map = new HashMap<>();        List<CommentaryProject> list = null;        try {            list = commentaryProjectService.selectObjectList(map);            System.out.println(list);        } catch (Exception e) {            return ResultTO.newFailResultTO("查询失败", null);        }        return ResultTO.newSuccessResultTO("查询成功", list);    }
}
运行结果:
[]
总结:如果在查询之前抛出一个异常,则运行结果为:null
4.List的一些方法使用:
public class Test3 {    public static void main(String[] args) {List<String> list = new LinkedList<>();        list.add("xiaoming");        list.add("xiaohong");        list.add("xiaohua");        list.add("xiaobai");        list.add("xiaoli");        list.add("xiaofei");        System.out.println(list);      //运行结果: [xiaoming, xiaohong, xiaohua, xiaobai, xiaoli, xiaofei](数组)        String string = list.toString();        System.out.println(string);    //运行结果: [xiaoming, xiaohong, xiaohua, xiaobai, xiaoli, xiaofei](字符串)        String substring = string.substring(1, string.length() - 1);        System.out.println(substring);  //运行结果: xiaoming, xiaohong, xiaohua, xiaobai, xiaoli, xiaofei(字符串)        String replace = substring.replace(",", "");        System.out.println(replace);    //运行结果: xiaoming xiaohong xiaohua xiaobai xiaoli xiaofei(字符串)        System.out.println(replace.toString()); //运行结果: xiaoming xiaohong xiaohua xiaobai xiaoli xiaofei(字符串)        String s = NetDateUtil.dateToString(new Date(), NetDateUtil.DATE_PATTERN);        System.out.println(s); //运行结果: 20171109101429}}




原创粉丝点击