复习java基础第四天(集合:List、Map、Collections、Enumeration)

时间:2022-09-18 02:15:34

一、List:

List 代表一个元素有序、且可重复的集合,集合中的每个元素都有其对应的顺序索引

List 允许使用重复元素,可以通过索引来访问指定位置的集合元素。

List 默认按元素的添加顺序设置元素的索引。

List 集合里添加了一些根据索引来操作集合元素的方法:

复习java基础第四天(集合:List、Map、Collections、Enumeration)

另外:

List 额外提供了一个 listIterator() 方法,该方法返回一个 ListIterator 对象,

ListIterator 接口继承了 Iterator 接口,提供了专门操作 List 的方法:

boolean hasPrevious()
Object previous()
void add()

ArrayList 和 Vector 是 List 接口的两个典型实现 区别:

1、Vector 是一个古老的集合,通常建议使用 ArrayList。

2、ArrayList 是线程不安全的,而 Vector 是线程安全的。

3、即使为保证 List 集合线程安全,也不推荐使用 Vector。

 public class 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;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person(){}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}

练习代码1:Person类

 import java.util.ArrayList;
import java.util.Comparator;
import java.util.List; public class TestArrayList {
public static void main(String []args){ List list = new ArrayList();
List list2 = new ArrayList();
Person p1 = new Person("lisi007",15);
list.add(new Person("lisi003",12));
list.add(new Person("lisi003",12));
list.add(p1);
list.add(new Person("lisi005",10));
list.add(new Person("lisi006",20)); for(Object obj:list){
System.out.println(obj);
} System.out.println();
list2.add(new Person("zhangsan",22));
list2.add(new Person("zhangsan",23));
list.addAll(2, list2);
list.add(3, p1);
// list.remove(1); System.out.println(list.indexOf(p1));
System.out.println(list.lastIndexOf(p1)); for(Object obj:list){
System.out.println(obj);
} /* //遍历List方法1:调用iterator()方法
Iterator iterator = list.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
} //遍历List方法2:增强for循环
for(Object obj:list){
System.out.println(obj);
} //遍历List方法3:for循环,利用get(int index)
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
//遍历List方法4:
ListIterator li = list.listIterator();
while(li.hasNext()){
System.out.println(li.next());
}
*/
}
}

练习代码2:TestArrayList类(主程序)

二、Map:

——Map 用于保存具有映射关系的数据,因此 Map 集合里保存着两组值, 一组值用于保存 Map 里的 Key,

另外一组用于保存 Map 里的 Value。

——Map 中的 key 和 value 都可以是任何引用类型的数据。

——Map 中的 Key 不允许重复(值可以),即同一个 Map 对象的任何两个 Key 通过 equals 方法比较中

返回 false。

——Key 和 Value 之间存在单向一对一关系,即通过指定的 Key 总能找到唯一的,确定的 Value。

Map常用方法:

复习java基础第四天(集合:List、Map、Collections、Enumeration)

HashMap 和 Hashtable 是 Map 接口的两个典型实现类,它们的区别:

——Hashtable 是一个古老的 Map 实现类,不建议使用。

——Hashtable 是一个线程安全的 Map 实现,但 HashMap 是线程不安全的。

——Hashtable 不允许使用 null 作为 key 和 value,而 HashMap 可以。

注意:

1、与 HashSet 集合不能保证元素的顺序的顺序一样,Hashtable 、HashMap 也不能保证其中

key-value 对的顺序。

2、Hashtable 、HashMap 判断两个 Key 相等的标准是:两个 Key 通过 equals 方法返回 true,

hashCode 值也相等。

3、Hashtable 、HashMap 判断两个 Value相等的标准是:两个 Value 通过 equals 方法返回 true。

另外:

——LinkedHashMap 是 HashMap 的子类。

——LinkedHashMap 可以维护 Map 的迭代顺序:迭代顺序与 Key-Value 对的插入顺序一致。

Properties :

——Properties 类是 Hashtable 的子类,该对象用于处理属性文件。

——由于属性文件里的 key、value 都是字符串类型,所以 properties 里的 Key 和 Value 都是字符串类型的。

TreeMap:

1、TreeMap 存储 Key-Value 对时,需要根据 Key 对 key-value 对进行排序。

TreeMap 可以保证所有的 Key-Value 对处于有序状态。

2、TreeMap 的 Key 的排序:

——自然排序:TreeMap 的所有的 Key 必须实现 Comparable 接口,而且所有的 Key 应该是

同一个类的对象,否则将会抛出 ClasssCastException。

——定制排序:创建 TreeMap 时,传入一个 Comparator 对象,该对象负责对 TreeMap 中的

