java基础(集合概述)

一、集合概述
1.Java集合可分为Set List和Map三种体系
--Set:无序、不可重复的集合
--List:有序、可重复的集合
--Map:具有映射关系的集合

二、.Collection 的方法
1.collection方法分类
①、添加元素的
②、获取元素的 & 查找指定的元素
③、移除元素
④、工具方法
2.添加方法 add() addAll();
  // add(Object obj):添加一个元素到集合中
public void testAdd(){
  Collection collection = new ArrayList();
  System.out.println(collection.size());

  collection.add("ABC");
  collection.add(new Person("Tom",12));
  collection.add(new Person("Jerry",13));
  collection.add(new Person("Mike",14));
  System.out.println(collection.size());
}
  // addAll(Collection coll):添加一组元素到集合中
@Test
public void testAddAll(){
  Collection collection2 = new ArrayList();
  collection2.add("ABGCD");

  Collection collection = new ArrayList();
  collection.add("ABC");
  collection.add(new Person("Tom",12));
  collection.add(new Person("Jerry",13));
  collection.add(new Person("Mike",14));

  collection2.addAll(collection);
  System.out.println(collection2.size());
}
3.获取方法 遍历
在Collection中无法获取指定的元素,但可以遍历所有的元素
①.使用增强的for循环
public void testIterator(){
  Collection collection = new ArrayList();
  collection.add("ABC");
  collection.add(new Person("Tom",12));
  collection.add(new Person("Jerry",13));
  collection.add(new Person("Mike",14));

  for(Object obj:collection){
    System.out.println(obj);
  }
}
②.使用Iterator 迭代器
2.1 获取迭代器对象:调用Collection 的iterator()方法,获取Iterator接口对象
2.2调用Iterator 接口的方法进行迭代
public void testIterator(){
  Collection collection = new ArrayList();
  collection.add("ABC");
  collection.add(new Person("Tom",12));
  collection.add(new Person("Jerry",13));
  collection.add(new Person("Mike",14));

  Iterator it = collection.iterator();
  while(it.hasNext()){
    Object obj = it.next();
    System.out.println(obj);
  }
  // 若下一条记录无效,还调用next()方法,则抛出NoSuchElementException异常
  // Object obj = it.next();
}
4. 移除
①.clear():清空集合
②.remove(obj):移除指定的元素.通过equals()方法在集合中查找指定的元素,若存在,则移除
③.removeAll(Collection coll) 移除所有coll内有元素
④.retainAll(Collection coll) 保存coll中有的元素
5.工具类
public void testToolMethod(){
  Collection collection = new ArrayList();
  collection.add("ABC");

  Person p = new Person("Tom",12);
  collection.add(p);
  collection.add(new Person("Jerry",13));
  collection.add(new Person("Mike",14));

  ①.contains(Object o):利用equals()方法比较,查看集合中有没有指定的元素
  boolean flag = collection.contains(new Person("Jerry",13));
  System.out.println(flag);
  ②.containsAll(Collection<?> c):查看集合中有没有指定的集合
  Collection collection2 = new ArrayList();
  collection2.add("ABGCD");
  collection.add(new Person("Mike",14));
  System.out.println(collection.containsAll(collection2));
  ③.isEmpty():检验集合是否为空;
  System.out.println(collection.isEmpty());
  ④.toArray():把集合元素转为Object对象的数组
  Object [] objs = collection.toArray();
  System.out.println(objs.length);
}

三、Set集合
<1>Set集合不允许包含相同的元素;Set判断两个对象是否相同使用equals方法;
<2>关于HashSet
①.HashSet是Set的最典型实现
②.HashSet中不能有重复的元素,判定两个元素相等的标准:equals()方法返回true;
③.HashSet 根据hashCode()值来存放元素,所以不能保证元素的顺序
④.如果两个对象通过equals()方法返回true,这两个对象的hashCode值也应该相同。
⑤.HashSet是线程不安全的。
<3>.LinkedHashSet集合
①.LinkedHashSet 是HashSet的子类;
②.LinkedHashSet 集合根据元素的 hashCode 值来决定元素的存储位置,但它同时使用链表维护元素的次序,这使得元素看起来是以插入顺序保存的。
③.LinkedHashSet 不允许集合元素重复。
<4>TreeSet可以确保集合元素处于排序状态
①.TreeSet 支持两种排序方法:自然排序和定制排序。默认情况下,TreeSet 采用自然排序。
②.默认情况下TreeSet 要求集合中的元素必须实现Comparable接口
comparable 接口中只有一个方法:
public int compareTo(T o):若返回0代表两个元素相等,若返回正数。则当前元素大
若返回负数,则当前元素小;
@Override
public int compareTo(Object o) {
  if(o instanceof Person){
    Person person = (Person) o;
    return this.name.compareTo(person.name);
    return this.age + person.age;
  }else{
    throw new ClassCastException("不能转为Person类型");
  }
}

