一、Collection层次结构中的根接口。Collection表示一组对象,这些对象也称为collection的元素。一些collection允许有重复的元素,而另一些则不允许。一些collection是有序的,而另一些则是无序的.JDK不提供此接口的任何直接实现:它提供更具体的子接口(如Set和List)实现。
1.List:有序的collection(也称为序列)。此接口的用户可以对列表中每个元素的插入位置进行精确地控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。列表通常允许重复的元素。更确切地讲,列表通常允许满足e1.equals(e2)的元素对e1和e2,并且如果列表本身允许null元素的话,通常它们允许多个null元素。List接口提供了特殊的迭代器,称为ListIterator,除了允许Iterator接口提供的正常操作外,该迭代器还允许元素插入和替代,以及双向访问。还提供了一个方法来获取从列表中指定位置开始的列表迭代器。hasNext()以正向遍历列表时,如果列表迭代器有多个元素,则返回true(换句话说,如果next返回一个元素而不是抛出异常,则返回true)。hasPrevious(用这个的是时候,它的迭代器为ListIterator)如果以逆向遍历列表,列表迭代器有多个元素,则返回true.List的对象要排序时,如果这个对象是String、Iterger这类似的酒可以直接用Collects.sort(List对象),如果是自己新创建的一个类时,想要用sort()方法,就必须让这个类实现Comparable接口,并实现该接口中的comperTo方法;List的主要实现类有ArrayList、Stack、LinkedList 代码实例:TestList.java
package collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class TestList {
public static void print(Collection c){
Iterator iterator=c.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
public static void main(String[] args) {
List list=new ArrayList<>();
list.add(new Student("SC", 22));
list.add(new Student("ZM", 23));
list.add(new Student("WYZ", 21));
list.add(new Student("LHM", 22));
print(list);
Collections.sort(list);
System.out.println();
print(list);
}
}
class Student implements Comparable{
String name;
int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(Object o) {
if(o!=null &&o instanceof Student){
Student stu=(Student)o;
if(this.age!=stu.age){
return stu.age-this.age; //先按年龄降序排序
}else{
return this.name.compareTo(stu.name); //如果年龄相同,就按姓名的升序排序
}
}
return 0;
}
}
输出接口为:
Student [name=SC, age=22]
Student [name=ZM, age=23]
Student [name=WYZ, age=21]
Student [name=LHM, age=22]
Student [name=ZM, age=23]
Student [name=LHM, age=22]
Student [name=SC, age=22]
Student [name=WYZ, age=21]
2.Set:一个不包含重复元素的collection。更确切地讲,set不包含满足e1.equals(e2)的元素对e1和e2,并且最多包含一个null元素。set是无序的,Set的主要实现类有HashSet和TreeSet
A.HashSet:通过hashCode和equals来进行排重,当hashCode返回只不一样时,就直接添加此元素;当hashCode返回值一样时,就比较equals方法,如果返回true,就丢弃这个元素,如果为false,就添加这个元素。实例代码:TestHashSet.java
package collection;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class TestHashSet {
public static void show(Collection c){
Iterator it=c.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public static void main(String[] args) {
Set set=new HashSet<>();
Student1 s1=new Student1(1,"lhm",22);
Student1 s2=new Student1(2,"wyz",22);
Student1 s3=new Student1(3,"sc",21);
Student1 s4=new Student1(4,"zm",21);
Student1 s5=new Student1(3, "wyz", 21);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
set.add(s5);
show(set);
}
}
class Student1{
private int id;
private String name;
private int age;
public Student1() {
super();
}
public Student1(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
return id+name.hashCode()+age;
}
@Override
public boolean equals(Object o) {
if(o==null) return false;
if(this==o) return true;
if(o instanceof Student1){
Student1 stu=(Student1)o;
if((this.getId()==stu.getId()) && (this.getName().equals(stu.getName())) &&(this.getAge()==stu.getAge())){
return true;
}else{
return false;
}
}
return false;
}
}
输出结果为:
Student [id=3, name=sc, age=21]
Student [id=4, name=zm, age=21]
Student [id=3, name=wyz, age=21]
Student [id=2, name=wyz, age=22]
Student [id=1, name=lhm, age=22]
B.TreeSet:通过comperto来进行排重,通过哪个属性来返回comperto值,就对哪个属性排重 实例代码:TestTreeSet.java
package collection;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class TestTreeSet {
public static void show(Collection c){
Iterator it=c.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public static void main(String[] args) {
Set set=new TreeSet<>();
Teacher t1=new Teacher("abc",20);
Teacher t2=new Teacher("lisi",21 );
Teacher t3=new Teacher("lisi",22);
Teacher t4=new Teacher("wangwu",21);
set.add(t1);
set.add(t2);
set.add(t3);
set.add(t4);
show(set);
}
}
class Teacher implements Comparable{
private String name;
private int age;
public Teacher() {
super();
}
public Teacher(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Teacher [name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(Object o) {
if(o!=null && o instanceof Teacher){
Teacher t=(Teacher)o;
// if(this.age!=t.age){
// return t.age-this.age;
// }else{
return this.getName().compareTo(t.getName());
// }
}
return 0;
}
}
输出结果为:
Teacher [name=abc, age=20]
Teacher [name=lisi, age=21]
Teacher [name=wangwu, age=21]
当把注释去掉时,输出结果为:
Teacher [name=lisi, age=22]
Teacher [name=lisi, age=21]
Teacher [name=wangwu, age=21]
Teacher [name=abc, age=20]
二、Map K-此映射所维护的键的类型 V-映射值的类型。将键映射到的值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。当要插入的键值对的键值在Map中已经有了,则会覆盖和这个键相同的对象。(而Set若有相同的对象,就迭起还没插入的对象)。Map的主要实现类有HashMap()和TreeMap();
A.HashMap():基于哈希表的Map接口的实现。此实现提供所有可选的映射操作,并允许使用null值和null键。(除了非同步和允许使用null之外,HashMap类与Hashtable大致相同)。此类不保证映射的顺序,特别是它不保证该顺序恒久不变。它的键值唯一性同HashSet唯一性一样,通过hashCode()和equals()来判断是否是一样的值 代码实例:
package map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class TestHashMap {
public static void show(Map map){
Set set=map.keySet();
Iterator it=set.iterator();
while(it.hasNext()){
Object key=it.next();
Object value=map.get(key);
System.out.println(key+"--"+value);
}
}
public static void main(String[] args) {
Map map=new HashMap<>();
Person p1=new Person("lhm", 21);
Person p2=new Person("lhm", 20);
Person p3=new Person("zhangsan",18);
Person p4=new Person("lisi",18);
map.put(p1, 1);
map.put(p2, 2);
map.put(p3, 3);
map.put(p4, 4);
// map.put(1, "one");
// map.put(2, "one");
// map.put(2, "two");
// map.put(3, "three");
show(map);
}
}
class Person{
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public boolean equals(Object o) {
if(o==null) return false;
if(this==o) return true;
if(o instanceof Person){
Person p=(Person)o;
if((p.name.equals(this.name))){
return true;
}else {
return false;
}
}
return false;
}
}
输出结果为:
Person [name=lisi, age=18]--4
Person [name=zhangsan, age=18]--3
Person [name=lhm, age=21]--2
B.TreeMap():该映射根据其键的自然顺序进行排序,或者根据创建映射时提供的Comparator进行排序,具体取决于使用的构造方法。它的键值唯一性同TreeSet唯一性一样,通过CompareTo来判断。代码实例:TestTreeMap.java
package map;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class TestTreeMap {
public static void show(Map map){
Set set=map.keySet();
Iterator it=set.iterator();
while(it.hasNext()){
Object key=it.next();
Object value=map.get(key);
System.out.println(key+"--"+value);
}
}
public static void main(String[] args) {
Map map=new TreeMap<>();
Person1 p1=new Person1("lhm", 20);
Person1 p2=new Person1("zhangsan",18);
Person1 p3=new Person1("lhm", 21);
Person1 p4=new Person1("lhm",20);
Person1 p5=new Person1("lisi",20);
map.put(p1, 1);
map.put(p2, 2);
map.put(p3, 3);
map.put(p4, 4);
map.put(p5, 5);
// map.put(1,"two");
// map.put(1, "one");
// map.put(3, "three");
// map.put(2, "two");
show(map);
}
}
class Person1 implements Comparable{
private String name;
private int age;
public Person1() {
super();
}
public Person1(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person1 [name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(Object o) {
if(o!=null && o instanceof Person1){
Person1 p=(Person1)o;
if(!p.name.equals(this.name)){
return this.name.compareTo(p.name);
}else{
return p.age-this.age;
}
}
return 0;
}
}
输出结果为:
Person1 [name=lhm, age=21]--3
Person1 [name=lhm, age=20]--4
Person1 [name=lisi, age=20]--5
Person1 [name=zhangsan, age=18]--2