JAVASE_集合框架

来源:互联网 发布:所有云计算上市公司 编辑:程序博客网 时间:2024/06/11 22:45

1.集合类:面向对象语言对事物的体现都是以对象的形式,所以为了方便操作多个对象的操作,就对对象进行存储。2.数组和集合类有何不同:<span style="white-space:pre"></span>数组可以存储对象和基本数据类型,但是数组的长度是不能改变的。集合类只能存储对象,长度可以变化。<span style="white-space:pre"></span>3.集合类的特点:集合类只能存储对象,集合长度是可变的。集合可以存储不同类型的对象。4.都继承于Collection接口<span style="white-space:pre"></span>Collection——<span style="white-space:pre"></span>|——List<span style="white-space:pre"></span>|<span style="white-space:pre"></span>|——Set5.list1.retainAll(list2);//取list1和list2的交集6.迭代器:Iterator it = al.iterator();while(it.hasNext()){<span style="white-space:pre"></span>Syso(...);} for(Iterator it = al.iterator ; it.hasNext() ; ){//优于前<span style="white-space:pre"></span>Syso(...);}7.List下有其继承Iterator的迭代器,名为ListIterator,使用迭代器操作元素时,不可以使用集合对象的方法操作集合中的元素。会发生异常。所以在迭代时,采用迭代器的方法来操作元素,但是Iterator方法是有限的,只能对元素进行判断,取出,删除操作,如若需要其他的操作,如添加和修改等,就要使用其子接口ListIterator8.set集合元素无序,元素存入顺序和取出顺序不一定一致。元素不能重复。set集合的功能和Collection功能一样|--set    |--HashSet:数据结构是哈希表,数据结构结构是非同步的,保证元素唯一性的原理是,判断元素的hashCode值是否相同,如果相同还会判断equals方法是够为真    |<span style="white-space:pre"></span>|---LinkedHashList    |    |--TreeSet:可以对set集合中的元素进行排序。数据结构是二叉树。元素确定唯一性和比较的方法都是通过compareTo()方法来完成,return 1,0,-1表示大,相等,小。<span style="white-space:pre"></span>9.hashCode()和equals()<span style="white-space:pre"></span>equals()相等的两个对象,hashcode()一定相等; <span style="white-space:pre"></span>equals()不相等的两个对象,却并不能证明他们的hashcode()不相等。换句话说,equals()方法不相等的两个对象,hashcode()有可能相等。(我的理解是由于哈希码在生成的时候产生冲突造成的)。 <span style="white-space:pre"></span>反过来:hashcode()不等,一定能推出equals()也不等;hashcode()相等,equals()可能相等,也可能不等。<span style="white-space:pre"></span><span style="white-space:pre"></span>在java的集合中,判断两个对象是否相等的规则是: <span style="white-space:pre"></span>1),判断两个对象的hashCode是否相等 <span style="white-space:pre"></span>      如果不相等,认为两个对象也不相等,完毕 <span style="white-space:pre"></span>      如果相等,转入2) <span style="white-space:pre"></span>(这一点只是为了提高存储效率而要求的,其实理论上没有也可以,但如果没有,实际使用时效率会大大降低,所以我们这里将其做为必需的。后面会重点讲到这个问题。) <span style="white-space:pre"></span>2),判断两个对象用equals运算是否相等 <span style="white-space:pre"></span>      如果不相等,认为两个对象也不相等 <span style="white-space:pre"></span>      如果相等,认为两个对象相等(equals()是判断两个对象是否相等的关键)<span style="white-space:pre"></span><span style="white-space:pre"></span>*hashcode是对属性比较多的对象简单的equals方法的替代. <span style="white-space:pre"></span>10.要对set元素去重复或者添加重复元素可对其hashCode已经equals方法进行重写<span style="white-space:pre"></span>public class person(){<span style="white-space:pre"></span><span style="white-space:pre"></span>public person(int num , String name){<span style="white-space:pre"></span>this.num = num;<span style="white-space:pre"></span>this.name = name;<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>private int num;<span style="white-space:pre"></span>private String sex;<span style="white-space:pre"></span><span style="white-space:pre"></span>....<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>//当以上的javabean用户的名称和性别相同时,我们把他看做一个人存入hashset中时,此时,我们就需要对person的hashcode方法和equals方法进行重写<span style="white-space:pre"></span><span style="white-space:pre"></span>改为:<span style="white-space:pre"></span>public class person(){<span style="white-space:pre"></span>public person(int num , String name){<span style="white-space:pre"></span>this.num = num;<span style="white-space:pre"></span>this.name = name;<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>private int num;<span style="white-space:pre"></span>private String sex;<span style="white-space:pre"></span><span style="white-space:pre"></span>....<span style="white-space:pre"></span><span style="white-space:pre"></span>public int hashCode(){//hashSet先判断hashcode再判断equals方法,来决定元素的唯一性。<span style="white-space:pre"></span>return name.hashcode() + num;<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>public boolean equals(Obejct o){//此处的参数一定是object类型,覆盖父类的equals方法。<span style="white-space:pre"></span><span style="white-space:pre"></span>if( !(o instanceof Person))<span style="white-space:pre"></span>return false;<span style="white-space:pre"></span><span style="white-space:pre"></span> Student s = (Student)o;<span style="white-space:pre"></span>return num==s.num && name.equals ? (s.name)?ture:false;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>*contains方法也是通过:hashCode()和equals()方法进行判断的。remove方法也是。<span style="white-space:pre"></span>11.Map接口<span style="white-space:pre"></span>|---Map<K,V> 该集合存键值对,一对一的往里面存,而且保证键的唯一性。<span style="white-space:pre"></span> |<span style="white-space:pre"></span> |---HashTable:底层是哈希表数据结构,不可以存入null键和null值,该集合是线程同步的。效率低<span style="white-space:pre"></span> |---HashMap:底层是哈希表数据结构,并允许使用null键和null值,该集合是线程不同步的。效率高<span style="white-space:pre"></span> |---TreeMap:底层是二叉树数据结构,线程不同步,可对键排序。<span style="white-space:pre"></span> <span style="white-space:pre"></span>map添加元素如果添加相同的元素(键相同),原来的元素会被替换,并被返回。<span style="white-space:pre"></span><span style="white-space:pre"></span>遍历map集合:<span style="white-space:pre"></span>遍历方式一:<span style="white-space:pre"></span>Set<String> key = map.keySet();//将key取出,存入到set<span style="white-space:pre"></span>Iterator it =key.iterator();<span style="white-space:pre"></span><span style="white-space:pre"></span>while(it.hasNext()){<span style="white-space:pre"></span>String key = it.next();<span style="white-space:pre"></span>String value = map.get("key");<span style="white-space:pre"></span>Syso("key:"+key+",value:"+value);<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>set<Map.Entry<K,V>> entrySet:将map集合中的映射关系存到set集合中,而这个关系的数据类型就是:Map.Entry<span style="white-space:pre"></span><span style="white-space:pre"></span>遍历方式二:<span style="white-space:pre"></span>Set<Map.Entry<Stirng,String>> entrySet = map.entrySet();//将关系取出,存入到set中<span style="white-space:pre"></span>Iterator<Map.Entry<String,String>> it = entrySet.iterator();<span style="white-space:pre"></span><span style="white-space:pre"></span>while(it.hasNext()){<span style="white-space:pre"></span>Map.Entry<String,String> me = it.next();<span style="white-space:pre"></span>String key = me.getKey();<span style="white-space:pre"></span>String value = me.getValue();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>12.集合框架工具类<span style="white-space:pre"></span>|--Collections:于Connection为接口,Connections为工具类,内部方法都是静态方法,这些方法都是操作集合的,可对集合排序,添加元素,折半查找。<span style="white-space:pre"></span>|--Arrays:用于操作数组的工具类,里面都是静态方法。可进行数组元素折半查找和复制。可对数组转换成list集合。asList();<span style="white-space:pre"></span>@SuppressWarnings("rawtypes")public class A implements Comparable{//实现排序接口int field=100;public A(int field){this.field = field;}//重写A的equals方法,用于判断与一个对象是否是同一值。此处把对象引用相等和filed相等的同类对象视为相同的值public boolean equals(Object obj){if(this == obj) return true;if(obj != this && obj.getClass()== A.class){A a = (A) obj;if(this.field == a.field) return true;}return false;}//重写compareTo,根据filed的大小来比较对象的大小public int compareTo(Object obj){A a = (A)obj;return this.field > a.field ? 1 : this.field < a.field ? -1:0; //可用过大于返回-1,小于返回1改成降序排序}}
package javaIO集合类练习题;


