Java容器复习
一 容器api
说明(Hashset与TreeSet的区别为 TreeSet是按照字母顺序排序的,Map类似)
二 Collection接口
2.1一些重要的方法
- boolean add(Object o):添加成功返回true
- boolean remove(Object o):删除成功返回true(如果是要删除自定义类的话需要实现equals方法)
- boolean contains(Object obj):包含这个对象则返true(其本质是调用一般类自带的equals方法进行比较,如果没有则需要自行定义)
- void sort():对集合进行排序(需要实现Comparable接口)
- Object[] toArray():返回包含此 collection 中所有元素的数组
2.2Collection接口对自定义类的比较
2.2.1在不实现equals方法的情况下对自定义类进行remove等操作
package 容器;
import java.util.ArrayList;
import java.util.Collection;
class Name
{
String firstName;
String lastName;
Name(String s1,String s2)
{
this.firstName=s1;
this.lastName=s2;
}
}
public class TestCollection {
public static void main(String[] args) {
Collection collection=new ArrayList();
collection.add(new Name("f1","l1"));
collection.add(new Name("f2","l2"));
System.out.println(collection.contains(new Name("f2","l2")));
System.out.println(collection.remove(new Name("f1","l1")));
System.out.println(collection);
}
/* 结果:false
false
[容器.Name@4554617c, 容器.Name@74a14482]*/
}
2.2.2 添加equals方法后进行操作
package 容器;
import java.util.ArrayList;
import java.util.Collection;
class Name
{
String firstName;
String lastName;
Name(String s1,String s2)
{
this.firstName=s1;
this.lastName=s2;
}
public boolean equals(Object 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 class TestCollection {
public static void main(String[] args) {
Collection collection=new ArrayList();
collection.add(new Name("f1","l1"));
collection.add(new Name("f2","l2"));
System.out.println(collection.contains(new Name("f2","l2")));
System.out.println(collection.remove(new Name("f1","l1")));
System.out.println(collection);
}
/* 结果:true
true
[容器.Name@c8c]*/
}
2.2.3 注意事项
对于new 出来而没有取别名的对象才需要重写equals方法,如果对象的引用确定的话则不用
三 Set接口
3.1 Set接口顾名思义即集合,里面的元素是不能够重复的
3.2 Set接口可分为HashSet和TreeSet
3.3 HashSet
package 容器;
import java.util.HashSet;
import java.util.Set;
public class TestHashSet {
public static void main(String[] args) {
Set s=new HashSet();
s.add("hello");
s.add("world");
s.add(new Name("f1","f2"));
s.add(new Integer(100));
System.out.println(s.add(new Name("f1","f2")));
System.out.println(s.add("hello"));
System.out.println(s);
/* 结果:false
false
[world, 100, hello, 容器.Name@c8b]
*/
}
}
3.4 TreeSet
3.4.1与上面代码相同,即只把HashSet变为TreeSet,则会抛出异常,原因是TreeSet是排序的Set需要实现Comparable接口
package 容器;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class TestTreeSet {
public static void main(String[] args) {
Set s=new TreeSet();
s.add("hello");
s.add("world");
s.add(new Name("f1","f2"));
s.add(new Integer(100));
System.out.println(s.add(new Name("f1","f2")));
System.out.println(s.add("hello"));
System.out.println(s);
/* Exception in thread "main" java.lang.ClassCastException:
容器.Name cannot be cast to java.lang.Comparable*/
}
}
四 List接口
4.1
List接口内是有顺序的,即加入第一个元素则成为List的第一个元素,与Set要区别,Set加入后是无序的,因此List可以用索引来查找元素
4.2 常用方法
- Object get(int index)
- Object set(int index,Object element)
- int indexof(Object o)
4.3 ArrayList
ArrayList的作用与Array类似
4.4LinkedList
通常被当做栈或者队列来用
package 容器;
import java.util.LinkedList;
import java.util.List;
public class TestLinkedList {
public static void main(String[] args) {
/* 将LinkedList当做栈用*/
LinkedList stack=new LinkedList();
stack.addFirst(1);
stack.addFirst(2);
stack.addFirst(3);
System.out.println(stack);
System.out.println(stack.pollFirst());//弹出栈顶元素
System.out.println(stack.peekFirst());//显示栈顶元素,但不弹出
System.out.println(stack);
/*结果: [3, 2, 1]
3
2
[2, 1] */
/*将LinkedList当做队列来用*/
LinkedList queue=new LinkedList();
queue.addLast(1);
queue.addLast(2);
queue.addLast(3);
System.out.println(queue);
System.out.println(queue.pollFirst());//弹出队列头元素
System.out.println(queue.peekFirst());//显示队列头元素
System.out.println(queue);
/*[1, 2, 3]
1
2
[2, 3]
*/
}
}
五 Comparable接口
5.1当需要对列表里的元素进行排序时需要实现Comparable接口,即对自己定义的类实现Comparable接口
5.2 Comparable接口
实现Comparable接口需要实现里面的一个方法
public int compareTo(Object obj);
该方法:
返回0表示this==obj
返回正数表示this>obj
返回负数表示this<obj
调用者一般都是自己
5.3 list实现Comparable接口
package 容器;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
class Name implements Comparable
{
String firstName;
String lastName;
Name(String s1,String s2)
{
this.firstName=s1;
this.lastName=s2;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public void setFirstName(String s)
{
firstName=s;
}
public void setLastName(String s)
{
lastName=s;
}
public boolean equals(Object 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)//先根据lastName排序,如果相等则根据firstName排序
{
Name n=(Name)o;
int lastCmp=lastName.compareTo(n.lastName);
return (lastCmp!=0?lastCmp:firstName.compareTo(n.firstName));
}
}
public class TestLinkedList2 {
public static void main(String[] args) throws Exception {
List list=new LinkedList();
list.add(new Name("Karl","M"));
list.add(new Name("Steven","Lee"));
list.add(new Name("John","O"));
list.add(new Name("Tom","M"));
Iterator iterator=list.iterator();
while(iterator.hasNext())
{
Name name=(Name)iterator.next();
System.out.println(name.getFirstName()+" "+name.getLastName());
}
/* Karl M
Steven Lee
John O
Tom M */
Collections.sort(list);
Iterator iterator1=list.iterator();
while(iterator1.hasNext())
{
Name name=(Name)iterator1.next();
System.out.println(name.getFirstName()+" "+name.getLastName());
}
/*
Steven Lee
Karl M
Tom M
John O
*/
}
}
六 Map接口
6.1
Map接口的实现类有HashMap和TreeMap等
6.2
Map类中存储的键-值对通过键来标识,所以键值不能重复
6.3 常用方法
- Object put(Object key,Object value)
- Object get(Object key)