java类集框架总结

Collection是一个基础的类集接口,其下有两个主要的接口list和set,list下又有ArrayList和vector

ArrayList

通用方法:

public boolean add()

public boolean remove()

public boolean contains()

public boolean isEmpty()

public void  clear()

public int size()

public Objiect[] toArray()

public Iterator<E>Irerator()

特有方法:

public ListIterator<E>ListIrerator()

public E get(int index)

public E set(int index,E element)

代码演示:

public class ArrayListAddDemo {
	public static void main(String args[]) {
		List<String> allList = new ArrayList<String>();// 为List接口实例化
		allList.add("hello");// 增加元素,Collection接口定义
		allList.add(0, "MLDN");// 增加元素,List接口定义
		allList.add("world");// 增加元素,Collection接口定义
		allList.add("world");// 增加元素,Collection接口定义
		allList.add("world");// 增加元素,Collection接口定义
		allList.add("world");// 增加元素,Collection接口定义
		allList.add("world");// 增加元素,Collection接口定义
		System.out.println(allList) ;
	}
}


public class ArrayListPrintDemo02 {
	public static void main(String args[]) {
		Collection<String> allList = new ArrayList<String>();// 为Collection接口实例化
		allList.add("hello");// 增加元素,Collection接口定义
		allList.add("MLDN");// 增加元素,Collection接口定义
		allList.add("world");// 增加元素,Collection接口定义
		Object obj[] = allList.toArray() ;
		for (int i = 0; i < obj.length; i++) {// 循环输出
			System.out.println(obj[i]) ;
		}
	}
}
ArrayList与vector的主要区别是ArrayList采用异步处理,且非线程安全,vector则采用同步处理,线程安全。


Set
Set接口有两个子类分别是HashSet与TreeSet

HashSet通过散列方式无序的存放集合数据。

public class HashSetPersonAddDemo {
	public static void main(String[] args) {
		Set<Person> allSet = new HashSet<Person>();
		allSet.add(new Person("张三",30)); // 增加重复元素
		allSet.add(new Person("李四",30)); // 增加重复元素
		allSet.add(new Person("王五",31)); // 增加重复元素
		allSet.add(new Person("赵六",32)); // 增加重复元素
		allSet.add(new Person("孙七",32)); // 增加重复元素
		allSet.add(new Person("孙七",32)); // 增加重复元素
		System.out.println(allSet);
	}
}

此时无序输出所有数据,重复元素并未被插入

TreeSet默认通过类的CompareTo方法来实现顺序排列(类必须先实现Comparble接口)

public class Person implements Comparable<Person> {
	private String name;
	private int age;

	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public String toString() { // 覆写toString()
		return "姓名:" + this.name + ";年龄:" + this.age;
	}

	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof Person)) {
			return false;
		}
		Person p = (Person) obj;
		if (this.name.equals(p.name) && this.age == p.age) {
			return true;
		} else {
			return false;
		}
	}

	public int hashCode() { // 这个方法的返回值都是通过一个公式计算的
		// 此时的公式:名字的hashCode * age
		return this.name.hashCode() * this.age;
	}

	public int compareTo(Person o) {
		if (this.age > o.age) {
			return 1;
		} else if (this.age < o.age) {
			return -1;
		} else {
			return this.name.compareTo(o.name); // 比较name
		}
	}
}
public class TreeSetPersonAddDemo {
	public static void main(String[] args) {
		Set<Person> allSet = new TreeSet<Person>();
		allSet.add(new Person("张三",30)); // 增加重复元素
		allSet.add(new Person("李四",30)); // 增加重复元素
		allSet.add(new Person("王五",31)); // 增加重复元素
		allSet.add(new Person("赵六",32)); // 增加重复元素
		allSet.add(new Person("孙七",32)); // 增加重复元素
		allSet.add(new Person("孙七",32)); // 增加重复元素
		System.out.println(allSet);
	}
}
此时按照年龄顺序输出数据


Iterator(迭代)

学过C++的都很好理解这个接口的用法和含义,不再赘述

常用方法

public boolean hasnext()      判断是否有下一个数据

public E next()                      获取当前数据

public void remove()             移除当前数据

public class IteratorDemo02 {
	public static void main(String[] args) {
		List<String> allList = new ArrayList<String>();
		allList.add("A");
		allList.add("B");
		allList.add("C");
		allList.add("D");
		allList.add("E");
		Iterator<String> iter = allList.iterator();
		while (iter.hasNext()) {
			String str = iter.next(); // 取出内容
			if ("C".equals(str)) {
				iter.remove();// 删除元素
			}
			System.out.print(str + "、");
		}
		System.out.println("删除之后的集合:" + allList);
	}
}
ListIterator(双向迭代)
独有方法:

public boolean hasprevios()            判断是否有前一个元素

public E previous()                          取出前一个元素

public class ListIteratorDemo01 {
	public static void main(String[] args) {
		List<String> allList = new ArrayList<String>();
		allList.add("A");
		allList.add("B");
		allList.add("C");
		allList.add("D");
		allList.add("E");
		ListIterator<String> iter = allList.listIterator();
		System.out.print("从前向后输出:");
		while (iter.hasNext()) {
			System.out.print(iter.next() + "、");
		}
		System.out.print("\n从后向前输出:");
		while (iter.hasPrevious()) {
			System.out.print(iter.previous() + "、");
		}
	}

}

注意:

迭代后的对象可以和原始对象混用方法

public class ListIteratorDemo02 {
	public static void main(String[] args) {
		List<String> allList = new ArrayList<String>();
		allList.add("A");
		allList.add("B");
		allList.add("C");
		allList.add("D");
		allList.add("E");
		ListIterator<String> iter = allList.listIterator();
		iter.add("X"); // 增加数据
		System.out.print("从前向后输出:");
		while (iter.hasNext()) {
			String str = iter.next();
			iter.set(str + " - MLDN"); // 修改
			System.out.print(str + "、");
		}
		System.out.print("\n从后向前输出:");
		while (iter.hasPrevious()) {
			System.out.print(iter.previous() + "、");
		}
	}

}

Map

HashMap

public V put(K key,V value)                                 

public V get(Object key)

public Set<K>keySet()                                    将Map中的所有key以Set集合的方式返回

public Set<Map.Entry<K,V>>entrySet()          将Map集合变成Set集合

public class HashMapDemo04 {
	public static void main(String[] args) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("zhangsan", 1);
		map.put("zhangsan", 2);
		map.put("lisi", 3);
		map.put("wangwu", 5);
		Set<String> set = map.keySet(); // 返回全部的key
		Iterator<String> iter = set.iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			System.out.println(key + " --> " + map.get(key));
		}
	}
}

使用Iterator输出Map集合

1)通过 entrySet()将Map集合变成Set集合

2)利用iterator()方法取得Iterator的实例化对象

3)迭代找到每一个Map.Entry对象

public class IteartorMapDemo {
	public static void main(String[] args) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("zhangsan", 1);
		map.put("zhangsan", 2);
		map.put("lisi", 3);
		map.put("wangwu", 5);
		Set<Map.Entry<String, Integer>> allSet = null;
		allSet = map.entrySet();
		Iterator<Map.Entry<String, Integer>> iter = allSet.iterator();
		while (iter.hasNext()) {
			Map.Entry<String, Integer> me = iter.next();
			System.out.println(me.getKey() + " --> " + me.getValue());
		}
	}
}

HashTable已被淘汰,HashMap采用异步,非线程安全,且允许将key或value设置为null。

Map注重查找,而Collection注重输出

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值