TreeSet 会调用每个元素的compareTo()方法去和集合中每一个已经有的元素去比较,进而
决定当前元素在集合中的位置
③.定制排序需要在创建 TreeSet 集合对象时,提供一个 Comparator 接口的实现类对象。由该 Comparator 对象负责集合元素的排序逻辑
Comparator comparator = new Comparator() {
@Override
public int compare(Object o1, Object o2) {
  if(o1 instanceof Person1 && o2 instanceof Person1){
    Person1 p1 = (Person1) o1;
    Person1 p2 = (Person1) o2;

    return p1.getAge() - p2.getAge();
  }
    throw new ClassCastException("不能转为Person1类型");
  }
};

四、List集合
<1>①.List 代表一个元素有序、且可重复的集合,集合中的每个元素都有其对应的顺序索引
②.List 允许使用重复元素,可以通过索引来访问指定位置的集合元素。
③.List 默认按元素的添加顺序设置元素的索引。
④.List 集合里添加了一些根据索引来操作集合元素的方法
<2>
void add(int index, Object ele):把元素添加到指定的位置 ,原来的元素被后移;
boolean addAll(int index, Collection eles):把一组元素添加到指定的位置

Object get(int index) :获取指定索引的元素
int indexOf(Object obj):获取指定元素的索引,若元素不存在则返回-1;
如:int index = list.indexOf(new Person("cc",10));
System.out.println(index);

int lastIndexOf(Object obj):List中可以存放重复的元素,获取重复元素的最后一个索引;
Object remove(int index):移除指定索引的元素

Object set(int index, Object ele)
List subList(int fromIndex, int toIndex)

五、Map集合
<1> Map 用于保存具有映射关系的数据,因此 Map 集合里保存着两组值,一组值用于保存 Map 里的 Key,另外一组用于保存 Map 里的 Value
<2> Map 中的 Key 不允许重复,即同一个 Map 对象的任何两个 Key 通过 equals 方法比较中返回 false
<3>Key 和 Value 之间存在单向一对一关系,即通过指定的 Key 总能找到唯一的,确定的 Value。
<4>
Map map = new HashMap();

//1.V put(K key,V value):放入一组键值对
map.put("AA", new Person("AA",12));
map.put("BB", new Person("BB",16));
map.put("CC", new Person("CC",13));
map.put("DD", new Person("DD",18));
map.put("EE", new Person("EE",19));

//2.void clear():清空Map
// map.clear();

//3.boolean containsKey(Object key):Map中是否包含指定的Key,若某个key的equals方法
// 和Map中已有的某个Key比较返回true,则包含。
System.out.println(map.containsKey("DD"));

//4.boolean containsValue(Object value):Map中是否包含指定的Value;
// System.out.println(map.containsValue(new Person("DD",18)));

//5.Set<Map.Entry<K,V>> entrySet():得到键值对对应的Entry的Set,需借助于泛型

//6.V get(Object key):根据key 返回对应的value
Object obj = map.get("CC");
System.out.println(obj);

//7.boolean isEmpty():检验Map是否为空
System.out.println(map.isEmpty());

//8.Set<K> keySet():返回key 对应的集合:Set类型
Set keySet = map.keySet();
System.out.println(keySet);

//9.void putAll(Map<? extends K,? extents V>m):放入一组键值对
Map map2 = new HashMap();
map2.put("ONE", "111");
map2.put("TWO", "222");
map2.put("THREE", "333");
map.putAll(map2);
//10. V remove(Object key):移除指定键对应的键值对
// map.remove("CC");

//11.int size():返回Map容量的大小
System.out.println(map.size()); //5

//12.Collection<V> values():返回Value对应的集合
Collection values = map.values();
System.out.println(values);

//13.对Map遍历
//13.1遍历键的集合:keySet()
//13.2遍历值的集合:values()
//13.3得到键值对的集合

Iterator it = keySet.iterator();
while(it.hasNext()){
Object key = it.next();
Object val = map.get(key);
System.out.println(key + ":" + val);
}
<5>HashSet由HashMap来定义:
1).在HashSet中维护了一个HashMap属性
2).在调用HashSet的方法,实际操作的是HashpMap对应的方法
3).add(Object obj)实际上是把obj放入了map中:
public boolean add(E e){
  return map.put(e,PRESENT) == null;
}
<6>Properties
①.Properties 类是 Hashtable 的子类,该对象用于处理属性文件
②.由于属性文件里的 key、value 都是字符串类型,所以 properties 里的 Key 和 Value 都是字符串类型的
③.Properties 对应.properties属性文件;
.properties中存放的是键值对,键值对都是String类型的

@Test
public void testProperties() throws IOException{
  //读取jdbc.properties

  //1.创建Properties对象
  Properties properties = new Properties();

  //2.调用Properties的load()方法加载属性文件对应的
  InputStream inStream = PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
  properties.load(inStream);

  //3.调用getProperties(String key)方法获取属性值
  String password = properties.getProperty("password");
  System.out.println(password);
}

posted @ 2018-01-19 14:33  ~零度~  阅读(130)  评论(0编辑  收藏  举报