Java集合学习

一、集合的分类:

Java集合学习_第1张图片

 

二、常用的集合:

 

1、Java collection:Jdk中的集合

1、List

//List
List  list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
System.out.println(list); // [a, b, c]

 

2、Map

//Map
Map  map = new HashMap<>();
map.put("name","by");
map.put("age","18");
System.out.println(map); // {name=by, age=18}

 

3、Set

//Set
Set 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 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 entry : mapdemo.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } //迭代器遍历 //法三:使用keySet()遍历 Iterator ite = mapdemo.keySet().iterator(); while (ite.hasNext()) { String key = ite.next(); System.out.println(key + " :" + mapdemo.get(key)); } //法四:使用entrySet()遍历 Iterator> iter = mapdemo.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = iter.next(); System.out.println(entry.getKey() + " :" + entry.getValue()); }

 

2、Guava Collections(google开源工具 )

1、引入依赖:



    com.google.guava
    guava
    21.0

 

2、示例:

 

2.1 List

//创建List
List 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 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>)
Multimap maps = ArrayListMultimap.create();
maps.put("map",1);
maps.put("map",2);
System.out.println(maps); //{map=[1, 2]}

 

2.3 Set

//定义Set
Set set = Sets.newHashSet();
set.add("value");

 

3、Trove

1、引入依赖:



    net.sf.trove4j
    trove4j
    3.0.3

 

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、引入依赖:



    commons-collections
    commons-collections
    3.2.1

 

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 list = new ArrayList<>();
list.add(user);
list.add(user1);

List list1 = new ArrayList<>();
list1.add(user);

// 并集
Collection unionList = CollectionUtils.union(list, list1);
System.out.println(unionList); // [User(id=1, name=by, alimony=null), User(id=2, name=ly, alimony=null)]
// 交集
Collection intersectionList = CollectionUtils.intersection(list, list1);
System.out.println(intersectionList); // [User(id=1, name=by, alimony=null)]
// 集合相减
Collection subtractList = CollectionUtils.subtract(list, list1);
System.out.println(subtractList); // [User(id=2, name=ly, alimony=null)]

 

转载于:https://www.cnblogs.com/aibaiyang/p/9093730.html

你可能感兴趣的:(Java集合学习)