[零基础学JAVA]Java SE应用部分-35.JAVA类集之四
JAVA按以下首要次序·面向目标
·类集结构
·JDBC
·文件编程
本季方案
首要解说以下的内容:
·Collection
·List
·Set
·Map
·Iterator
·ListIteator
·Enumeration
·for…each句子(JDK1.5的新特性)
·Comparable接口
·二叉树
1、类集结构
(1)类集的产生目的
类集简单来讲即是一个动态的目标数组,此目标数组可以改动大小,可以恣意的终结目标的添加、删去、输出。一切的类集寄存java.util包中。
(2)类集的区别
1、寄存单值:Collection
2、寄存一对值:Map
3、输出:Iterator
(3)类集的详细概念
lt;1gt;Collection(只能寄存一个值)
-首要功能:用于输出使用
-子接口<imgsrc="sweat.gif"smilieid="10"border="0"alt=""/>ist(许可有重复的元素,并且参加的次序即是输出的次序)
-子类:ArrayList,是在Java2以后推出的,是新的类,是使用异步管理方式,其功能较高
-子类:Vector,是在JDK1.0的时候就推出,由于是旧的类,有很多List所没有的功能,是使用同步管理方式,其线程安全性较高,会比较慢。使用Vector除了可以使用Iterator输出以外,也可以使用Enumeration进行输出。两者是通用的。Vector子类许可直接使用addElement(Objectobj),也是可以向调集中参加元素的。
-自己新增的方法:
-取每一个目标:publicObjectget(intind);
-子类:Stack(栈)
-子接口:Set(不许可有重复元素,重复元素会更换)?如何可以区别重复元素呢?
-子类:HashSet:是无序列寄存
-子类:TreeSet:有序寄存,是通过Comparable接口终结的
-常用方法:
-添加一个目标:publicbooleanadd(Objectobj)
-获得类集的长度:publicintsize();
-判别调集中的内容是不是为空:publicbooleanisEmpty()
-删去一个目标:publicbooleanremove(Objectobj):就必须牵扯到目标的比较状况。
-实例化Iterator目标:publicIteratoriterator();
lt;2gt;JDK1.5提供了一个方便的输出操作:for…each句子。
lt;3gt;Iterator接口输出是最常见的
-常用方法
-publicbooleanhasNext():判别是不是有下一个元素
-publicObjectnext():取出下一个元素
-子接口<imgsrc="sweat.gif"smilieid="10"border="0"alt=""/>istIterator,可以进行双向输出,只用于List接口有效
-常用方法:具有了Iterator接口中的全面内容
-publicbooleanhasPrevious()
-publicObjectprevious()
lt;4gt;Enumeration(在1.5以后也参加、泛型支持哈~)
-常用方法:
-判别是不是有下一个元素:publicbooleanhasMoreElements()
-取出元素:publicObjectelement()
5、Map(寄存一对值)
-功能:与Collection的功能不一样,Map的首要功能是用于搜索使用的。
-注意点:
-使用Map操作时,不能直接使用Iterator进行输出。
-并且里边不能有重复key。
-假如非要使用Iterator进行输出,则必须按以下的过程进行操作:
-Map--gt;Set--gt;Iterator--gt;Map.Entry--gt;key与value的分别
-常用子类:
-HashMap:无序寄存,是新的类,是JDK1.2时推出的,是异步管理,功能较高
-Hashtable:是旧的类,是JDK1.0时推出的,是线程安全的,功能较低
-Properties(要点):属性文件操作类
-TreeMap:有序寄存
-常用方法:
-向调集中保留数据:publicObjectput(Objectkey,Objectvalue):回来的是value
-从调集中搜索数据:publicObjectget(Objectkey):依据key搜索。
-将Map数据变为Set实例:Setlt;Map.Entrylt;K,Vgt;gt;entrySet()
-将全面的key变为set实例:Setlt;Kgt;keySet()
-将全面的vlaue变为Collection接口实例:Collectionlt;Vgt;values()
-Map.Entry:保留key与value
-publicObjectgetKey()
-publicObjectgetValue()
-寄存key的注意点:
################Michael分割线####################
2、代码解说
(11)HashMap
Map的基本使用
假如查到了,则回来详细的值,假如没有查到,则回来null
使用Iterator输出Map接口中的内容
HashSet:是无序列寄存滴~~~
将全面的key进行输出
输出全面的值
(12)Hashtable
(13)TreeMap
按key进行排序,本操作中仍然使用了Comparable接口。
TreeSet:有序寄存,是通过Comparable接口终结的
(14)Stack
入栈:publicObjectpush(Objectobj)
出栈:publicObjectpop()
注意点:假如此刻栈中现已没有内容了,则会出现异常。
(15)Properties(要点)
·属性操作类,一切的key与value都按属性设置,都是字符串。并且此属性文件可以直接保留在文件中,或从文件中读取出来。
设置属性:publicObjectsetProperties(Stringkey,Stringvalue)
获得属性:
-publicStringgetProperty(Stringkey):依据key读取属性,回来值
-publicStringgetProperty(Stringkey,Stringdef):假如没有读到值,则回来def指定的内容。
保留在文件中:
-保留成一般文件:publicvoidstore(OutputStreamout,Stringcomments)throwsIOException
-保留成XMl文件:publicvoidstoreToXML(OutputStreamout,Stringcomments)throwsIOException
从文件中读取属性:
-从一般文件中读取:publicvoidload(InputStreaminput)throwsIOException
-从XML文件中读取:publicvoidloadFromXML(InputStreaminput)throwsIOException
将内容保留成一般文件:
将内容保留为XML文件:
(JAVA in the following order of priority
· Goal-oriented
·Class set structure
·JDBC
·File programming
Program this season
First explain the following:
·Collection
·List
·Set
·Map
·Iterator
·ListIterator
·Enumeration
·for...each sentence (new feature of JDK1.5)
Comparable interface
·Binary tree
1. Class set structure
(1) The purpose of generating the set
The class set is simply a dynamic target array, the target array can be changed in size, and the addition, deletion and output of the target can be arbitrarily terminated. All classes are stored in the java.util package.
(2) Differences in class sets
1. Consignment single value: Collection
2. Register a pair of values: Map
3. Output: Iterator
(3) Detailed concept of class set
lt;1gt;Collection (only one value can be registered)
- Primary function: for output use
- Sub-interface <imgsrc="sweat.gif"smilieid="10"border="0"alt=""/>ist (repeated elements are allowed, and the order of participation is the order of output)
-Subclass: ArrayList, which was launched after Java2, is a new class, uses asynchronous management, and has higher functions
-Subclass: Vector, which was launched in JDK1.0. Because it is an old class, it has many functions that List does not have. It uses the synchronization management method, and its thread safety is high and it will be slower. In addition to using the Iterator output, you can also use the Enumeration for output. Both are generic. The Vector subclass allows direct use of addElement(Objectobj), and can also add elements to the collection.
-Added methods by yourself:
- Take each object: publicObjectget(intind);
- Subclass: Stack (stack)
- Sub-interface: Set (repeated elements are not allowed, repeated elements will be replaced)? How can we distinguish repeated elements?
- Subclass: HashSet: is an unordered register
- Subclass: TreeSet: Ordered storage, which is terminated through the Comparable interface
-Common method:
- add an object: publicbooleanadd(Objectobj)
- Get the length of the class set: publicintsize();
- Determine whether the content in the set is empty: publicbooleanisEmpty()
-Deleting an object: publicbooleanremove(Objectobj): must involve the comparison of the object.
- Instantiate the Iterator target: publicIteratoriterator();
lt;2gt;JDK1.5 provides a convenient output operation: for...each sentence.
lt;3gt;Iterator interface output is the most common
-Common methods
-publicbooleanhasNext(): Determine if there is a next element
-publicObjectnext(): take out the next element
-The sub-interface <imgsrc="sweat.gif"smilieid="10"border="0"alt=""/>istIterator, which can perform bidirectional output, is only valid for the List interface
-Common methods: with comprehensive content in the Iterator interface
-publicbooleanhasPrevious()
-publicObjectprevious()
lt;4gt;Enumeration (also participate after 1.5, generic support ha~)
-Common method:
- Determine if there is a next element: publicbooleanhasMoreElements()
- Take out elements: publicObjectelement()
5. Map (register a pair of values)
-Function: Unlike the function of Collection, the primary function of Map is for searching.
-be careful:
- When using Map operations, iterator cannot be used directly for output.
-And there cannot be duplicate keys in it.
-If you have to use Iterator for output, you must do the following process:
-Map--gt;Set--gt;Iterator--gt;Map.Entry--gt;The difference between key and value
-Common subclasses:
-HashMap: unordered storage, is a new class, launched in JDK1.2, asynchronous management, high function
-Hashtable: It is an old class, launched when JDK1.0, it is thread-safe and has lower functions
-Properties (points): Properties file operation class
-TreeMap: ordered storage
-Common method:
-Retain data in the call set: publicObjectput(Objectkey, Objectvalue): The value returned is
-Search data from the collection: publicObjectget(Objectkey): Search by key.
- Turn Map data into Set instances: Setlt;Map.Entrylt;K,Vgt;gt;entrySet()
- Turn a comprehensive key into a set instance: Setlt;Kgt;keySet()
- Turn a comprehensive vlaue into an instance of the Collection interface: Collectionlt;Vgt;values()
-Map.Entry: keep key and value
-publicObjectgetKey()
-publicObjectgetValue()
-Notes on storing keys:
################Michael dividing line####################
2. Code Explanation
(11) HashMap
Basic use of Map
If it is found, it will return the detailed value, if it is not found, it will return null
Use Iterator to output the contents of the Map interface
HashSet: It is a non-sequential storage drop~~~
output the full key
output full value
(12) Hashtable
(13)TreeMap
Sorting by key, the Comparable interface is still used in this operation.
TreeSet: Ordered storage, terminated through the Comparable interface
(14) Stack
Push: publicObjectpush(Objectobj)
Pop out: publicObjectpop()
Note: If there is no content in the stack at the moment, an exception will occur.
(15) Properties (points)
·Attribute operation class, all keys and values ??are set according to attributes, which are all strings. And this property file can be directly kept in the file, or read out from the file.
Set properties: publicObjectsetProperties(Stringkey, Stringvalue)
Get properties:
-publicStringgetProperty(Stringkey): Read the property according to the key and return the value
-publicStringgetProperty(Stringkey, Stringdef): If no value is read, return the content specified by def.
Keep in the file:
- Keep as general file: publicvoidstore(OutputStreamout, Stringcomments) throwsIOException
- Preserve as XML file: publicvoidstoreToXML(OutputStreamout, Stringcomments) throwsIOException
Read properties from file:
- Reading from general file: publicvoidload(InputStreaminput) throwsIOException
- Reading from XML file: publicvoidloadFromXML(InputStreaminput) throwsIOException
Keep the content as a regular file:
Keep the content as an XML file:)
页:
[1]