一、Set集合
java.util.Set集合是Collection集合的子集合,与List集合平级。该集合中元素没有先后放入次序,并且不允许重复。该集合的主要实现类是:HashSet类和TreeSet类。
HashSet类的底层是采用哈希表进行数据管理的。
TreeSet类的底层是采用二叉树进行数据管理的。
常用方法与Collection集合的相似:
boolean add(E e); //向集合中添加元素
boolean contains(Object o); //判断是否包含指定对象
boolean remove(Object o); //从集合中删除对象
void clear(); //清空集合
int size(); //返回包含对象的个数
boolean isEmpty(); //判断是否为空
Object[] toArray(); //将集合转换成数组
Iterator<E> iterator(); //遍历集合中的元素
----boolean hasNext(); //如果仍有元素可以迭代,则返回true
----E next(); //返回迭代的下一个元素
代码测试
import org.junit.Test;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
public class SetTest {
@Test
public void setTest() {
Set<String> set = new HashSet<>();
boolean b = set.add("two");
System.out.println(b); //true
System.out.println(set); //[two]
b = set.add("one");
System.out.println(b); //true
System.out.println(set); //[one, two]
b = set.add("three");
System.out.println(b);
System.out.println(set); //[one, two, three]
b = set.add("four");
System.out.println(b);
System.out.println(set); //[four, one, two, three]
b = set.add("five");
System.out.println(b);
System.out.println(set); //[four, one, two, three, five]
System.out.println("----------------------");
b = set.add("one");
System.out.println(b); //false
System.out.println(set);
}
//随机生成10个1-20之间的随机数放入Set集合并打印
@Test
public void randomSetTest() {
Random random = new Random();
Set<Integer> set = new HashSet<>();
// for (int i = 0; i < 10; i++) {
// int r = random.nextInt(20) + 1;
// boolean b = set.add(r);
// if (b==false) {
// i--;
// }
// }
while (set.size() < 10) {
set.add(random.nextInt(20) + 1);
}
System.out.println(set);
Iterator iter = set.iterator();
while (iter.hasNext()) {
System.out.print(iter.next() + " ");
}
}
}
二、Map集合
java.util.Map<K, V>集合中存取元素的基本单位是:单对元素,其中类型参数如下:
K - 此映射所维护的键(key)的类型,相当于目录。
V - 映射值(Value)的类型,相当于内容。
该集合中key是不允许重复的,而且一个key只能对应一个value,该集合的主要实现类有:HashMap类和TreeMap类。
常用方法:
V put(K key, V value); //将Key-Value对存入Map,若集合中已经包含该Key,则替换该Key所对应的Value,返回值为该Key原来所对应的Value,若没有则返回null
V get(Object key);
boolean containsKey(Object key);
boolean containsValue(Object value);
V remove(Object key);
Map集合的遍历方式:
代码测试
import org.junit.Test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapTest {
@Test
public void hashMapTest() {
Map<Integer, String> map = new HashMap<>();
//向集合中添加元素
map.put(1, "郝俊芳");
map.put(2, "容嬷嬷");
map.put(3, "玛丽");
map.put(4, "缘如水");
System.out.println(map); //{1=郝俊芳, 2=容嬷嬷, 3=玛丽, 4=缘如水}
String value = map.get(3);
System.out.println("3 = " + value);
boolean b = map.containsKey(5);
System.out.println(b); //false
b = map.containsKey(2);
System.out.println(b); //true
b = map.containsValue("哈哈");
System.out.println(b); //false
b = map.containsValue("郝俊芳");
System.out.println(b); //true
//删除map集合中的元素,根据key,返回旧值
String v = map.remove(3);
System.out.println("删除的值为:" + v); //玛丽
v = map.remove(11);
System.out.println("删除的值为:" + v); //null
}
@Test
public void iteratorMapTest() {
Map<Integer, String> map = new HashMap<>();
//向集合中添加元素
map.put(1, "郝俊芳");
map.put(2, "容嬷嬷");
map.put(3, "玛丽");
map.put(4, "缘如水");
//Map集合遍历,方式一
System.out.println(map);
System.out.println("-----------------");
//使用keySet方法遍历Map集合中的元素,方式二
//使用for-each循环遍历集合元素,可以迭代访问Collection和数组
Set<Integer> set = map.keySet();
for (Integer it : set) {
System.out.println(it + "=" + map.get(it));
}
System.out.println("------------------");
//使用entrySet遍历Map集合中的元素,方式三
//entrySet()返回Map.Entry<Integer, String>类型
Set<Map.Entry<Integer, String>> entry = map.entrySet();
for (Map.Entry<Integer, String> e : entry) {
System.out.println(e);
}
System.out.println("-------------------");
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry1 = (Map.Entry)iter.next();
System.out.println(entry1.getKey() + " = " + entry1.getValue());
}
}
}
三、 异常机制
异常就是“不正常”的含义,但Java语言中体现为运行阶段发生的错误。java.lang.Throwable类是Java语言中所以错误(Error类)和异常(Exception类)的超类。
Error类主要用于描述比较严重无法编码解决的错误,如:JVM挂了等。
Exception类主要用于描述比较轻微可以编码解决的错误,如:0作为除数等。
基本分类:
java.lang.Exception类的所有子类主要分为两大类:
RuntimeException类:运行时异常,也叫做非检测型异常。
IOException和其它异常:其它异常,也叫做检测型异常(指在编译阶段能够被编译器检测出来的异常)。
注意:当程序执行过程中发生了异常但没有手动处理时,该异常由Java虚拟机采用默认方式处理,而默认处理方式就是打印异常的名称、异常的原因以及异常发生的位置并终止程序。
自定义异常
实现流程
a.自定义xxxException继承自Exception或者其子类。
b.提供两个版本的构造方法,一个无参构造方法,另一个是字符串作为参数的构造方法。
代码测试
public class AgeException extends Exception {
public AgeException() {}
public AgeException(String msg) {
super(msg);
}
}
public class Person {
private String name;
private int age;
public Person() {}
public Person(String name, int age) throws AgeException {
setName(name);
setAge(age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) throws AgeException {
if (age > 0 && age < 150) {
this.age = age;
} else {
// this.age = 0;
// System.out.println("年龄不合理!");
throw new AgeException("年龄不合理!");
}
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class PersonTest {
public static void main(String[] args) {
Person person = null;
try {
person = new Person("JJ", -3);
} catch (AgeException e) {
e.printStackTrace();
}
System.out.println(person);
}
}
四、重点
下面着重记忆,注意两者区别:
public class ExceptionTest02 {
public static void main(String[] args) {
Person person = haha();
System.out.println(person.age); //20
}
public static Person haha() {
Person person = new Person();
try {
person.age = 10;
return person; //复制出来的是地址,栈内
} catch (Exception e) {
return null;
} finally {
person.age = 20;
}
}
//静态内部类
static class Person {
int age;
}
}
public class ExceptionTest03 {
public static void main(String[] args) {
int a = haha();
System.out.println(a); //10
}
public static int haha() {
int a = 10;
try {
return a; //复制出来的是值,返回10
} catch (Exception e) {
return 0;
} finally {
a = 20; //栈内a被改为20
}
}
}