import java.util.Iterator;
import java.util.TreeSet;


public class TestTreeSet {


public static void main(String[] args){
TreeSet<A> ts  = new TreeSet<A>();
ts.add(new A(3));
ts.add(new A(1));
ts.add(new A(4));
System.out.println(ts.add(new A(5)));
System.out.println(ts.add(new A(3)));
A a = (A)ts.first();//SET集合中的排序filed尽量少的做改变,建议TreeSet中的值不去改变,只做工具类
//ts.remove(new A(1));
//a.field = 100;
//ts.add(a);
System.out.println(a.field);
Iterator it = ts.iterator();
while(it.hasNext()){
System.out.print(( (A)it.next()).field+" ,");
}
}
}
 package javaIO集合类练习题.Map集合的实现;


import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;


/**
 * 采用继承HashSet的方式来实现HashrMap 其实质是:把key—value值对象的对象当成HashSet的元素,重
 * 写SimplyEntry的类equals方法和hashCode方法,仅对其key做hashCode和equals判断
 * 
 * @author Administrator
 * 
 * @param <K>
 * @param <V>
 */
@SuppressWarnings("serial")
public class Set2Map<K, V> extends HashSet<SimpleEntry<K, V>> {


/**
* 清空hashMap中的所用存在的元素
*/
public void clear() {
super.clear();
}
public boolean containsKey(K key){
return super.contains(new SimpleEntry<K, V>(key,null));
}
/**
* 判断当前hashMap中是否包含key值

* @param key
* @return
*/
public boolean containsValue(Object value) {
for(SimpleEntry<K, V> se : this){
if(se.getValue().equals(value))
return true;
}
return false;
}


/**
* 取出当前hashMap中的值,并且判断该值的key是否等于当前搜索的key,若等于则输出该key的value值

* @param key
* @return
*/
public V get(Object key) {
for (SimpleEntry<K, V> se : this) {
if (se.getKey().equals(key)) {
return se.getValue();
}
}
return null;
}


/**
* 给当前的hashMap放入一个值

* @return
*/
public V put(K key, V value) {
super.add(new SimpleEntry<K, V>(key, value));
return value;
}


/**
* 把一个Map的数据放入当前HashMap中

* @param m
*/
public void putAll(Map<? extends K, ? extends V> m) {
for(K key : m.keySet()){
super.add(new  SimpleEntry< K , V>(key , m.get(key)));
}
}


/**
* 删除key指定的key-value

* @param key
* @return
*/
public V removeEntry(Object key) {
Iterator< SimpleEntry<K, V>> it = this.iterator();
while(it.hasNext()){
SimpleEntry<K, V> se = (SimpleEntry<K,V>)it.next();
if(se.getKey().equals(key)){
V v = se.getValue();
it.remove();
return v;
}
}
return null;
}


/**
* 查看当前Map中含有多少个元素
*/
public int size() {
return super.size();
}
}package javaIO集合类练习题.Map集合的实现;


