存储对象可以考虑两种方式:①数组,②集合
数组存储对象的特点:Student[] stu = new Student[20]; stu[0] = new Student();......这样存储有一定的弊端
1、一旦创建,其长度不可变。
2、真实的数组存放的对象的个数是不可知的。
集合:
Collection接口
|--------------List接口:存储有序的,可以重复的元素
|----------ArrayList(List的主要实现类)
|-----------LinkedList(对集合频繁的插入和删除时使用)
|-----------Vector(线程安全的,比较古老的实现类,效率较低)
|---------------Set接口:存储无序的,不可重复的元素。set中常用的方法都是Collection定义的。
|-----------HashSet(Set的主要实现类):1.无序的≠随机的,真正的无序性是指元素在底存储的位置是无序的。
2.不可重复性:当向set中添加进相同的元素的时候,后面的这个不能添加进去
3.
说明:要求添加进set中的元素所在的类要重写equals()方法和hashCode()方法,进而保证元素的不可重复性。
set中元素是如何存储的?使用哈希算法
当向set中添加元素时,首先调用此对象所在类的hashCode()方法,然后计算此对象的hash值,此hash值决定了 此对象在set中的存储位置,若此位置之前没有对象存储,则此对象直接存到该位置,若此位置已有对象存储,再 通过equals方法比较这两个对象是否相同,若相同那么后一个对象就不能添加进来
要求:equals()方法要与hashCode()方法一致。
|-----------LinkedHashSet:使用链表维护了一个添加进集合中的顺序。导致当我们遍历LinkedHashSet集合元素时,是按照添加进去的顺序遍历的。
LinkedHashSet插入性能略低于HashSet,但在迭代访问set里的全部元素时有很好的性能。
|-----------TreeSet:1.向TreeSet中添加元素必须是同一个类的。
2.可以按照添加进集合中的元素的指定的顺序遍历。像String,包装类等默认按照从小到大的顺序遍历。
3. 当向TreeSet中添加自定义类对象时,有两种排序方法:①自然排序②定制排序
4.自然排序:要求自定义类实现java.lang.Comparable接口并重写其compareTo(Object obj)抽象方法,在此方法中指明按照自定 义类的哪个属性进行排序。
5.向TreeSet中添加元素时,首先按照compareTo()方法进行比较,一旦返回0,虽然仅是两个对象的此属性值相同,但是程序会认 为这两个对象是相同的,进而后一个对象就不能添加进来。
>compareTo()与hashCode()以及equals()三者保持一致。
6.定制排序:compare()与hashCode()以及equals()三者保持一致。
public class TestCollection {
@Test
public void test1() {
Collection c1 = new ArrayList();
// 1.size():返回集合中元素的个数
System.out.println(c1.size());
// 2.add(Object obj):向集合中添加一个元素
c1.add(123);
c1.add("AA");
c1.add(new Date());
c1.add("BB");
System.out.println(c1.size());
// 3.addAll(Collection coll): 将形参coll中包含的所有元素添加到当前集合中
Collection c2 = Arrays.asList(1, 2, 3);
c1.addAll(c2);
System.out.println(c1.size());
// 4.isEmpty():判断集合是否为空
System.out.println(c1.isEmpty());
// 查看集合元素
System.out.println(c1);
// 5.clear():清空集合中的元素
c1.clear();
System.out.println(c1.isEmpty());
}
@Test
public void test2() {
Collection c1 = new ArrayList();
c1.add(123);
c1.add(new String("AA"));
c1.add(new Date());
c1.add("BB");
c1.add(new Person("MM", 22));
System.out.println(c1);
// 6.contains(Object obj): 判断集合中是否包含指定的obj元素
// 判断依据:根据元素所在的类的equals()方法判断
// 明确:如果集合中存入的元素是自定义类型对象,那么自定义的类需要重写equals()方法。
boolean b1 = c1.contains(123);
System.out.println(b1);
b1 = c1.contains("AA");
System.out.println(b1);
boolean b2 = c1.contains(new Person("MM", 22));
System.out.println(b2);
// 7.containsAll(Collection coll):判断当前集合中是否包含coll中所有的元素。
Collection c2 = new ArrayList();
c2.add(123);
c2.add(new String("AA"));
c2.add(456);
boolean b3 = c1.containsAll(c2);
System.out.println(b3);
// 8.retainAll(Collection coll):求当前集合与coll的共有的元素,返回给当前集合。
c1.retainAll(c2);
System.out.println(c1);
// 9.remove(Object obj):从当前集合中删除obj元素,若删除成功返回true,否则返回false。
System.out.println(c1.remove("BB"));
// 10.removeAll(Collection coll):从当前集合中删除包含在coll中的元素,若删除成功返回true,否则返回false。
System.out.println(c1.removeAll(c2));
}
@Test
public void test3() {
Collection c1 = new ArrayList();
c1.add(123);
c1.add(new String("AA"));
c1.add(new Date());
c1.add(new String("BB"));
c1.add(new Person("GG", 22));
Collection c2 = new ArrayList();
c2.add(123);
c2.add(new Person("GG", 22));
System.out.println(c1.remove("AA"));
System.out.println(c1.removeAll(c2));
System.out.println(c1);
// 11.equals(Object obj):判断集合中所有元素是否完全相等。
// 12.hashCode(): 获得集合中元素的散列值。
System.out.println(c1.hashCode());
// 13.toArray():将集合转换为数组
System.out.println(c1);
Object[] obj = c1.toArray();
for (int i = 0; i < obj.length; i++) {
System.out.println(obj[i]);
}
System.out.println();
// 14.iterator():返回一个Iterator接口实现类对象,用来遍历集合。
Iterator it = c1.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
Map接口:存储“键-值”结构的数据
|-----------HashMap
|-----------LinkedHashMap
|-----------TreeMap
|-----------HashTable(子类:Properties)
HashMap:Map的主要实现类。
1、key是用Set来存放的,不可重复。value是用Collection来存放的,可重复。一个key-value对,是一个Entry。所有的Entry是用Set存放的,也是不可重复的。
2、向HashMap中添加元素时会调用key所在类的equals()方法判断两个key是否相同,若相同,则只能添加进后添加的元素。
如何遍历Map? Set keySet()、Collection values()、Set entrySet()
LinkedHashMap:
使用链表维护添加进Map中的顺序。故遍历时,是按照添加的顺序遍历的。与LinkedHashSet类似
TreeMap:
按照添加进Map中的元素的key的指定属性进行排序。要求:key必须是同一个类的对象!
Hashtable:古老的实现类,线程安全,不建议使用,其子类Propertirs常用来处理属性文件。键和值都为String类型的。
public class TestMap {
@Test
public void test1() {
Map map = new HashMap();
// 1.Object put(Object key, Object value):向map中添加一个元素(对象)
// 2.int size():返回集合中元素的个数。
map.put("AA", 123);
map.put("BB", 456);
map.put("BB", 45);
map.put(123, "CC");
map.put(null, null);
map.put(new Person("AA", 20), 66);
map.put(new Person("AA", 20), 67);
System.out.println(map.size());
System.out.println(map);
map.remove(null);
System.out.println(map);
// 3.Object get(Object key):返回指定key的value值,若无此key则返回null.
Object value = map.get(123);
System.out.println(value);
System.out.println(map.get(null));
System.out.println(map.get(1234));
}
@Test
public void test2() {
Map map = new HashMap();
map.put("AA", 123);
map.put("BB", 456);
map.put(123, "CC");
map.put(null, null);
map.put(new Person("AA", 123), 22);
//遍历Map的key值
Set keySet = map.keySet();
Iterator it = keySet.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("==============");
// 遍历Map的value
Collection coll = map.values();
Iterator it1 = coll.iterator();
while (it1.hasNext()) {
System.out.println(it1.next());
}
System.out.println("+++++++++++++");
// 遍历Map的键值对 entry
Set entrySet = map.entrySet();
for (Object obj : entrySet) {
Map.Entry entry = (Map.Entry) obj;
System.out.println(entry.getKey() + "------->" + entry.getValue());
// System.out.println(entry);
}
}
@Test
public void test3() {
Map map = new LinkedHashMap();
map.put("AA", 123);
map.put("BB", 456);
map.put(123, "CC");
map.put(null, null);
map.put(new Person("AA", 123), 22);
Set set = map.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
@Test
public void testTreeMap1() {
Map map = new TreeMap();
map.put("AA", 123);
map.put("BB", 456);
map.put(123, "CC");
map.put(null, null);
map.put(new Person("AA", 123), 22);
}
@Test
public void testProperties() {
try {
Properties props = new Properties();
props.load(new FileInputStream(new File("jdbc.properties")));
String user = props.getProperty("user");
String password = props.getProperty("password");
System.out.println("用户名:" + user + "\n" + "密码:" + password);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
}
}
加:操作集合的工具类:Collections