[零基础学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、代码解说
(1)List实例
面向目标中清晰说了子类可以为其接口实例化
在List接口的使用之中,可以发现,里边是许可有重复元素的。
相同,假如如今的代码中使用了Collection接收也是可以的
之前的代码编译的时候会呈现一些正告:
注意<imgsrc="biggrin.gif"smilieid="3"border="0"alt=""/>emo02.java使用了未经检测或不安全的操作。
注意:要理解详细信息,请使用-Xlint:unchecked重新编译。
说是一个不安全的操作,需要进行限制。
在JDK1.5之前,类集中可以刺进恣意的数据,由于接收的类型全面是Object,但是在JDK1.5以后参加了泛型技术,所以在一切的接口定义的时候现已可以清晰的去声明究竟里边所加的是那个目标。
这样做的优点:假如直接使用了Object接收的话,则必定恣意的数据目标都可以向里边参加。
如今都用字符串接收数据哈,假如全面使用同一个类型接收,两种类型不匹配,则必定呈现类型转换异常哈~
这道程序可以参加instanceof进行判别,判别以后再进行转型。但是这样会添加代码的复杂度,那样十分烦恼哈~大家可以在运行处设置只能接收一种类型的目标哈~
这样可以确保目标数组中的每一个目标的类型统一。如今所说的只是开发中的规范,一个调集里放的都是同一种目标。
判别是不是为空:list.isEmpty()
删去一个目标:publicbooleanremove(Objectobj):就必须牵扯到目标的比较状况。
看下作用:
(2)Set实例
使用HashSet子类为Set接口实例化,调查其运行作用
看下作用:
Set接口不许可有重复元素,重复元素会更换而且HashSet是无序列寄存(3)for…each句子(理解)
for(类型目标称号:调集称号)--gt;JDK1.5新特性哈~
(4)Iterator
Iterator只能由前向后输出,假如要想进行双向输出,则可以使用其子接口——ListIterator
re
[零基础学JAVA]JavaSE使用部分-35.JAVA类集之一.doc
(JAVA in the following order of priority
· Goal-oriented
·Class set framework
·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 intention of generating the class set
In short, a class set is a dynamic target array, the size of this target array can be changed, and the addition, deletion and output of the final target can be arbitrarily. All classes are stored in the java.util package.
(2) Distinguishing of 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, and the repeated elements will be replaced)? How to distinguish the 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 the 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): returns value
-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
(1)List instance
Goal-oriented clearly states that subclasses can instantiate their interfaces
In the use of the List interface, it can be found that duplicate elements are allowed in it.
The same, if the current code uses Collection to receive it is also possible
The previous code compiles with some warnings:
Note that <imgsrc="biggrin.gif"smilieid="3"border="0"alt=""/>emo02.java uses unchecked or unsafe operations.
Note: To understand the details, recompile with -Xlint:unchecked.
Saying is an unsafe operation and needs to be restricted.
Before JDK1.5, arbitrary data can be inserted into the class set. Since the received type is Object, but after JDK1.5, the generic technology has been added, so it can be clearly declared when all the interfaces are defined. After all, what is added inside is the target.
The advantage of doing this: If you use Object to receive directly, then any data object can be added to it.
Nowadays, data is received by strings. If the same type is used to receive data and the two types do not match, there must be a type conversion exception~
This program can participate in instanceof to discriminate, and then perform transformation after discrimination. But this will increase the complexity of the code, which is very annoying~ You can set only one type of target at the runtime~
This ensures that each target in the target array is of the same type. What we're talking about these days is just a specification in development, and a set is all about the same goal.
Determine if it is empty: list.isEmpty()
Deleting an object: publicbooleanremove(Objectobj): must involve the comparison of the object.
Take a look at the effect:
(2) Set instance
Use a HashSet subclass to instantiate the Set interface and investigate its operation
Take a look at the effect:
The Set interface does not allow duplicate elements, the duplicate elements will be replaced and the HashSet is an unordered register (3) for...each sentence (understanding)
for (type target name: collection name) --gt; JDK1.5 new features ha~
(4)Iterator
Iterator can only output from front to back. If you want to output in both directions, you can use its sub-interface - ListIterator
re
JavaSE Use Part-35.One of JAVA Class Sets.doc)
页:
[1]