import java.util.Map;
/**
 * java集合类Map的实现
 * @author Administrator
 *
 * @param <K>
 * @param <V>
 */
@SuppressWarnings("serial")
public class SimpleEntry < K ,V > implements Map.Entry<K, V>,java.io.Serializable{
private final K key;
private V value;

/**
* 初始化时,可以放入对象,也可以存放对象的值
* @param key
* @param value
*/
public SimpleEntry(K key, V value){
this.key = key;
this.value = value;
}
/**
* 可以存放某个对象的子类的类型值
* @param entry
*/
public SimpleEntry(Map.Entry<? extends K,? extends V> entry){
this.key = entry.getKey();
this.value = entry.getValue();
}
@Override
public K getKey() {
return this.key;
}

@Override
public V getValue() {
return value;
}


@Override
public V setValue(V value) {
V oldValue = this.value;
this.value = value;
return oldValue;
}

public boolean equals(Object o){
if(o == this) return true;
if(o.getClass() == SimpleEntry.class){
@SuppressWarnings("rawtypes")
SimpleEntry se = (SimpleEntry) o;
return se.getKey().equals(getKey());
}
return false;
}

public int hashCode(){
return this==null?0:key.hashCode();
}

public String toString(){
return key+" = "+ value;
}
}package javaIO集合类练习题.Map集合的实现;


public class TestMyHashMap {


public static void  main(String[] args){
Set2Map <String , Integer> scores = new Set2Map<String, Integer>();
scores.put("李军", 100);
scores.put("黎明", 112);
scores.put("李明", 96);
System.out.println(scores);
System.out.println(scores.size());
System.out.println("删除李军的成绩后:"+scores.removeEntry("李军")+scores);
System.out.println("是否包含黎明:"+scores.containsKey("李明"));

}
}


0 0
原创粉丝点击