一、 1图 1类(collections) 3知识点
6接口(hashset arraylist linkedlist hashmap iterator)
(容器位于Java.util包内)
collection Map
set list
HashSet LinkedList ArrayList HashMap
一个一个得装 一对一对的装
collection接口定义了存取一组对象的方法,其子接口set list分别定义存取方式。
例:
impor
java.util.*;
public
class Test{
public static void main(String[] args){
Collection c = new ArrayList();//父类对象指向子类对象,便于以后修改如把ArrayList改成
LinkedIist 下面的特定代码则可以不用修改。
c.add("hello");
c.add(new
Integer(100));//注意括号中必须为对象而不能为基础类型对象的值
System.out.println(c.size());
System.out.println(c);//调用toString方法
}
}
二、Iterator接口
所有实现了Collection接口的容器类都有一个iterator方法用以返回一个实现了Iterator接口的对象。
Iterator对象称作迭代器,用以方便的实现对容器内元素的遍历操作。
Iterator接口定义了如下方法:
boolean
hasNext(); //判断游标右边是否有元素
Object
next(); //返回游标右边的元素并将游标移动到下一个位置
void
remove(); //删除游标左面的元素,在执行完next之后该操作只能执行一次
Java代码
importjava.util.*;
publicclassTest {
publicstaticvoidmain(String[] args) {
Collection c =newHashSet();
c.add(newName("f1","l1"));
c.add(newName("f2","l2"));
c.add(newName("f3","l3"));
Iterator i = c.iterator();
while(i.hasNext()) {
//next()的返回值为Object类型,需要转换为相应类型
Name n = (Name)i.next();
System.out.print(n.
getFirstName_r()+" ");
}
}
}
**1.5增强for循环
1、对于遍历array和Collection相当简单
2、不方便访问Array的下标值,与iterator相比不方便删除集合中的内容,除了简单遍历读出其中内容外,
不建议使用这种方法
import java.util.*;
public class EnhancedFor {
public static void
main(String[] args) {
int[] arr = {1, 2, 3, 4,
5};
//把arr中的元素拿出来放在i中
for(int i : arr) {
System.out.println(i);
}
Collection c = new
ArrayList();
c.add(new String("aaa"));
c.add(new String("bbb"));
c.add(new String("ccc"));
for(Object o : c) {
System.out.println(o);
}
}
}
三、Set List算法举例
1、 public static void
main(String[] args){
Set s1 = new HashSet();
Set s2 = new HashSet();
s1.add("a");s1.add("b");s1.add("c");
s2.add("k");s2.add("a");s2.add("a");
Set s3 = new HashSet(s1);
s3.retainAll(s1);
Set s4 = new HashSet(s1);
s4.addAll(s2);
System.out.println(s3);
System.out.println(s4);
}
2、List l1 = new
LinkedList();
for(int i = 0;i<=5;i++){
l1.add("a"+i);
}
System.out.println(l1);
l1.add(3,"a100");
System.out.println(l1);
l1.set(6,"a200");
System.out.println(l1);
System.out.print((String)l1. get_r(2)+" ");
System.out.print(l1.indexof("a3"));
l1.remove(1);
System.out.println(l1);
常用算法:
List l1 = new LinkedList();
List l2 = new LinkedList();
for(int i=0;i<=9;i++){l1.add("a"+i);}
System.out.println(l1);
Collections.shuffle(l1);//随机排序
System.out,println(l1);
Collections.reverse(l1);//逆序
System.out,println(l1);
Collections.sort(l1);//排序
System.out,println(l1);
System.out.println(Collections.binarySearch(l1,"a5"));//2分法查找
3、comparable接口
comparable接口中只有一个方法public int compareTo(object
o),因此在继承了该接口的子类中需要重
写compareTo方法。
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","l1")));
System.out.println(c);
}
}
//继承Comparable接口
class Name implements Comparable {
private String firstName,lastName;
public Name(String firstName, String lastName)
{
this.firstName = firstName; this.lastName = lastName;
}
public String getFirstName_r()
{ return
firstName; }
public
String getLastName_r() { return
lastName; }
public String toString() { return firstName + " " + lastName; }
//对象在调用remove、contains方法时比较对象是否相等,对于自定义的类型需要重写equals方法
//和hashCode方法以实现自定义对象相等规则。
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();
}
//重写compareTo方法确定对象间的大小
public int compareTo(Object o) {
Name n = (Name)o;
int lastCmp =
l astName.compareTo(n.lastName);
return
(lastCmp!=0 ? lastCmp : firstName.compareTo(n.firstName));
}
}
4、
Map接口:
Map接口的类用来存储键-值对,
Map接口的实现类有HashMap和TreeMap,键值对通过键标识,所以键值
不能重复。
例:
import java.util.*;
public class TestMap {
public static void main(String args[]) {
Map m1 = new HashMap();
Map m2 = new TreeMap();
//m1.put("one",new
Integer(1));
m1.put("one", 1);
//m1.put("two",new Integer(2));
m1.put("two", 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)));
(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);
}
}
运用泛型和自动打包解包后实例:
import java.util.*;
public class TestMap {
public static void main(String args[]) {
Map
Integer> m1 = new HashMap
Integer>();
m1.put("one", 1);
m1.put("two", 2);
m1.put("three", 3);
System.out.println(m1.size());
System.out.println(m1.containsKey("one"));
if(m1.containsKey("two"))
{
//int i = ((Integer)m1.
get_r("two")).intValue();
int i = m1. get("two");
System.out.println(i);
}
}
}
5,自动打包解包。
import java.util.*;
public class TestArgsWords {
//private static final Integer ONE = new Integer(1);
private
static final int ONE = 1;
public static void main(String args[]) {
Map m = new HashMap();
for (int i = 0; i < args.length;
i++) {
//Integer freq = (Integer) m.get(args[i]);
int freq = (Integer) m.get(args[i]) == null ? 0
: (Integer) m.get(args[i]);
//m.put(args[i],(freq == null? ONE : new
Integer(freq.intValue() + 1)));
m.put(args[i], freq==0 ? ONE : freq + 1);
}
System.out.println(m.size() + " distinct words
detected:");
System.out.println(m);
}
}
6、泛型 :在定义集合时同时定义集合中对象的类型。
import java.util.*;
public class BasicGeneric {
public static void main(String[] args) {
List c = new
ArrayList();//API中类后面跟有<>就可用泛型
c.add("aaa");
c.add("bbb");
c.add("ccc");
for(int i=0;
i
String s =
c.get(i);
System.out.println(s);
}
Collection
c2 = new HashSet();
c2.add("aaa"); c2.add("bbb");
c2.add("ccc");
for(Iterator it =
c2.iterator(); it.hasNext(); ) {
String s = it.next();
System.out.println(s);
}
}
}
class MyName implements
Comparable {
int age;
public int compareTo(MyName mn) {
if(this.age >
mn.age) return 1;
else if(this.age < mn.age) return -1;
else return 0;
}
}
读单词出现的次数:未使用和使用泛型比较:
import java.util.*;
public class TestArgsWords {
//private static final Integer ONE = new
Integer(1);
private static final int ONE = 1;
public static void main(String args[]) {
Map m = new HashMap();
for (int i = 0; i < args.length;
i++) {
//Integer freq = (Integer) m.get(args[i]);
int freq = (Integer) m.get(args[i]) == null ? 0 : (Integer)
m.get(args[i]);
//m.put(args[i],(freq == null? ONE : new
Integer(freq.intValue() + 1)));
m.put(args[i], freq==0 ? ONE : freq + 1);
}
System.out.println(m.size() + " distinct words
detected:");
System.out.println(m);
}
}
使用后
import java.util.*;
public class TestArgsWords2 {
private static final int ONE = 1;
public
static void main(String args[]) {
Map m = new
HashMap();
for (int i = 0; i < args.length; i++) {
if(!m.containsKey(args[i])) {
m.put(args[i], ONE);
}
else {
int freq = m.get(args[i]);
m.put(args[i], freq + 1);
}
}
System.out.println(m.size() + " distinct words detected:");
System.out.println(m);