------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

类集是一个动态对象数组,不受对象数组长度的限制。

List集合里面的内容允许重复,Set集合不允许重复,重复的内容靠hashCode()和equals()两个方法区分,Iterator只能从前往后单向输出,ListIterator可以双向输出

SortedSet 单值得排序接口,可以使用比较器排序,SortedMap 一对值的排序接口,按key排序

Queue 队列接口,可以实现队列操作

Map.Entry 每个Map.Entry对象都保存着一对key-value的内容,每个Map接口保存着多个Map.Entry实例??

jdk1.5以后类集才增加了对泛型的支持

Collection接口常用方法

add():插入对象

allAll():将一个集合中的内容加入进来

clear():清除集合中所有内容

contains():判断对象是否在集合中

containsAll():判断一组对象是否在集合中

equals():对象比较

hashCode():哈希码

isEmpty():集合是否为空

iterator():为Iterator接口实例化

remove():删除指定对象

removeAll():删除一组对象

retainAll(c):删除集合c中不包含的

size():集合的大小

toArray():将一个集合变为对象数组、

toArray(T[] c):将一个集合变为对象数组,指定好返回的对象数组类型

开发中提倡使用Collection接口的子接口

List接口常用方法

add(int,E):在指定位置增加元素

addAll(int,c):在指定位置增加一组元素

get():返回指定位置的元素

indexOf():查找指定元素的位置

lastIndexOf():从后往前查找指定元素的位置

listIterator():为ListIterator接口实例化

remove(int):删除指定位置的元素

subList(int,int)取出集合中的子集合

set(int,E):替换指定位置的元素

删除对象

将集合变为对象数组

public class ArrayListDemo04{public static void main(String args[]){List<String> allList = null ;allList = new ArrayList<String>() ;   // 指定操作的泛型为StringallList.add("Hello") ;   // 此方法由Collection接口而来allList.add(0,"World") ; // 在第一个位置上添加新的内容allList.add("itheima") ;  // 向Collection中加入内容allList.add("www.itheima.com") ;String str[] = allList.toArray(new String[]{}) ;  // 指定好类型System.out.print("指定数组类型:") ;for(int i=0;i<str.length;i++){System.out.print(str[i] + "、") ;}System.out.print("\n返回对象数组:") ;Object obj [] = allList.toArray() ;  // 返回Object类型for(int i=0;i<obj.length;i++){String temp = (String)obj[i] ;    // 进行向下转型System.out.print(temp + "、") ;}}
};

ArrayList       异步性能高   非线程安全   新       Iterator foreach 输出

Vector            同步性能低  线程安全       旧       Iterator foreach enumeration 输出

Queue接口

FIFO,常用方法

element():找到链表的表头,如果队列为空,则抛出一个NoSuchElementException

offer():将指定元素加入到链表的结尾

peek(): 找到链表的表头,如果队列为空,则返回null

poll():移除并返回队列头部的元素    如果队列为空,则返回null

remove():移除并返回队列头部的元素    如果队列为空,则抛出一个NoSuchElementException

LinkedList类实现了Queue接口

常用方法:

addFirst():链表开头增加元素

addLast():链表结尾增加元素

boolean offer(): 链表结尾增加元素,返回结果

removeFirst():删除链表第一个元素

removeLast():删除链表最后一个元素

Set接口

不能放入重复的元素

HashSet是Set的子类,采用散列的存储方式,所以没有顺序

TreeSet是有顺序的

public class TreeSetDemo01{public static void main(String args[]){Set<String> allSet = new TreeSet<String>() ;allSet.add("C") ;   // 增加内容allSet.add("C") ;  // 重复内容allSet.add("C") ;  // 重复内容allSet.add("D") ;  // 增加内容allSet.add("B") ;  // 增加内容allSet.add("A") ;  // 增加内容allSet.add("E") ;  // 增加内容System.out.println(allSet) ;}
};

运行结果:[A, B, C, D, E]

自定义类的对象放入TreeSet集合中时,对象所在的类必须实现Comparable接口!

import java.util.Set ;
import java.util.TreeSet ;
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(){return "姓名:" + this.name + ";年龄:" + this.age ;}public int compareTo(Person per){if(this.age>per.age){return 1 ;}else if(this.age<per.age){return -1 ;}else{return 0 ;}}
};
public class TreeSetDemo03{public static void main(String args[]){Set<Person> allSet = new TreeSet<Person>() ;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)) ;allSet.add(new Person("赵六",33)) ;allSet.add(new Person("孙七",33)) ;System.out.println(allSet) ;}
};

