第八章 容器类 Collection接口(Set、List、Map)、Iterator接口、Comparable接口
-
提纲
-
容器的概念
Set无序不可重复,List有序可重复,两者是否equals
-
Collection接口
*有关接口类Collection c = new ArratList()实例化的原因:
*有关接口类Collection c = new ArratList()实例化的好处:
collection c = new ArrayList(); 好处:修改方便。这里使用父类引用指向子类对象,就意味着不能使用ArrayList中所特有的方法,后期如果要换别的对象,直接修改此处的名字就行不用再考虑底下的代码了(动态绑定)。*add()添加只能添加一个对象,不能添加一个基础数据类型,因为基础数据类型分配在栈上面。
*System.out.println(c)相当于调用c.toString()方法。但是查看API在ArrayList类中并未直接发现其中含有toString()方法。 大佬的解释
注:要重写equals方法必须重写hashCode方法,两个对象equals时则必须拥有相同的hashCode。当一个对象类型作为收集型对象的元对象时,这个对象应该拥有自己处理equals(),和/或处理hashCode()的设计,而且要遵守前面所说 的两种原则。equals()先要查null和是否是同一类型。查同一类型是为了避免出现ClassCastException这样的异常给丢出来。查 null是为了避免出现NullPointerException这样的异常给丢出来。
马士兵老师相关的讲解
上述有关内容参考大佬1相关的讲解
大佬2的讲解
大佬3的讲解
大佬4的讲解
简单例子代码:
import java.util.*;
public class BasicContainer {
public static void main (String [] args) {
Collection c = new HashSet();
c.add("hello");
c.add(new Name("f1","l1"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));
System.out.println(c.remove(new Name("f1","l2")));
System.out.println(c.remove(new Name("f1","l1")));
System.out.println(c.remove(" "));
System.out.print(c);
}
}
class Name {
private String firstName,lastName;
public Name (String firstName,String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String toString() {
return firstName + " " + lastName;
}
public boolean equals(Object obj) {
if(obj==null) {
return super.equals(obj);
}
if(obj instanceof Name) {
Name name = (Name) obj;
return (firstName.equals(name.firstName))
&& (lastName.equals(name.lastName));
}
return super.equals(obj);
}
public int hashCode() {
return firstName.hashCode();
}
}
- Iterator接口(迭代器)
使用Iterator时只能使用其自带的remove()方法删除而不能使用容器自带的remove()方法,因为迭代期间相当于锁死状态。 - 增强的for循环
简单小例子代码:
import java.util.*;
public class EnhancedFor {
public static void main (String[] args) {
int [] array = {1,2,3,4,5};
for(int i:array){
System.out.println(i);
}
Collection c = new ArrayList();
c.add(new String("aaa"));
c.add(new String("bbb"));
c.add(new String("ccc"));
c.add("aaa");
c.add("bbb");
c.add("aaa");
for(Object o:c) {
System.out.println(o);
}
}
}
- Set 接口(无序不重复)
- List 接口(有序可重复)
注意这里的add()方法和set()方法的区别,add()方法是新增一个元素,原来的元素向后移动一个,set()方法在指定位置使用指定元素取代指定元素。
这里的类是Collections而不是Collection,注意看下属例子中各方法的用法。
- comparable 接口
简单的例子代码:
import java.util.*;
public class BasicContainer {
public static void main (String [] args) {
Collection c = new HashSet();
c.add("hello");
c.add(new Name("f1","l1"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));
System.out.println(c.remove(new Name("f1","l2")));
System.out.println(c.remove(new Name("f1","l1")));
System.out.println(c.remove(" "));
System.out.print(c);
System.out.println("===========================");
List l1 = new LinkedList();
l1.add(new Name("Kar1","M"));
l1.add(new Name("Steven","Lee"));
l1.add(new Name("John","o"));
l1.add(new Name("Aom","M"));
System.out.println(l1);
Collections.sort(l1);
System.out.println(l1);
}
}
class Name implements Comparable{
private String firstName,lastName;
public Name (String firstName,String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String toString() {
return firstName + " " + lastName;
}
public boolean equals(Object obj) {
if(obj==null) {
return super.equals(obj);
}
if(obj instanceof Name) {
Name name = (Name) obj;
return (firstName.equals(name.firstName))
&& (lastName.equals(name.lastName));
}
return super.equals(obj);
}
public int hashCode() {
return firstName.hashCode();
}
public int compareTo(Object o) {
Name name = (Name)o;
int lastCmp = lastName.compareTo(name.lastName);
return
(lastCmp!=0 ? lastCmp : firstName.compareTo(name.firstName));
}
}
- 如何选择数据结构
- Map 接口
例子代码:
import java.util.*;
public class TestMap {
public static void main(String[] args) {
Map m1 = new HashMap();
Map m2 = new HashMap();
m1.put("one",new Integer(1));
m1.put("two",new Integer(2));
m1.put("three",new Integer(3));
m2.put("A",new Integer(1));
m2.put("B",new Integer(2));
System.out.println(m1.size());
System.out.println(m1.containsKey("one"));
System.out.println(m2.containsValue(new Integer(1)));
if(m1.containsKey("two")) {
int i = ((Integer)m1.get("two")).intValue();
System.out.println(i);
}
Map m3 = new HashMap(m1);
m3.putAll(m2);
System.out.println(m3);
}
}
- Auto-boxing/unboxing(自动封箱/解箱)
大佬关于自动封箱/解箱的解释
包装类的大佬解释
包装类的默认值均为null
简单例子:
import java.util.*;
public class TestMap2 {
public static void main(String[] args) {
Map m1 = new HashMap();
Map m2 = new HashMap();
//m1.put("one",new Integer(1));
m1.put("one",1);
//m1.put("two",2);
m1.put("two",new Integer(2));
//m1.put("three",new Integer(3));
m1.put("three",3);
//m2.put("A",new Integer(1));
m2.put("A",1);
//m2.put("B",new Integer(2));
m2.put("B",2);
System.out.println(m1.size());
System.out.println(m1.containsKey("one"));
//System.out.println(m2.containsValue(new Integer(1)));
System.out.println(m2.containsValue(1));
if(m1.containsKey("two")) {
//int i = ((Integer)m1.get("two")).intValue();
int i = (Integer)m1.get("two");
System.out.println(i);
}
Map m3 = new HashMap(m1);
m3.putAll(m2);
System.out.println(m3);
}
}
简化:int类没有办法表示null,所以freq的类型必须是Integer。
12. 泛型
例子代码:
import java.util.*;
public class TestBasicGeneric {
public static void main (String [] args) {
List<String> c = new ArrayList<String>();
c.add("aaa");
c.add("bbb");
c.add("ccc");
for(int i=0;i<c.size();i++) {
String s = c.get(i);
System.out.println(s);
}
Collection<String> c2 = new HashSet<String>();
c2.add("aaa");
c2.add("bbb");
c2.add("ccc");
for(Iterator<String> it = c2.iterator();it.hasNext();) {
String s = it.next();
System.out.println(s);
}
List<MyName> c3 = new LinkedList<MyName>();
c3.add(new MyName(15));
c3.add(new MyName(12));
c3.add(new MyName(16));
Collections.sort(c3);
System.out.println(c3);
Collections.reverse(c3);
System.out.println(c3);
}
}
class MyName implements Comparable <MyName> {
int age;
MyName(int age) {
this.age = age;
}
public String toString() {
return String.valueOf(age);
}
public boolean equals(MyName mn) {
if(mn==null) {
return super.equals(mn);
}else{
return ((Integer)age).equals((Integer)mn.age);
}
}
public int hashCode() {
return ((Integer)age).hashCode();
}
public int compareTo(MyName mn) {
if(this.age > mn.age) return 1;
else if (this.age < mn.age) return -1;
else return 0;
}
}
总结
注:本博客由溺水狗原创但其学习资源资料来源于网络(马士兵老师的教学视频),特此感谢马士兵老师