集合一共分为两种:Collection/Map
Collection集合
List集合
使用for循环输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public static void main(String[] args) {
List<Person> persons = new ArrayList<Person>(); persons.add(new Person("张三", 23, "男")); persons.add(new Person("李四", 31, "女")); persons.add(new Person("王五", 22, "男")); persons.add(new Person("赵六", 27, "女"));
for (int i = 0; i < persons.size(); i++) { System.out.println(persons.get(i).getName()); } }
|
张三
李四
王五
赵六
使用for…each循环输出
for…each其实是一个专门用来输出对象集合或者数组的循环。
语法:
for(数据类型 对象:对象集合){
执行的语句;
}
案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public static void main(String[] args) {
List<Person> persons = new ArrayList<Person>(); persons.add(new Person("张三", 23, "男")); persons.add(new Person("李四", 31, "女")); persons.add(new Person("王五", 22, "男")); persons.add(new Person("赵六", 27, "女"));
for (Person per : persons) { System.out.println(per.getName()); } }
|
张三
李四
王五
赵六
使用迭代器输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public static void main(String[] args) {
List<Person> persons = new ArrayList<Person>(); persons.add(new Person("张三", 23, "男")); persons.add(new Person("李四", 31, "女")); persons.add(new Person("王五", 22, "男")); persons.add(new Person("赵六", 27, "女"));
Iterator<Person> iterator = persons.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next().getName()); } }
|
张三
李四
王五
赵六
Set集合
List类型的集合中添加的元素是可以重复添加但是有序,而Set集合中不能重复添加对象,而且没有顺序。
Set集合在开发中常用的子类有两个:
HashSet
HashSet是Set接口的一个子类,主要的特点是:里面不能存放重复元素,而且是采用散列的存储方式,所以是没有顺序的。
1 2 3 4 5 6
| public static void main(String[] args) {
HashSet<String> hashSet = new HashSet<String>(); System.out.println(hashSet); }
|
[]
输出对象的时候发现其实和List集合没有太大的区别。
向集合中添加重复的内容1 2 3 4 5 6 7 8 9 10 11 12
| public static void main(String[] args) {
HashSet<String> hashSet = new HashSet<String>();
hashSet.add("lvjie"); hashSet.add("edu"); hashSet.add("lvjie"); hashSet.add("com"); System.out.println(hashSet); }
|
[com, org, edu, lvjie]
添加完成后发现HashSet集合存放对象是无序的。在添加完重复的内容以后,在集合中存放的重复内容只能有一个。
输出集合的内容
for…each
1 2 3 4 5 6 7 8 9 10 11 12
| public static void main(String[] args) {
HashSet<String> hashSet = new HashSet<String>(); hashSet.add("lvjie"); hashSet.add("edu"); hashSet.add("com"); for (String str : hashSet) { System.out.println(str); } }
|
com
edu
lvjie
迭代器输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public static void main(String[] args) {
HashSet<String> hashSet = new HashSet<String>(); hashSet.add("lvjie"); hashSet.add("edu"); hashSet.add("com");
Iterator<String> iterator = hashSet.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } }
|
com
edu
lvjie
自定义的类的对象输出
自定义Person类1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package com.lvjie.entity;
public class Person {
private String name; private Integer age; private String sex;
public Person(String name, Integer age, String sex) { this.name = name; this.age = age; this.sex = sex; }
public Person() {}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
public String getSex() { return sex; }
public void setSex(String sex) { this.sex = sex; }
@Override public String toString() { return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]"; } }
|
1 2 3 4 5 6 7 8 9 10
| public static void main(String[] args) {
HashSet<Person> hashSet = new HashSet<Person>(); hashSet.add(new Person("张三", 23, "男")); hashSet.add(new Person("李四", 31, "女")); hashSet.add(new Person("王五", 22, "男")); hashSet.add(new Person("赵六", 27, "女")); System.out.println(hashSet); }
|
[Person [name=王五, age=22, sex=男], Person [name=张三, age=23, sex=男], Person [name=李四, age=31, sex=女], Person [name=赵六, age=27, sex=女]]
TreeSet
TreeSet集合中存放的内容需要排序,而这里的排序不是添加内容时的顺序而是要按照指定的属性来完成对象的排序操作。
1 2 3 4 5 6
| public static void main(String[] args) {
TreeSet<String> treeSet = new TreeSet<String>(); System.out.println(treeSet); }
|
[]
可以发现TreeSet和HashSet类的操作其实没有太大的区别。
1 2 3 4 5 6 7 8 9 10 11 12 13
| public static void main(String[] args) {
TreeSet<String> treeSet = new TreeSet<String>();
treeSet.add("lvjie"); treeSet.add("edu"); treeSet.add("admin"); treeSet.add("com"); treeSet.add("admin"); System.out.println(treeSet); }
|
[admin, com, edu, lvjie]
向TreeSet集合中添加完成后,内容是有序的,这里的有序不是添加内容的顺序,而是内容按照一定的方式进行排序了,但是添加的重复内容就只能有一个存放在此集合中。
自定义的类的对象输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| package com.lvjie.entity;
public class Person implements Comparable<Person> {
private String name; private Integer age; private String sex;
public Person(String name, Integer age, String sex) { this.name = name; this.age = age; this.sex = sex; }
public Person() {}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
public String getSex() { return sex; }
public void setSex(String sex) { this.sex = sex; }
@Override public String toString() { return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]"; }
@Override public int compareTo(Person per) { if (this.age < per.getAge()) { return -1; } else if (this.age > per.getAge()) { return 1; } else { return 0; } } }
|
[Person [name=王五, age=22, sex=男], Person [name=张三, age=23, sex=男], Person [name=赵六, age=27, sex=女], Person [name=李四, age=31, sex=女]]
在TreeSet集合中集合会给存储的对象进行排序,而这个排序的操作是由Comparable接口来完成的,所以自定义类一定要实现Comparable接口,这也是和HashSet的不同所在。
Map集合
HashMap
1 2 3 4 5 6
| public static void main(String[] args) {
HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); System.out.println(hashMap); }
|
{}
输出代表了Map类的对象是一个键值对对象。
在Map集合中是通过key值获取对应的内容的。
public V get(Object key){}
1 2 3 4 5 6 7 8 9 10 11 12 13
| public static void main(String[] args) {
HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); hashMap.put(1, "lvjie"); hashMap.put(2, "edu"); hashMap.put(3, "com"); System.out.println(hashMap);
System.out.println(hashMap.get(2)); }
|
{1=lvjie, 2=edu, 3=com}
edu
获取Map集合中的所有value值
public Collectionvalues(){}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public static void main(String[] args) {
HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); hashMap.put(11, "lvjie"); hashMap.put(22, "edu"); hashMap.put(33, "com"); System.out.println(hashMap); Collection<String> list = hashMap.values(); for (String value : list) { System.out.println(value); } }
|
{33=com, 22=edu, 11=lvjie}
com
edu
lvjie
TreeMap
TreeMap是可以进行排序的,但是它的排序是给Map集合中的key(键)进行排序。
1 2 3 4 5 6 7 8 9 10 11
| public static void main(String[] args) {
TreeMap<String, String> treeMap = new TreeMap<String, String>(); treeMap.put("num2", "edu"); treeMap.put("num3", "com"); treeMap.put("num4", "lvjie"); treeMap.put("num1", "lvjie"); System.out.println(treeMap); }
|
{num1=lvjie, num2=edu, num3=com, num4=lvjie}
Map集合的内容输出
输出Map集合中key
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public static void main(String[] args) {
TreeMap<Student, String> treeMap = new TreeMap<Student, String>(); treeMap.put(new Student("李四", 31, 1002), "回族"); treeMap.put(new Student("张三", 23, 1001), "汉族"); treeMap.put(new Student("王五", 22, 1003), "维吾尔族");
Set<Student> keys = treeMap.keySet(); for (Student student : keys) { System.out.println(student); }
Iterator<Student> iterator = keys.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } }
|
Student [name=王五, age=22, code=1003]
Student [name=李四, age=31, code=1002]
Student [name=张三, age=23, code=1001]
输出Map集合中value
通过key获取value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public static void main(String[] args) {
TreeMap<Student, String> treeMap = new TreeMap<Student, String>(); treeMap.put(new Student("李四", 31, 1002), "回族"); treeMap.put(new Student("张三", 23, 1001), "汉族"); treeMap.put(new Student("王五", 22, 1003), "维吾尔族");
Set<Student> keys = treeMap.keySet(); for (Student student : keys) { System.out.println(treeMap.get(student)); }
Iterator<Student> iterator = keys.iterator(); while (iterator.hasNext()) { System.out.println(treeMap.get(iterator.next())); } }
|
维吾尔族
回族
汉族
直接获取value值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public static void main(String[] args) {
TreeMap<Student, String> treeMap = new TreeMap<Student, String>(); treeMap.put(new Student("李四", 31, 1002), "回族"); treeMap.put(new Student("张三", 23, 1001), "汉族"); treeMap.put(new Student("王五", 22, 1003), "维吾尔族");
Collection<String> collection = treeMap.values(); for (String string : collection) { System.out.println(string); }
Iterator<String> iterator = collection.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } }
|
维吾尔族
回族
汉族