运行结果:[姓名:张三;年龄:30, 姓名:李四;年龄:31, 姓名:王五;年龄:32, 姓名:赵六;年龄:33]

重复的王五对象只剩一个了,但是赵六和孙七只是年龄相同,姓名并不相同,因为如果某个属性没有进行比较的指定,比较器会认为是同一个对象,因此比较器中增加姓名的比较

public int compareTo(Person per){if(this.age>per.age){return 1 ;}else if(this.age<per.age){return -1 ;}else{return this.name.compareTo(per.name) ; // 调用String中的compareTo()方法}}

运行结果:[姓名:张三;年龄:30, 姓名:李四;年龄:31, 姓名:王五;年龄:32, 姓名:孙七;年龄:33, 姓名:赵六;年龄:33]

此时出现了孙七,也去掉了重复元素,但不是真正去掉重复元素,而是依靠Comparable接口完成的,如果换成HashSet还是会有重复元素,如果要真正意义上去掉重复元素,得依靠Object类中的equals方法和hashCode方法

class Person{private String name ;private int age ;public Person(String name,int age){this.name = name ;this.age = age ;}public boolean equals(Object obj){    // 覆写equals,完成对象比较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(){return this.name.hashCode() * this.age  ; // 定义一个公式}public String toString(){return "姓名:" + this.name + ";年龄:" + this.age ;}
};
public class RepeatDemo02{public static void main(String args[]){Set<Person> allSet = new HashSet<Person>() ;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)) ;allSet.add(new Person("赵六",33)) ;allSet.add(new Person("孙七",33)) ;System.out.println(allSet) ;}
};

重写equals方法:判断对象是否重复

hashCode可以理解为一个对象的编码。可以讲类中的全部属性进行适当的计算,求出一个不会 重复的哈希码

实际开发中经常遇到区分是否同一个类的问题,所以一个完整的类最好覆写Object类toString,hashCode,equals方法

SortedSet接口

public class TreeSetDemo05{public static void main(String args[]){SortedSet<String> allSet = new TreeSet<String>() ;    // allSet.add("A") ;  allSet.add("B") ; allSet.add("C") ; allSet.add("C") ; allSet.add("C") ; allSet.add("D") ; allSet.add("E") ; System.out.println("第一个元素:" + allSet.first()) ;     System.out.println("最后一个元素:" + allSet.last()) ;System.out.println("headSet元素:" + allSet.headSet("C")) ;  //返回开始到指定元素的集合System.out.println("tailSet元素:" + allSet.tailSet("C")) ;//从指定元素到最后System.out.println("subSet元素:" + allSet.subSet("B","D")) ;//返回指定对象间的元素}
};

运行结果:

第一个元素:A
最后一个元素:E
headSet元素:[A, B]
tailSet元素:[C, D, E]
subSet元素:[B, C]
集合的输出

Iterator 迭代输出 并删除指定内容

public class IteratorDemo02{public static void main(String args[]){List<String> all=  new ArrayList<String>() ; // all.add("hello") ;all.add("_") ;all.add("world") ;Iterator<String> iter = all.iterator() ;  // 为Iterator接口实例化while(iter.hasNext()){ // 判断是否有内容String str = iter.next() ;if("_".equals(str)){iter.remove() ;  // 删除元素     }else{System.out.println(str) ; // 输出内容}}System.out.println("删除之后的集合:" + all) ;}
};

运行结果:

hello
world
删除之后的集合:[hello, world]

删除时如果使用了List接口中的remove而不是Iterator中的remove,则会出现运行时错误

public class IteratorDemo03{public static void main(String args[]){List<String> all=  new ArrayList<String>() ; // all.add("hello") ;all.add("_") ;all.add("world") ;Iterator<String> iter = all.iterator() ;  // 为Iterator接口实例化while(iter.hasNext()){ // 判断是否有内容String str = iter.next() ;if("_".equals(str)){all.remove(str) ;    // 删除元素     }else{System.out.println(str) ; // 输出内容}}System.out.println("删除之后的集合:" + all) ;}
};

运行结果:

hello

删除之后的集合:[hello, world]

内容虽然删除了,但是迭代输出在内容删除之后就终止了,因为集合本身的内容被破坏掉,迭代出现错误,停止输出

双向迭代输出 ListIterator

常用方法
hasNext():判断是否有下一个值

next():取出下一个元素

remove():移除当前元素

add():加入指定元素

hasPrevious():判断是否有上一元素

previous():取出上一个元素

nextIndex():下一个元素的索引号

previousIndex():上一个元素的索引号

set():替换元素

ListIterator接口只能通过List接口实例化,只能输出list接口中的内容

public class ListIteratorDemo01{public static void main(String argsp[]){List<String> all = new ArrayList<String>() ;all.add("hello") ;all.add("_") ;all.add("world") ;ListIterator<String> iter = all.listIterator() ;System.out.print("由前向后输出:") ;while(iter.hasNext()){String str = iter.next() ;System.out.print(str + "、") ;}System.out.print("\n由后向前输出:") ;while(iter.hasPrevious()){String str = iter.previous() ;System.out.print(str + "、") ;}}
};

在使用双向输出时,如果想从后往前输出,必须先从前往后输出。

增加和替换集合中的内容

public class ListIteratorDemo02{public static void main(String argsp[]){List<String> all = new ArrayList<String>() ;all.add("hello") ;all.add("_") ;all.add("world") ;ListIterator<String> iter = all.listIterator() ;System.out.print("由前向后输出:") ;while(iter.hasNext()){String str = iter.next() ;System.out.print(str + "、") ;iter.set("ZQT- " + str) ;  // 修改内容}System.out.print("\n由后向前输出:") ;iter.add("ZQT");while(iter.hasPrevious()){String str = iter.previous() ;System.out.print(str + "、") ;}}
};

运行结果:

由前向后输出:hello、_、world、
由后向前输出:ZQT、ZQT - world、ZQT - _、ZQT - hello、

Map接口

常用方法

clear:清空map集合

containskey:判断指定的key是否存在

containsValue:判断指定的value是否存在

entrySet:将Map对象变为Set集合

equals:对象比较

get:通过key取得value

hashCode:返回哈希码

isEmpty:判断集合是否为空

keySet:取得所有的key

put:加入元素

putAll:加入集合

remove:根据key删除元素

size:集合的长度

values:取出所有的value

Map.Entry

Map.Entry是使用static关键字声明的内部接口,可以由外部类.内部类的形式调用。专门用来保存key->value的内容

在一般的Map操作中,一般不管Map.Entry接口,只在将Map数据全部输出时在需要用到。

常用方法:

equals:对象比较

getKey:取得key

getValue:取得value

hashCode:返回哈希码

setValue:设置value的值

集合是将key->value数据保存在Map.Entry之后,再在Map集合中插入Map.Entry的实例化对象

Map接口常用子类

HashMap:无序,key不能重复,新

HashTable:无序,key不能重复,旧

TreeMap:按key排序,key不能重复

WeakHashMap:弱引用Map集合,当集合中的内容不再使用时清除掉无用数据,gc回收

IdentityHashMap:key可以重复

输出全部的key和value

public class HashMapDemo03{
<span style="white-space:pre"> </span>public static void main(String args[]){
<span style="white-space:pre">     </span>Map<String,String> map = null; // 声明Map对象,其中key和value的类型为String
<span style="white-space:pre">     </span>map = new HashMap<String,String>() ;
<span style="white-space:pre">     </span>map.put("itheima","www.itheima.com") ;<span style="white-space:pre">  </span>// 增加内容
<span style="white-space:pre">     </span>map.put("csdn","www.csdn.net") ;<span style="white-space:pre">    </span>// 增加内容
<span style="white-space:pre">     </span>map.put("github","www.github.com") ;<span style="white-space:pre">    </span>// 增加内容
<span style="white-space:pre">     </span>Set<String> keys = map.keySet() ;<span style="white-space:pre">    </span>// 得到全部的key
<span style="white-space:pre">     </span>//Collection<String> values = map.values();  //得到全部的value
<span style="white-space:pre">     </span>Iterator<String> iter = keys.iterator() ;
<span style="white-space:pre">     </span>while(iter.hasNext()){
<span style="white-space:pre">         </span>String str = iter.next() ;
<span style="white-space:pre">         </span>System.out.print(str + "、") ;
<span style="white-space:pre">     </span>}
<span style="white-space:pre"> </span>}
};

接受的Set集合指定的泛型一定要和Map中的key泛型一致

HashMap        异步性能较高   非线程安全   新

HashTable     同步性能较低  线程安全       旧

TreeMap

public class TreeMapDemo01{public static void main(String args[]){Map<String,String> map = null; // 声明Map对象,其中key和value的类型为Stringmap = new TreeMap<String,String>() ;map.put("A、itheima","www.itheima.com") ;   // 增加内容map.put("C、csdn","www.csdn.net") ;   // 增加内容map.put("B、github","www.github.com") ;   // 增加内容Set<String> keys = map.keySet() ; // 得到全部的keyIterator<String> iter = keys.iterator() ;while(iter.hasNext()){String str = iter.next() ;System.out.println(str + " --> " + map.get(str)) ; // 取出内容}}
};

结果:

A、itheima --> www.itheima.com
B、github --> www.github.com
C、csdn --> www.csdn.net

上面的代码使用String作为key排序 因为String已经实现了Comparable接口,如果自定义一个类作为key,此类必须实现Comparable接口,否则出现类转换异常
WeakHashMap

public class WeakHashMapDemo01{public static void main(String args[]){Map<String,String> map = null; // 声明Map对象,其中key和value的类型为Stringmap = new WeakHashMap<String,String>() ;map.put(new String("itheima"),new String("www.itheima.com")) ;map.put(new String("csdn"),new String("www.csdn.com")) ;map.put(new String("github"),new String("www.github.com")) ;System.gc() ;    // 强制性进行垃圾的收集操作map.put(new String("zqt"),new String("himi")) ;System.out.println(map) ;}
};

结果:{zqt=himi}
对象引用由4种级别

强引用:当内存不足时,JVM宁可出现OutOfMemoryError时程序停止,也不会回收此对象来释放空间

软引用:内存不足时,会回收这些对象的内存,用来实现内存敏感的告诉缓存

若引用:无论内存是否紧张,被垃圾收集器发现立即回收

虚引用:和没有任何引用一样

Iterator输出Map

public class IteratorDemo04{public static void main(String args[]){Map<String,String> map = new HashMap<String,String>() ;// 声明Map对象,其中key和value的类型为Stringmap.put("A、itheima","www.itheima.com") ;   // 增加内容map.put("C、csdn","www.csdn.net") ;   // 增加内容map.put("B、github","www.github.com") ;   // 增加内容Set<Map.Entry<String,String>> allSet = map.entrySet() ;Iterator<Map.Entry<String,String>> iter = allSet.iterator() ;while(iter.hasNext()){Map.Entry<String,String> me = iter.next() ;System.out.println(me.getKey() + " --> " + me.getValue()) ;}}
};

foreach输出Map

for(Map.Entry<String,String> me:map.entrySet()){System.out.println(me.getKey() + " --> " + me.getValue()) ;}

使用自定义类作为key,应该实现Comparable接口

class Person{private String name ;private int age ;public Person(String name,int age){this.name = name ;this.age = age ;}public String toString(){return "姓名:" + this.name + ";年龄:" + this.age ;}
};
public class HashMapDemo05{public static void main(String args[]){Map<String,Person> map = null ;map = new HashMap<String,Person>() ;map.put("zhangsan",new Person("张三",30)); // 增加内容System.out.println(map.get("zhangsan")) ;}
};

结果:姓名:张三;年龄:30

Map<Person,String> map = new HashMap<Person,String>() ;
<span style="white-space:pre">     </span>map.put(new Person("张三",30),"zhangsan");<span style="white-space:pre">    </span>// 增加内容
<span style="white-space:pre">     </span>System.out.println(map.get(new Person("张三",30))) ;

结果:null

匿名对象,地址发生了变化,所以查不到对应的value

Map<Person,String> map = new HashMap<Person,String>() ;Person per = new Person("张三",30) ;map.put(per,"zhangsan");  // 增加内容System.out.println(map.get(per)) ;

结果:zhangsan

实例化对象,地址没变

如果想使用匿名对象也能查到就必须实现Comparable接口

class Person{private String name ;private int age ;public Person(String name,int age){this.name = name ;this.age = age ;}public String 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(){return this.name.hashCode() * this.age ;}
};
public class HashMapDemo08{public static void main(String args[]){Map<Person,String> map = new HashMap<Person,String>() ;map.put(new Person("张三",30),"zhangsan");  // 增加内容System.out.println(map.get(new Person("张三",30))) ;}
};

IdentityHashMap

key可以重复的Map集合

class Person{private String name ;private int age ;public Person(String name,int age){this.name = name ;this.age = 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(){return this.name.hashCode() * this.age ;}public String toString(){return "姓名:" + this.name + ",年龄:" + this.age ;}
};
public class IdentityHashMapDemo02{public static void main(String args[]){
<span style="white-space:pre">     </span>Map<Person,String> map =new IdentityHashMap<Person,String>() ;
<span style="white-space:pre">     </span>map.put(new Person("张三",30),"zhangsan_1") ;<span style="white-space:pre"> </span>// 加入内容
<span style="white-space:pre">     </span>map.put(new Person("张三",30),"zhangsan_2") ;<span style="white-space:pre"> </span>// 加入内容
<span style="white-space:pre">     </span>map.put(new Person("李四",31),"lisi") ;<span style="white-space:pre">   </span>// 加入内容
<span style="white-space:pre">     </span>Set<Map.Entry<Person,String>> allSet = map.entrySet() ;
<span style="white-space:pre">     </span>Iterator<Map.Entry<Person,String>> iter = allSet.iterator() ;
<span style="white-space:pre">     </span>while(iter.hasNext()){
<span style="white-space:pre">         </span>Map.Entry<Person,String> me = iter.next() ;
<span style="white-space:pre">         </span>System.out.println(me.getKey() + " --> " + me.getValue()) ;
<span style="white-space:pre">     </span>}
<span style="white-space:pre"> </span>}
};

结果:

姓名:张三,年龄:30 --> zhangsan_1
姓名:李四,年龄:31 --> lisi
姓名:张三,年龄:30 --> zhangsan_2
SortedMMap

常用方法

firstKey:返回第一个元素的key

headMap:返回小于等于指定key的部分集合

lastKey:返回最后一个元素的key

subMap:返回指定key范围的集合

tailMap:返回大于指定key的集合

public class SortedMapDemo{public static void main(String args[]){SortedMap<String,String> map = null ;map = new TreeMap<String,String>() ;    // 通过子类实例化接口对象map.put("D、51cto","http://www.51cto.com/") ;map.put("A、itheima","www.itheima.com") ;map.put("C、github","www.github.com") ;map.put("B、stackoverflow","www.stackoverflow.com") ;System.out.print("第一个元素的内容的key:" + map.firstKey()) ;System.out.println(":对应的值:" + map.get(map.firstKey())) ;System.out.print("最后一个元素的内容的key:" + map.lastKey()) ;System.out.println(":对应的值:" + map.get(map.lastKey())) ;System.out.println("返回小于指定范围的集合:") ;for(Map.Entry<String,String> me:map.headMap("B、stackoverflow").entrySet()){System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;}System.out.println("返回大于指定范围的集合:") ;for(Map.Entry<String,String> me:map.tailMap("B、stackoverflow").entrySet()){System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;}System.out.println("部分集合:") ;for(Map.Entry<String,String> me:map.subMap("A、itheima","C、github").entrySet()){System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;}}
};

结果:

第一个元素的内容的key:A、itheima:对应的值:www.itheima.com
最后一个元素的内容的key:D、51cto:对应的值:http://www.51cto.com/
返回小于指定范围的集合:
|- A、itheima --> www.itheima.com
返回大于指定范围的集合:
|- B、stackoverflow --> www.stackoverflow.com
|- C、github --> www.github.com
|- D、51cto --> http://www.51cto.com/
部分集合:
|- A、itheima --> www.itheima.com
|- B、stackoverflow --> www.stackoverflow.com
集合工具类 Collections

1.返回不可变的集合

public class CollectionsDemo01{public static void main(String args[]){List<String> allList = Collections.emptyList() ;    // 返回空的 List集合Set<String> allSet = Collections.emptySet() ;  // 返回空的 List集合allList.add("Hello") ;  // 加入数据  报错}
};

2.为集合增加内容

可以接受可变参数

public class CollectionsDemo02{public static void main(String args[]){List<String> all = new ArrayList<String>() ;  // 返回空的 List集合Collections.addAll(all,"ZQT","csdn","github") ;Iterator<String> iter = all.iterator() ;while(iter.hasNext()){System.out.print(iter.next() + "、") ;}}
};

3.反转,检索

public class CollectionsDemo02{public static void main(String args[]){List<String> all = new ArrayList<String>() ;  // 返回空的 List集合Collections.addAll(all,"ZQT","csdn","github") ;
//      Collections.reverse(all) ;  // 内容反转int point = Collections.binarySearch(all,"csdn") ;    // 检索数据System.out.println("检索结果:" + point) ;Iterator<String> iter = all.iterator() ;while(iter.hasNext()){System.out.print(iter.next() + "、") ;}}
};

4替换

public class CollectionsDemo05{public static void main(String args[]){List<String> all = new ArrayList<String>() ;  // 返回空的 List集合Collections.addAll(all,"zqt","csdn","gihub") ;if(Collections.replaceAll(all,"zqt","himi")){// 替换内容System.out.println("内容替换成功!") ;}System.out.print("替换之后的结果:") ;System.out.print(all) ;}
};

5排序

集合中对象所在的类必须实现Comparable接口

public class CollectionsDemo06{public static void main(String args[]){List<String> all = new ArrayList<String>() ;  // 返回空的 List集合Collections.addAll(all,"1、ithcast","2、ZQT","3、github") ;Collections.addAll(all,"B、www.csdn.net") ;Collections.addAll(all,"A、www.github.com") ;System.out.println("排序之前的集合:" + all) ;Collections.sort(all) ;System.out.println("排序之后的集合:" + all) ;}
};

结果:
排序之前的集合:[1、ithcast, 2、ZQT, 3、github, B、www.csdn.net, A、www.github.com]
排序之后的集合:[1、ithcast, 2、ZQT, 3、github, A、www.github.com, B、www.csdn.net]

6交换位置

public class CollectionsDemo07{public static void main(String args[]){List<String> all = new ArrayList<String>() ;  // 返回空的 List集合Collections.addAll(all,"1、csdn","2、zqt","3、github") ;System.out.println("交换之前的集合:" + all) ;Collections.swap(all,0,2) ;System.out.println("交换之后的集合:" + all) ;}
};

结果:

交换之前的集合:[1、csdn, 2、zqt, 3、github]
交换之后的集合:[3、github, 2、zqt, 1、csdn]
Stack

浏览器回退就是使用栈实现的

常用方法

empty 判断是否为空

peek 查看栈顶,但不删除

pop 出栈

push 入栈

search 查找
Properties

是HashTable的子类

属性文件 key-value 方式保存

window启动引导文件boot.ini 使用属性文件方式保存

1.设置和取得属性

public class PropertiesDemo01{public static void main(String args[]){Properties pro = new Properties() ;    // 创建Properties对象pro.setProperty("BJ","BeiJing") ;  // 设置属性pro.setProperty("TJ","TianJin") ;pro.setProperty("NJ","NanJing") ;   System.out.println("1、BJ属性存在:" + pro.getProperty("BJ")) ;System.out.println("2、SC属性不存在:" + pro.getProperty("SC")) ;System.out.println("3、SC属性不存在,同时设置显示的默认值:" + pro.getProperty("SC","没有发现")) ;}
};

2.将属性保存在普通文件中

public class PropertiesDemo02{public static void main(String args[]){Properties pro = new Properties() ;    // 创建Properties对象pro.setProperty("BJ","BeiJing") ;  // 设置属性pro.setProperty("TJ","TianJin") ;pro.setProperty("NJ","NanJing") ;   File file = new File("D:" + File.separator + "area.properteis") ;    // 指定要操作的文件try{pro.store(new FileOutputStream(file),"Area Info") ;    // 保存属性到普通文件}catch(FileNotFoundException e){e.printStackTrace() ;}catch(IOException e){e.printStackTrace() ;}}
};

结果:

#Area Info
#Wed Oct 29 17:33:34 CST 2014
BJ=BeiJing
NJ=NanJing
TJ=TianJin

3.从普通文件中读取属性内容

public class PropertiesDemo03{public static void main(String args[]){Properties pro = new Properties() ;    // 创建Properties对象File file = new File("D:" + File.separator + "area.properteis") ;   // 指定要操作的文件try{pro.load(new FileInputStream(file)) ;    // 读取属性文件}catch(FileNotFoundException e){e.printStackTrace() ;}catch(IOException e){e.printStackTrace() ;}System.out.println("1、BJ属性存在:" + pro.getProperty("BJ")) ;System.out.println("2、SH属性存在:" + pro.getProperty("SH")) ;}
};

4.将属性保存在xml文件中

public class PropertiesDemo04{public static void main(String args[]){Properties pro = new Properties() ;    // 创建Properties对象pro.setProperty("BJ","BeiJing") ;  // 设置属性pro.setProperty("TJ","TianJin") ;pro.setProperty("NJ","NanJing") ;   File file = new File("D:" + File.separator + "area.xml") ;   // 指定要操作的文件try{pro.storeToXML(new FileOutputStream(file),"Area Info") ;   // 保存属性到普通文件}catch(FileNotFoundException e){e.printStackTrace() ;}catch(IOException e){e.printStackTrace() ;}}
};

结果:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Area Info</comment>
<entry key="BJ">BeiJing</entry>
<entry key="NJ">NanJing</entry>
<entry key="TJ">TianJin</entry>
</properties>
5.从xml文件中读取文件

public class PropertiesDemo05{public static void main(String args[]){Properties pro = new Properties() ;    // 创建Properties对象File file = new File("D:" + File.separator + "area.xml") ;  // 指定要操作的文件try{pro.loadFromXML(new FileInputStream(file)) ; // 读取属性文件}catch(FileNotFoundException e){e.printStackTrace() ;}catch(IOException e){e.printStackTrace() ;}System.out.println("1、BJ属性存在:" + pro.getProperty("BJ")) ;}
};

一对多的关系

一个学校包含多个学生,一个学生属于一个学校

class School{private String name ;private List<Student> allStudents ;public School(){this.allStudents = new ArrayList<Student>() ;}public School(String name){this() ;this.setName(name) ;}public void setName(String name){this.name = name ;}public String getName(){return this.name; }public List<Student> getAllStudents(){return this.allStudents ;}public String toString(){return "学校名称:" + this.name ;}
};
class Student{private String name ;private int age ;private School school; // 一个学生属于一个学校public Student(String name,int age){this.setName(name) ;this.setAge(age) ;}public void setSchool(School school){this.school = school ;}public School getSchool(){return this.school ;}public void setName(String name){this.name = name ;}public void setAge(int age){this.age = age ;}public String getName(){return this.name; }public int getAge(){return this.age ;}public String toString(){return "学生姓名:" + this.name + ";年龄:" + this.age ;}
};
public class TestDemo{public static void main(String args[]){School sch = new School("清华大学") ;   // 定义学校Student s1 = new Student("张三",21) ;Student s2 = new Student("李四",22) ;Student s3 = new Student("王五",23) ;sch.getAllStudents().add(s1) ;sch.getAllStudents().add(s2) ;sch.getAllStudents().add(s3) ;s1.setSchool(sch) ;s2.setSchool(sch) ;s3.setSchool(sch) ;System.out.println(sch) ;Iterator<Student> iter = sch.getAllStudents().iterator() ;while(iter.hasNext()){System.out.println("\t|- " + iter.next()) ;}}
};

结果:

学校名称:清华大学
|- 学生姓名:张三;年龄:21
|- 学生姓名:李四;年龄:22
|- 学生姓名:王五;年龄:23
多对多关系

一个学生可以选多门课程,一门课程有多名学生参加

class Course{private String name ;private int credit ;private List<Student> allStudents ;public Course(){this.allStudents = new ArrayList<Student>() ;}public Course(String name,int credit){this() ;this.name = name ;this.credit = credit ;}public List<Student> getAllStudents(){return this.allStudents ;}public void setName(String name){this.name = name  ;}public void setCredit(int credit){this.credit = credit ;}public String getName(){return this.name ;}public int getCredit(){return this.credit ;}public String toString(){return "课程名称:" + this.name + ";课程学分:" + this.credit ;}
};
class Student{private String name ;private int age ;private List<Course> allCourses ;public Student(){this.allCourses = new ArrayList<Course>() ;}public Student(String name,int age){this() ;this.name = name ;this.age = age ;}public List<Course> getAllCourses(){return this.allCourses ;}public void setName(String name){this.name = name ;}public void setAge(int age){this.age = age ;}public String getName(){return this.name ;}public int getAge(){return this.age ;}public String toString(){return "学生姓名:" + this.name + ";年龄:" + this.age ;}
};
public class TestMore{public static void main(String args[]){Course c1 = new Course("英语",3   ) ; // 第一门课程Course c2 = new Course("计算机",5) ;    // 第二门课程Student s1 = new Student("张三",20) ;Student s2 = new Student("李四",21) ;Student s3 = new Student("王五",22) ;Student s4 = new Student("赵六",23) ;Student s5 = new Student("孙七",24) ;Student s6 = new Student("钱八",24) ;// 第一门课程有三个学生参加c1.getAllStudents().add(s1) ;c1.getAllStudents().add(s2) ;c1.getAllStudents().add(s6) ;s1.getAllCourses().add(c1) ;s2.getAllCourses().add(c1) ;s6.getAllCourses().add(c1) ;// 第二门课程有六个学生参加c2.getAllStudents().add(s1) ;c2.getAllStudents().add(s2) ;c2.getAllStudents().add(s3) ;c2.getAllStudents().add(s4) ;c2.getAllStudents().add(s5) ;c2.getAllStudents().add(s6) ;s1.getAllCourses().add(c2) ;s2.getAllCourses().add(c2) ;s3.getAllCourses().add(c2) ;s4.getAllCourses().add(c2) ;s5.getAllCourses().add(c2) ;s6.getAllCourses().add(c2) ;// 输出一门课程的信息,观察一门课程有多少个学生参加\System.out.println(c1) ;Iterator<Student> iter1 = c1.getAllStudents().iterator() ;while(iter1.hasNext()){Student s = iter1.next() ;System.out.println("\t|- " + s) ;}// 通过学生找到学生参加的课程System.out.println(s6) ;Iterator<Course> iter2 = s6.getAllCourses().iterator() ;while(iter2.hasNext()){Course c = iter2.next() ;System.out.println("\t|- " + c) ;}}
};

黑马程序员——集合框架相关推荐

  1. 黑马程序员-----集合框架类(四) 高级for循环、方法的可变参数及静态导入

    ------- android培训.java培训.期待与您交流! ---------- 黑马程序员-----集合框架类(四) 高级for循环.方法的可变参数及静态导入 1.1 高级for循环(示例1) ...

  2. 黑马程序员————集合框架1(day14)

    ----------------------ASP.Net+Android+IOS开发----------------------期待与您交流! 集合框架1 l  体系概述 l  共性方法 l  迭代 ...

  3. 黑马程序员——集合框架(二)

    ----------- android培训.java培训.java学习型技术博客.期待与您交流! ------------ package com.yang.ex; /*------Set:不包含重复 ...

  4. 黑马程序员-MyBatis 框架-最全入门笔记、阿伟看了都得说真大、真细、真全!!!

    MyBaits 写在最前 本篇文章是我学完黑马程序员 JavaWeb MyBatis 的总结分享,定位呢是最大小白友好的快速入门,欢迎大家一起交流技术! 文章目录 MyBaits 代码仓库 MyBat ...

  5. 黑马程序员——集合Collection:体系详述

    ------<ahref="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培 ...

  6. 黑马程序员--Foundation框架之--NSObject类

    ------IOS培训期待与您交流! ------- OC中的Foundation框架是系统提供的,他就相当于是系统的一套api,其内部有很多现有的类和功能提供给我们使用. 那么今天首先来介绍一下Fo ...

  7. 黑马程序员————集合2(day17)

    ----------------------ASP.Net+Android+IOS开发----------------------期待与您交流! 集合2 l  Collections – sort l ...

  8. 黑马程序员--Foundation框架之--NSArray类以及它的子类NSMutableArray类

    ------IOS培训期待与您交流! ------- 今天我们继续来看一下Foundation框架中的NSArray类和 NSMutableArray类,NSArray类 是不可变的,NSMutabl ...

  9. 黑马程序员-集合(Map)

    ---------------------- ASP.Net+Android+IOS开发..Net培训.期待与您交流! ---------------------- 区分map集合和collectio ...

最新文章

  1. pytorch 状态字典:state_dict 模型和参数保存
  2. mysql修改界面代码_mysql修改编码
  3. Scrapy 框架【学习笔记01】
  4. Web 2.0 编程思想:16条法则(转)
  5. AD19 add pins to nets错误_NGINX 502 Bad Gateway错误疑难解答
  6. Java包数据消息头消息尾_读Socket流时产生阻塞的解决方案(粘包拆包问题)
  7. [转]如何使用WinPE硬盘安装Windows XP
  8. 移动开发或将被颠覆?
  9. GRBL分析:gcode解释器(二)
  10. 自定义validator
  11. 放大器的频率特性(2)-- 共源极的频率特性
  12. 左手指令_屈老师大班科学教案:猜左手猜右手
  13. 腾讯加盟:Kafka-on-Pulsar 项目迎来 2 位腾讯 Maintainer!
  14. MVC、MVP与MVT
  15. T5模型和GPT2模型初步对比
  16. 什么是interop
  17. 最近项目用到Dubbo框架,临时抱佛脚分享一下共探讨。
  18. 无线有线统一认证计费管理平台
  19. javascript设计模式-模块模式(module pattern)
  20. 大数据信息资料采集:公众号武志红文章评论爬取八爪鱼采集器规则

热门文章

  1. 2019网易《Face-to-Parameter Translation for Game Character Auto-Creation》论文解析
  2. hyperopt/hyperas
  3. 13个Python小游戏,可以上班摸鱼玩了一天
  4. 5G需要什么APP小程序?哪里有免费制作5G小程序APP和网站
  5. 给大家介绍一款校园网盘软件,孜创校园网盘软件,搭建学校私有云存储
  6. C#实现QQ窗体功能
  7. CAD教程:CAD看图软件中怎么设置比例?
  8. 地图 显示 动态轨迹_动态轨迹怎么制作?华为Watch GT2轻松搞定
  9. 家庭版Windows10/Windows11不支持远程桌面的解决办法
  10. php课设报告致谢_科学网—博士论文致谢 - 曹墨源的博文