Java集合学习
Posted 艾白羊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java集合学习相关的知识,希望对你有一定的参考价值。
一、集合的分类:
二、常用的集合:
1、Java collection:Jdk中的集合
1、List
//List List<String> list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("c"); System.out.println(list); // [a, b, c]
2、Map
//Map Map<String,String> map = new HashMap<>(); map.put("name","by"); map.put("age","18"); System.out.println(map); // {name=by, age=18}
3、Set
//Set Set<String> set = new HashSet<>(); set.add("a"); set.add("b"); System.out.println(set); // [a, b]
4、Iterator遍历集合
//Iterator遍历List集合 Iterator iterator = list.iterator(); while (iterator.hasNext()){ String parm = (String) iterator.next(); System.out.println(parm); if(parm.equals("a")){ iterator.remove(); } } System.out.println(list); // [b, c]
5、遍历MAP
//Map遍历比较,迭代器遍历比增强for循环遍历效率高,使用(方法四)遍历效率最高
Map<String, String> mapdemo = new HashMap<>(); mapdemo.put("name", "by"); mapdemo.put("age", "18"); mapdemo.put("love", "reading"); mapdemo.put("girlfriend", "marry"); //增强for循环遍历 //法一:使用keySet()遍历 for (String key : mapdemo.keySet()) { System.out.println(key + ":" + mapdemo.get(key)); } //法二:使用entrySet()遍历 for (Map.Entry<String, String> entry : mapdemo.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } //迭代器遍历 //法三:使用keySet()遍历 Iterator<String> ite = mapdemo.keySet().iterator(); while (ite.hasNext()) { String key = ite.next(); System.out.println(key + " :" + mapdemo.get(key)); } //法四:使用entrySet()遍历 Iterator<Map.Entry<String, String>> iter = mapdemo.entrySet().iterator(); while (iter.hasNext()) { Map.Entry<String, String> entry = iter.next(); System.out.println(entry.getKey() + " :" + entry.getValue()); }
2、Guava Collections(google开源工具 )
1、引入依赖:
<!-- https://mvnrepository.com/artifact/com.google.guava/guava --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>21.0</version> </dependency>
2、示例:
2.1 List
//创建List List<String> list = Lists.newArrayList("a","b","c"); list.add("d"); //反转List System.out.println(Lists.reverse(list)); // [d, c, b, a] //将List集合转换为特定规则的字符串 String listResult = Joiner.on("-").join(list); System.out.println(listResult); // a-b-c-d
2.2 Map
//定义Map Map<String,String > map = Maps.newHashMap(); map.put("name","by"); map.put("age","23"); System.out.println(map); //{name=by, age=23} //将Map集合转换为特定规则的字符串 String mapResult = Joiner.on(",").withKeyValueSeparator("=").join(map); System.out.println(mapResult); // name=by,age=23 //定义Map中放List的形式(Map<String,List<Integer>>) Multimap<String,Integer> maps = ArrayListMultimap.create(); maps.put("map",1); maps.put("map",2); System.out.println(maps); //{map=[1, 2]}
2.3 Set
//定义Set Set<String> set = Sets.newHashSet(); set.add("value");
3、Trove
1、引入依赖:
<!-- https://mvnrepository.com/artifact/net.sf.trove4j/trove4j --> <dependency> <groupId>net.sf.trove4j</groupId> <artifactId>trove4j</artifactId> <version>3.0.3</version> </dependency>
2、示例:
2.1 构造基本类型的集合
//直接构造int类型的集合 TIntArrayList intList = new TIntArrayList(); intList.add(1); intList.add(2); intList.add(3); intList.reverse(); System.out.println(intList);
4、Apache commons collections
1、引入依赖:
<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections --> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency>
2、示例:
2.1. 集合的并集、交集、差集
User user = new User(); user.setId(1l); user.setName("by"); User user1 = new User(); user1.setId(2l); user1.setName("ly"); User user2 = new User(); user2.setId(3l); user2.setName("hy"); List<User> list = new ArrayList<>(); list.add(user); list.add(user1); List<User> list1 = new ArrayList<>(); list1.add(user); // 并集 Collection<User> unionList = CollectionUtils.union(list, list1); System.out.println(unionList); // [User(id=1, name=by, alimony=null), User(id=2, name=ly, alimony=null)] // 交集 Collection<User> intersectionList = CollectionUtils.intersection(list, list1); System.out.println(intersectionList); // [User(id=1, name=by, alimony=null)] // 集合相减 Collection<User> subtractList = CollectionUtils.subtract(list, list1); System.out.println(subtractList); // [User(id=2, name=ly, alimony=null)]
以上是关于Java集合学习的主要内容,如果未能解决你的问题,请参考以下文章
201621123037 《Java程序设计》第9周学习总结
201621123057 《Java程序设计》第9周学习总结