所有 key 进行排序。此时不需要 Map 的 Key 实现 Comparable 接口。

练习代码:

 url=jdbc:mysql:///test
driver=com.mysql.jdbc.Driver
user=root
password=123

jdbc.properties

 public class 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;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person(){}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}

Person类

 import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap; public class TestHashMap {
public static void main(String []args) throws FileNotFoundException, IOException{
Map map = new HashMap(); //添加Map元素:key不可以重复,value可以重复
map.put("AA", new Person("AAA",12));
map.put("DD", new Person("AA",12));
map.put("CC", new Person("CCC",13));
map.put("MM", new Person("MMM",15));
map.put("NN", new Person("AA",14)); //遍历Map中的元素:得到键的集合通过keySet()方法
Set keySet = map.keySet();
for(Object obj:keySet){
Object value = map.get(obj);
System.out.println("key= "+obj+" value= "+value);
} //直接得到值的集合
Collection values = map.values(); //这里得到的值是可以重复的
for(Object val:values){
System.out.println(val);
} //得到键值对的集合
/* Map<String,Object> map = new HashMap<String,Object>();//此方法需要泛型
for(Map.Entry<String, Object> kv:map.entrySet()){
String key = kv.getKey();
Object value = kv.getValue();
System.out.println(key + " : " +value);
}*/ //移除元素的
map.remove("DD"); //操作Map的工具方法:
System.out.println(map.size());
System.out.println(map.isEmpty());
System.out.println(map.containsKey("AA")); Comparator comparator = new Comparator(){
@Override
public int compare(Object o1, Object o2) {
int result;
Person p1 = (Person) o1;
Person p2 = (Person) o2;
result = p1.getAge() - p2.getAge();
if(result == 0){
return p1.getName().compareTo(p2.getName());
}
return result;
}
}; TreeMap tm = new TreeMap(comparator);
tm.put(new Person("ABC",12), "ABC");
tm.put(new Person("ABCDF",17), "ABCD");
tm.put(new Person("ABCDE",14), "ABCD");
tm.put(new Person("ABCD",14), "ABCD"); Set c = tm.keySet();
for(Object keys:c){
Object value = tm.get(keys);
System.out.println(keys+" : "+value);
} //properties文件在java中对应的是一个properties类的对象
//1、创建一个properties类的对象
Properties properties = new Properties(); //2、使用IO流加载对应的properties文件
properties.load(new FileInputStream("jdbc.properties")); //3、得到对应的属性值
String url = properties.getProperty("url");
System.out.println(url);
}
}

TestHashMap类(主程序)

三、操作集合的工具类:Collections:(注意不是Collection)

1、Collections 是一个操作 Set、List 和 Map 等集合的工具类

2、Collections 中提供了大量方法对集合元素进行排序、查询和修改等操作,还提供了对集合对象设置不可变、

对集合对象实现同步控制等方法。

排序操作:

—reverse(List):反转 List 中元素的顺序。

—shuffle(List):对 List 集合元素进行随机排序。

—sort(List):根据元素的自然顺序对指定 List 集合元素按升序排序。

—sort(List,Comparator):根据指定的 Comparator 产生的顺序对 List 集合元素进行排序。

—swap(List,int, int):将指定 list 集合中的 i 处元素和 j 处元素进行交换。

同步控制:

Collections 类中提供了多个 synchronizedXxx() 方法,该方法可使将指定集合包装成线程同步的集合,

从而可以解决多线程并发访问集合时的线程安全问题。

复习java基础第四天(集合:List、Map、Collections、Enumeration)

四、Enumeration:

Enumeration 接口是 Iterator 迭代器的 “古老版本”。

复习java基础第四天(集合:List、Map、Collections、Enumeration)

练习代码:

 public class 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;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person(){}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}

Person类

 import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List; public class TestCollections {
public static void main(String []args){ List list = new ArrayList();
list.add(new Person("lisi003",22));
list.add(new Person("lisi009",18));
list.add(new Person("lisi005",20));
list.add(new Person("lisi001",12)); //使用Collection中的方法对list中和元素进行排序
Collections.sort( list, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Person p1 = (Person) o1;
Person p2 = (Person) o2;
return p1.getAge() - p2.getAge();
}
}); for(Object obj:list){
System.out.println(obj);
} //获取线程安全的list对象
List list2 = Collections.synchronizedList(new ArrayList()); //对Enumeration进行遍历
Enumeration names = Collections.enumeration(list);
while(names.hasMoreElements()){
System.out.println(names.nextElement());
}
}
}

TestCollections类