黑马程序员----集合

---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------

集合类的由来

对象用于封装特有数据,对象多了需要存储,如果对象的个数不确定。
就使用集合容器进行存储。 就使用集合容器进行存储。

集合的特点

1.用于储存对象的容器.

2.集合的长度是可变的.

3.集合中不可以存储基本数据类型值。


集合容器因为内部的数据结构不同,有多种具体容器.

不断地向上抽取,就形成了集合框架.


框架的顶层collection接口.


1,添加 ,添加
void add(index,element);
void add(index,collection);
2,删除; ,删除;
Object remove(index): 
3,修改: ,修改:
Object set(index,element); 
4,获取: ,获取:
Object get(index); 
int indexOf(object);

int lastIndexOf(object);

List subList(from,to);


List集合是可以完成对元素的增删查改的


List: 
|-- Vector: 内部是数组据结构,同步的。增删查询都很慢!
|-- ArrayList: 内部是数组据结构,不同步的。替代了 Vector 。 查询的速度快。 
|-- LinkedList: 内部是链表数据结构,不同步的。 增删元素速度很快。


LinkedList: 
addFirst(); 
addLast():
jdk1.6
offerFirst ();
offetLast(); 


getFirst();//获取但不移除,如果链表为空抛出 NoSuchElementException
getLast(); 
jdk1.6 
peekFirst();//获取但不移除,如果链表为空返回 null.
peekLast(): 


removeFirst();//获取并移除,如果链表为空抛出  NoSuchElementException
removeLast(); 
jdk1.6 
pollFirst();//获取并移除, 如果链表为空获取
null并移除

pollLast();


set

Set: 元素不可以重复,是无序。 
Set 接口中的方法和 接口中的方法和  Collection 一致。
|-- HashSet--|: 内部数据结构是哈希表 ,是不同步的。


如何保证该集合的元素唯一性呢?
是通过对象的  hashCode和equals  方法来完成对象唯一性的。 
如果对象的 hashCode  值不同,那么用判断 值不同,那么用判断  equals 方法,就直接存储到哈希表中。
如果对象的 hashCode  值相同,那么要再次判断对象的 equals方法为 true。
如果为 true  ,视为 相同元素不存。如果false ,那么视为不同元素就进行存储。


记住:如果元素要存储到 HashSet集合中,必须覆盖 hashCode方法和 equals 方法。
一般情况下,如果定义的类会产生很多对象比人,学生,书等通常都需要覆盖 hashCode方法和 equals 方法
建立对象判断是否相同的依据。


|-- TreeSet--| 可以对Set集合中的元素进行排序。 是不同步的 
                      判断元素唯一性的方式:就是 根据比较方法的返回结果是否是0,是0,就是相同元素,不存。

TreeSet  对元素进行排序的方式一: 
让元素自身具备比较功能,就需要实现  Comparable 接口。覆盖 compareTo方法。 


如果不要按照对象中具备的自然顺序进行排。如果对象不具备自然顺序,怎么办?
可以使用 TreeSet 集合第二种排序方式:
让集合自身具备比较功能,定义一个类实现 Comparator接口,覆盖compare方法。
将该类对象作为参数传递给 TreeSet 集合的构造函数。



哈希表确定元素是否相同 
1,判断的是两个元素哈希值否相同。如果相同再判断两个对象的内容是否相同.
2,判断哈希值相同,其实是判断的对象hashCode 的方法。判断内容相同,用的是equals方法.

注意:如果哈希值不同是不需要判断equals方法的.


map集合和collection集合的区别.

1.

map中一次存储是键值对.

collection中一次存储是单个元素.

2.

map的存储使用的put方法.

collection存储使用的是add方法.

3.

map的取出,是将map装成set,在使用迭代器取出.

collection取出,使用就是迭代器.

4.

如果对象很多,必须使用容器存储。

如果元素存在着映射关系,可以优先考虑使用map存储或者使用数组.

如果没有映射关系,可以使用collection存储.



map简述:

package cn.itcast.p6.map.demo;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapDemo
{
	public static void main(String[] args)
	{
		Map<Integer,String> map = new HashMap<Integer,String>();
		method_2(map);
	}
	public static void method_2(map)
	{
		map.put(8,"zhaoliu");
		map.put(2,"zhaoliu");
		map.put(2,"zhaoliu");
		map.put(6,"zhaoliu");
		Collection<String> value = map.values();
		Iterator<String> it2 = values.iterator();
		while(it2.hasNext())
		{
			System.out.println(it2.next());
		}
		/*
		通过Map装成set就可以迭代。
		找到了另一个方法,entrySet.
		该方法将键和值得映射关系作为对象,存储在set集合里面,而这个映射关系的类型是map.entrySet();
		类型(结婚证)
		*/
		Set<Map.Entry<Integer,String>>entrySet = map.entrySet();
		Iterator<Map.Entry<Integer,String>> it = entrySet.iterator();
		
		while(it.hasNext())
		{
			Map.Entry<Integer,String> me = it.next();
			Integer key = me.getKey();
			String value = me.getValue();
			System.out.prinltn(key+"::::"+value);
		}
		/*
		取出map中的所有元素
		原理:通过keyset方法获取map中的所有的键所在的se集合,在通过set迭代器,获取到每一个键.
		在对每一个键通过map集合的get方法获取其对应的值即可.
		
		Set<Integer> keySet = map.keySet();
		Iterator<Integer> it = keySet.iterator();

		while(it.hasNext())
		{
			Integer key = it.next();
			String value = map.get(key);
			System.out.println(key+":"+value);
		}*/
		public static void method(Map<Integer,String>map)
		{
			System.out.println(map.put(8,"wangcai"));
			System.out.println(map.put(8,"xiaoqiang"));
			map.put(2,"zhangsan");
			map.put(7,"zhaoliu");
			//删除
			//System.out,println("remove:"+map.remove(2));
			//判断
			//System.out.println("get:"+map.get(6));
			//获取
			System.out.println("get:"+map.get(6));
			System.out.prinltn(map);
			Outer.Inner.show();
		}
	}
	interface MyMap()
	{
		public static interface MyEntry
		{
			void get();
		}
	}
	class MyDemo implements MyMap.MyEntry
	{
		public void get(){}
	}
	class Outer
	{
		static class Inner
		{
			static void show();
		}
	}
}

