复习_01_容器应用

一 . 工作中java用的少了,知识点忘记的很快。导致这两天面试出现了脱节的现象,真是悲哀啊。幸好思路还算清晰,只要把知识点在积累起来,加上简单的应用,应该就OK了。话不多说,进入正题吧。

        

二. 容器这章应该还算印象比较深的,因为这东西用的多,这是为了知识的完整性复习的。

容器分类:有序的List,无序的Set,键值对的Map三大接口;另外Iterator,Collections这两个工具类。

list中常用的有ArrayList(数组实现)和LinkedList(链表实现),Set中常用的有HashSet(哈希表实现)和TreeSet(二叉树实现,有序),Map中常用的有HashMap(哈希表实现)和TreeMap(二叉树实现,有序)。

   Iterator则是遍历集合的通用接口。Collections提供了操作容器的使用方法,比如排序 二分查找 容器同步 得到不可变更的容器等。

以下是列子程序:

 public class Student implements Comparable<Student>
{
	private int id;
	private String name;
	public Student(int id, String name) {
		this.id = id;
		this.name = name;
	}
	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;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj  instanceof Student) {
			Student tmp = (Student)obj;
			return tmp.getId() == id && tmp.getName().equals(name);
		} else {
			return false;
		}
	}
	@Override
	public int hashCode() {
		return name.hashCode();
	}
	@Override
	public String toString() {
		return id + " " + name;
	}
	public int compareTo(Student s) {
		if (id > s.getId()) {
			return 1;
		} else if (id == s.getId()) {
			return 0;
		} else {
			return -1;
		}
	}
	
}
 

 import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import java.util.Queue;

public class ListTest {
	
	public static void main(String[] args) {
		
		List<Student> a1 = new ArrayList<Student>();
		a1.add(new Student(2, "name2"));
		a1.add(new Student(1, "name1"));
		a1.add(new Student(3, "name3"));
		println("a1.contains:" + a1.contains(new Student(1,"name1")));
		Collections.sort(a1);
		for(int i=0; i<a1.size(); i++) {
			println(a1.get(i));
		}
		println("Collections.binarySearch:" + Collections.binarySearch(a1, new Student(3,"name3")));
		for(Iterator<Student> it=a1.iterator(); it.hasNext(); ) {
			println(it.next());
			it.remove();
		}
		println("a1.isEmpty: " + a1.isEmpty());
		
		Queue<Student> que = new LinkedList<Student>();
		que.offer(new Student(1,"S1"));
		que.offer(new Student(2,"S2"));
		que.offer(new Student(3,"S3"));
		Student s = que.poll();
		while (s != null) {
			println(s);
			s = que.poll();
		}
		println("a1.isEmpty: " + que.isEmpty());
	}

	public static void println(Object o) {
		System.out.println(o);
	}
}


import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class SetTest {
	
	public static void main(String[] args) {
		
		//Set<Student> a1 = new HashSet<Student>();
		Set<Student> a1 = new TreeSet<Student>();
		a1.add(new Student(2, "name2"));
		a1.add(new Student(1, "name1"));
		a1.add(new Student(3, "name3"));
		println("a1.contains:" + a1.contains(new Student(1,"name1")));
		for(Student s : a1) {
			println(s);
		}
		a1.add(new Student(4, "name4"));
		for(Iterator<Student> it=a1.iterator(); it.hasNext(); ) {
			println(it.next());
			it.remove();
		}
		println("a1.isEmpty: " + a1.isEmpty());

	}

	public static void println(Object o) {
		System.out.println(o);
	}
}

 

 import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;


public class MapTest {
	
	public static void main(String[] args) {
		
		Map<String,Student> a1 = new HashMap<String,Student>();
		//Map<String,Student> a1 = new TreeMap<String,Student>();
		a1.put("s3",new Student(3, "name3"));
		a1.put("s2",new Student(2, "name2"));
		a1.put("s0",new Student(0, "name0"));
		a1.put("s1",new Student(1, "name1"));
		println("a1.contains:" + a1.containsValue(new Student(1,"name1")));
		for(String i : a1.keySet()) {
			println(a1.get(i));
		}
		a1.put("s4",new Student(4, "name4"));
		Set<Map.Entry<String, Student>> et=a1.entrySet();

		for(Iterator<Map.Entry<String, Student>> it=et.iterator(); it.hasNext(); ) {
			println(it.next().getValue());
			it.remove();
		}
		println("et.isEmpty: " + et.isEmpty());
		println("a1.isEmpty: " + a1.isEmpty());
		
	}

	public static void println(Object o) {
		System.out.println(o);
	}
}
  三. 这程序实在是基础的不能再基础,不过这也是容器中最常用的部分。如果再深入的话,就是容器的底层实现,容器的同步和锁定了。有时间再来完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值