关于Java基础的文章,我觉得写得还可以,以前发在了我其它的博客了,肯定是原创,现在再分享给大家出来。
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
对象用于封装特有数据,对象多了需要存储,如果对象的个数不确定。就使用集合容器进行存储。
java集合类主要负责保存、盛装其他数据,因此集合类也称容器类。java集合类分为:set、list、map三大体系。
其中set代表无序、不可重复的集合;
------list代表有序、可重复的集合。
-----map代表具有映射关系的集合。
java中集合特点:
1,用于存储对象的容器。
2,集合的长度是可变的。
3,集合中不可以存储基本数据类型值。
集合框架由来:
集合容器因为内部的数据结构不同,有多种具体容器。
不断的向上抽取,就形成了集合框架。
这部分的知识还是比较多的,如果以前没接触过看文字叙述肯定把你看懵了,先看一下总体的框架图
java集合类主要由两个接口派生:Collection和Map,是集合框架的根接口。我们首先来介绍一下Collection接口
1、概述
Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素。一些 Collection允许相同的元素而另一些不行。一些能排序而另一些不行。
由Collection接口派生的两个接口是List和Set。
2、迭代
关于Collection的常见操作就不说了,大家查一下API文档就会很清楚,这里重点说一下迭代。
如何遍历Collection中的每一个元素?根据上面的集合框架图我们可以看出,不论Collection的实际类型如何,它都支持一个iterator()的方法,该方
法返回一个迭代子,使用该迭代子即可逐一访问Collection中每一个元素。
举例:
<span style="font-size:14px;"> <pre name="code" class="java"> Collection<String> c = new ArrayList<String>();//定义一个集合c
Collection<String> c1 = new ArrayList<String>();//定义一个集合c1
c.add("jin");//添加元素
c.add("fulin");
c1.add("uuu");
c1.add("fff");
c.addAll(c1);//将c1添加到c中
Iterator<String> it = c.iterator();//定义一个迭代器it
while(it.hasNext())//是否读取到下一个元素?为真则打印
System.out.println(it.next());</span>1、概述
List是有序的Collection,使用此接口能够精确的控制每个元素插入的位置。用户能够使用索引(元素在List中的位置,类似于数组下标)来访问List中的元素,这类似于Java的数组。
它包括三个子类ArrayList、LinkList、Vector,关于它们的关系,如下图
2、ListIterator接口
在迭代时,不可以通过集合对象的方法操作集合中的元素。
如果想要添加、修改操作,就需要使用其子接口:ListIterrator。
注意:只有list集合具备该迭代功能.
举例:
<span style="font-size:14px;"> List<String> list = new ArrayList<String>();
list.add("jin");
list.add("fulin");
ListIterator<String> it = list.listIterator();
System.out.println(list);
while(it.hasNext())
{
Object obj = it.next();
if (obj == "jin") {
it.set("bbb"); //注意是it的修改和添加,而不是list
it.add("aaa");
}
}
System.out.println(list);
}</span>1、概述
Set是一种不包含重复的元素的Collection,它包括两种类,一个是HashSet、一个是TreeSet、对应着数据结构中的哈希表哈希表和数结构。
2、HashSet
HashSet: 内部数据结构是哈希表 ,是不同步的。
A、如何保证该集合的元素唯一性呢?
是通过对象的hashCode和equals方法来完成对象唯一性的。‘
B、哈希表如何判断元素相同?
1、判断hashCode是否相同?
2、若为ture,判断equals是否相同?
3、若还为ture,视为相同元素不存。
所以:如果元素要存储到HashSet集合中,必须覆盖hashCode方法和equals方法。
<span style="font-size:14px;"><pre name="code" class="java">package cn.jinfulin.P.bean;
/**
* @author 金福林
*
*/
public class Person implements Comparable<Person>{//继承Comparable接口
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person(){}
public int hashCode() {<span style="white-space:pre"> </span>//复写hashCode编码,这里用的是自动生成的,可以自定义。
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public boolean equals(Object obj) {//复写equals方法,如果hashCode相同才判断
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}</span>调用:
<span style="font-size:14px;"> private static void myHashCodeDemo() {
HashSet<Person> hs = new HashSet<Person>();
hs.add(new Person("jin",12));
hs.add(new Person("fu",22));
hs.add(new Person("fu",12));
hs.add(new Person("lin",15));
/* for (Person p : hs) {//for循环的另一种形式,和下面的迭代器循环表达一个意思。
System.out.println(p.getName()+":" + p.getAge());
}*/
Iterator<Person> it = hs.iterator();
while(it.hasNext())
{
Person p = it.next();
System.out.println(p.getName()+":" + p.getAge());
}
}</span>
TreeSet:可以对Set集合中的元素进行排序。是不同步的。
判断元素唯一性的方式:
就是根据compareTo()或compare()方法的返回结果是否是0,是0,就是相同元素,不存。
TreeSet对元素进行排序的方式一:
1、让元素自身具备比较功能,就需要实现Comparable接口。覆盖compareTo方法。
2、让集合自身具备比较功能,定义一个类实现Comparator接口,覆盖compare方法。将该类对象作为参数传递给TreeSet集合的构造函数。
<span style="font-size:14px;"><pre name="code" class="java">//方法一
public class Person implements Comparable<Person>{//实现Cmopareable接口,定义泛型为Person
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
/**构造函数
* @param name
* @param age
*/
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person(){
}
public int compareTo(Person p) {//覆写compareTo方法,因为添加泛型,所有传参可以为Person类型
// Person p = (Person) o;
int temp = this.age - p.age;
return temp == 0?this.name.compareTo(p.name):temp;
}</span><span style="font-size:14px;"> private static void myTreeSetDemo() {
TreeSet<Person> ts = new TreeSet<Person>();
ts.add(new Person("jin",12));
ts.add(new Person("fu",22));
ts.add(new Person("fu",22));
ts.add(new Person("lin",15));
for (Person p : ts) {
System.out.println(p.getName()+":" + p.getAge());
}
}</span><span style="font-size:14px;"><pre name="code" class="java">//方法二:
public int compare(Person o1, Person o2) {
int result = o1.getName().compareTo(o2.getName());
return result == 0 ? o1.getAge()-o2.getAge():result;
/*等同于上面的语句
if (result == 0) {
if(o1.getAge() - o2.getAge()==0)
{
return 0;
}
}
return result;
*/
}</span><span style="font-size:14px;"> private static void myTreeSetDemo2() {
TreeSet<Person> ts = new TreeSet<Person>(new CompareByName());
ts.add(new Person("jin",12));
ts.add(new Person("fu",22));
ts.add(new Person("fu",22));
ts.add(new Person("lin",15));
for (Person p : ts) {
System.out.println(p.getName()+":" + p.getAge());
}
}</span>1、概述
Map<K,V>集合是一个接口,和List集合及Set集合不同的是,它是双列集合,并且可以给对象加上名字,即(键)。
其实map集合中存储的就是键值对。
map集合中必须保证键的唯一性。
2、Map与Collection区别
Map:一次添加一对元素。Collection 一次添加一个元素。
Map也称为双列集合,Collection集合称为单列集合。
3、常用方法举例:
<pre name="code" class="java"><pre name="code" class="java"><span style="font-size:14px;">public static void main(String[] args) {
Map<Integer, String> m = new HashMap<Integer, String>();// 初始化
m.put(1, "jin");
m.put(2, "fu");
m.put(3, "lin");
m.put(4, "aaa");
// m.clear(); //清空Map
// Set<Entry<Integer, String>> sm = m.entrySet(); //读取map的一种方式
Set<Integer> sm = m.keySet();// 读取map的第二种方式
Iterator<Integer> it = sm.iterator();
while (it.hasNext()) {
Integer key = it.next();
String value = m.get(key);
System.out.println("键:" + key + "," + "值:" + value);
}
// System.out.println(m.get(1)); //get方法通过键获取值
}</span>
|--Hashtable :内部结构是哈希表,是同步的。不允许null作为键,null作为值。
|--Properties:用来存储键值对型的配置文件的信息,可以和IO技术相结合。
|--HashMap : 内部结构是哈希表,不是同步的。允许null作为键,null作为值。
|--TreeMap : 内部结构是二叉树,不是同步的。可以对Map集合中的键进行排序。
本章内容比较多,感谢能看到这里的朋友,要想学好,还需要大家对照图,多整理,多思考,勤复习。
原文地址:http://blog.csdn.net/jinfulin/article/details/44928391