其中map里卖有HashMap和TreeMap。

其中HashMap简述:

package cn.itcast.p7.hashmap.demo;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import cn.itcast.p2.bean.Student;
public class HashMapDemo {
/**
* @param args
*/166 

	public static void main(String[] args) {
/*
* 将学生对象和学生的归属地通过键与值存储到map集合中。
*/
		HashMap<Student,String> hm = new HashMap<Student,String>();
		hm.put(new Student("lisi",38),"北京");
		hm.put(new Student("zhaoliu",24),"上海");
		hm.put(new Student("xiaoqiang",31),"沈阳");
		hm.put(new Student("wangcai",28),"大连");
		hm.put(new Student("zhaoliu",24),"铁岭");
		//Set<Student> keySet = hm.keySet();
		//Iterator<Student> it = keySet.iterator();
		Iterator<Student> it = hm.keySet().iterator();
		while(it.hasNext()){
			Student key = it.next();
			String value = hm.get(key);
			System.out.println(key.getName()+":"+key.getAge()+"---"+value);
			}
		}
}
TreeMap概述:

package cn.itcast.p8.treemap.demo;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import cn.itcast.p2.bean.Student;
import cn.itcast.p3.comparator.ComparatorByName;
public class TreeMapDemo {
/**
* @param args
*/
	public static void main(String[] args) {
		TreeMap<Student,String> tm = new TreeMap<Student,String>(new ComparatorByName());167 

		tm.put(new Student("lisi",38),"北京");
		tm.put(new Student("zhaoliu",24),"上海");
		tm.put(new Student("xiaoqiang",31),"沈阳");
		tm.put(new Student("wangcai",28),"大连");
		tm.put(new Student("zhaoliu",24),"铁岭");
		Iterator<Map.Entry<Student, String>> it = tm.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry<Student,String> me = it.next();
			Student key = me.getKey();
			String value = me.getValue();
			System.out.println(key.getName()+":"+key.getAge()+"---"+value);
			}
		}
}
collection概述:

package cn.itcast.p2.toolclass.collections.demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import cn.itcast.p2.comparator.ComparatorByLength;172 

public class CollectionsDemo {
	public static void main(String[] args) {
	/*
	* Collections:是集合框架的工具类。
	* 里面的方法都是静态的。
	*/
		demo_4();
}
	public static void demo_4() {
		List<String> list = new ArrayList<String>();
		list.add("abcde");
		list.add("cba");
		list.add("zhangsan");
		list.add("zhaoliu");
		list.add("xiaoqiang");
		System.out.println(list);
		//Collections.replaceAll(list, "cba", "nba"); // set(indexOf("cba"),"nba");
		Collections.shuffle(list);
		//Collections.fill(list, "cc");
		System.out.println(list);
		}
	public static void demo_3() {
	/*
	TreeSet<String> ts = new TreeSet<String>(new Comparator<String>(){
	@Override
	public int compare(String o1, String o2) {
	int temp = o2.compareTo(o1);
	return temp;
	}
	});
		*/
		TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder(new ComparatorByLength()));
		ts.add("abc");
		ts.add("hahaha");
		ts.add("zzz");
		ts.add("aa");173 

		ts.add("cba");
		System.out.println(ts);
	}
	public static void demo_2(){
		List<String> list = new ArrayList<String>();
		list.add("abcde");
		list.add("cba");
		list.add("aa");
		list.add("zzz");
		list.add("cba");
		list.add("nbaa");
		//Collections.sort(list);
		System.out.println(list);
		//int index = Collections.binarySearch(list, "cba");
		//System.out.println("index="+index);
		//获取最大值。
		String max = Collections.max(list,new ComparatorByLength());
		System.out.println("max="+max);
		}
	public static void demo_1(){
		List<String> list = new ArrayList<String>();
		list.add("abcde");
		list.add("cba");
		list.add("aa");
		list.add("zzz");
		list.add("cba");
		list.add("nbaa");
		System.out.println(list);
		//对list集合进行指定顺序的排序。
		//Collections.sort(list);
		//mySort(list);
		//mySort(list,new ComparatorByLength());
		Collections.sort(list,new ComparatorByLength());
		System.out.println(list);
		}
	public static <T> voidmySort(List<T> list,Comparator<? super T> comp){174 

		for (int i = 0; i < list.size()-1; i++) {
		for (int j = i+1; j < list.size(); j++) {
		if(comp.compare(list.get(i), list.get(j))>0){
		//T temp = list.get(i);
		//list.set(i, list.get(j));
		//list.set(j, temp);
		Collections.swap(list, i, j);
		}
	}
	}
}
	public static <T extends Comparable<? super T>> void mySort(List<T> list){
		for (int i = 0; i < list.size()-1; i++) {
		for (int j = i+1; j < list.size(); j++) {
		if(list.get(i).compareTo(list.get(j))>0){
		//T temp = list.get(i);
		//list.set(i, list.get(j));
		//list.set(j, temp);
		Collections.swap(list, i, j);
		}
	}
	}
}
}




---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值