黑马程序员_java基础笔记第六天

 

.------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------.

 

一 集合:
 1 定义:
  1 集合是用来存储对象的容器。
  2 集合长度是可变的。
  3 可以储存多种类型对象。
  
 2 体系:
  1 单列集合,Collection,是所有单列集合的基类是个接口。
  2 双列集合: Map,是所有双列集合的基类,也是接口。
二 Collection:
 1 定义:
  1 用来存储单列的集合。
  2 他有两大子类List,Set。
 2 常用功能
   1 增添功能:
     1 boolean add(E e)
            将任意类型添加到集合内。

     2 boolean addAll(Collection<? extends E> c)
              添加一个Colection集合。

  2 删除功能:
    1 remove(int index)
     删除指定坐标上的元素。
    2 removeAll(Collection<?> c)
     删除于c集合内所有相同的元素。
    3 clear()删除集合内的所有元素。
    
  3 判断功能:
    1 contains(Object o)
     是否包含包含o对象。

    2 containAll((Collection<?> c)
     是否包含另一个集合中的所有元素。

    3 equals(Object o)
     比较两个对象是否相等。
     
    4 isEnpty()
     判断集合是否为空。
  4 获取
    1 size()
     获取集合的长度。
  5 转换:
    1 toArray()
     将集合转换为数组。
  6 特殊功能:
    1 iterator()
     返回迭代器,用于获取集合内的所有元素。
三 List集合:
 1 定义:

  1 List是Collection集合的子类。
  2 存储有序,元素可重复。
  3  有三个重要子类,ArrayList,LinkeList,Vector。
 2 特有功能:
  1 get(int index)
   获取指定角标上的元素。

  2 indexOf(Object o)
   获取元素第一次出现的位置。

  2 lastIndexOf(Object o)
   获取元素最后一次出现的角标。
  4 set(int index, E element)
   将制定坐标上的元素替换成任意对象。
   
  5 subList(int fromIndex, int toIndex)
   获取集合内的一部分包左不包又。
  
四 List集合的三个子类:

 1 ArrayList特点:
   1 可重复的。
   2 存取有序的。
   3 内部是数组结构的。
   4 查找快增删慢。
   5 不安全。

 2 LinkeList特点:
   1 可重复的。
   2 存取有序的
   3 是链表式结构的。
   4 查找慢增删快。
   5 不安全。
   6 LinkeList集合多了首尾操作的方法。


 3 Vector:特点:
   1 可重复的。
   2 存取有序的 
   3 是数组结构。
   4 查找慢增删慢。
   5 安全,因为有锁。


五 set集合重要子类
 1 hashSet特点:
  1 存取无需。
  2 元素不可重复。
  3 是由哈希值确保唯一性的。
  4 自定义对象要复写hashcode和equals方法确保为一性。

 2 TreeSet特点:
  1 存取无需。
  2 不可重复。
  3 是按字典循序存储的。
  4 自定义对象要实现Comparable接口或自定Comparator比较器接口,自定义比较规则。
  5 TreeSet特有方法:
   1 first()
    获取第一个元素。
   2 last()
    获取最后移个元素。
   3 floor(E e)
    按比较器返回比e最大的元素。
   4 lower(E e)
    按比较器返回比e
   5 pollFirst()
    获取并移除第一个元素。
   4 pollLast()
    获取并移除最后一个元素。
   7subSet(E fromElement, E fromElement,
    获取从fromElement到fromElement的元素


六 Colection 例题:

1将自定义对象存储到hashSet集合,遍历取出。
public class HashSet01 {

public static void main(String[] args) {
 HashSet hs=new HashSet();
 hs.add(new Person("张三",20));
 hs.add(new Person("李四",26));
 hs.add(new Person("王五",28));
 hs.add(new Person("赵六",30));
 Iterator it=hs.iterator();
 while(it.hasNext())
 {
 System.out.println(it.next());
 }
}
}

2用逆向迭代器将集合逆向取出。
public static void main(String[] args) {
 ArrayList a=new ArrayList();
 a.add("1");
 a.add("2");
 a.add("3");
 a.add("4");
 ListIterator it=a.listIterator();
  while(it.hasNext())
 {
  Object obj= it.next();
  System.out.println(it.nextIndex());
  System.out.print(obj);
 }
  while(it.hasPrevious())
  {
   Object obj= it.previous();
   System.out.print(obj);
  }
 System.out.println(a);

}
}

3自定义对象存储到LinkeList集合相同属性不存储。
public class Student {
 private String name;
 private int age;
 public Student(String name, int age) {
  super();
  this.name = name;
  this.age = age;
 }
 @Override
 public String toString() {
  return "Student [name=" + name + ", age=" + 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 boolean equals(Object o)
 {
  if(!(o instanceof Student))
   return false;
  Student s=(Student)o;
  System.out.println(this.name+" d  "+s.name);
  return s.name.equals(name) && s.age==age;
 }
}
 

public static void main(String[] args) {
 LinkedList li=new LinkedList();
 li.offerFirst(new Student("张三",4));
 li.offerFirst(new Student("李四",3));
 li.offerFirst(new Student("王五",1));
 li.offerFirst(new Student("赵六",1));
 li.offerFirst(new Student("王五",1));
 System.out.println(show(li));
 }
public static  LinkedList show(LinkedList l)
 {
  LinkedList newl=new LinkedList();
  Iterator in=l.iterator();
  while(in.hasNext())
  {
   Object o=in.next();
   if(!newl.contains(o))
    newl.add(o);
  }
  return newl;
 }
}

4将Integer类型按从大到小的循序存入集合。
public class Test {
public static void main(String[] args) {
 TreeSet<Integer> t=new TreeSet<Integer>(new Comparator<Integer>(){
  @Override
  public int compare(Integer o1, Integer o2) {
    int x=o2.compareTo(o1);
    return x;
  }

 });
 t.add(3);
 t.add(2);
 t.add(3);
 t.add(4);
 System.out.println(t);
}
}

七 Map集合:
 1 定义:
  1 Map集合是以健值对的形式存在的双列集合。
  2 需要你确保为一性,不可重复。主要是健不可以重复。
  3 Map集合添加的方法是Put(key,value)。

 2 取出方式:
  1 getKey()返回key的set集合。通过遍历set集合获取每个键,再通过Map的get方法获取值。
  2 enptySet()方法返回键值对映射关系的set集合。遍历set集合获取每个键值对,再通过getKey,getValue获    取键值。
 3 Map的重要子类。
  1 hashMap特点: 
   1 线程不安全,存取速度快,允许有空键值对出现,null,null。
   2 通过hashcode确保为一性。
  2 TreeMap
   1 是二叉数排序的。规则和TreeSet集合相同。‘
   2 不允许渴念值对出现。
   
  3 hashtable
   1 线程安全,速度慢,不允许空键值对。
  4 properties
   1 是Hashtable的子类线程安全的,
   2 用于配置文件。
   3 是只能存储String类型的集合。
   4 不用定义泛型。

八 Map集合存储自定义对象:

public class Student implements Comparable<Student>{
 private String name;
 private int age;

 public Student(String name, int age) {
  this.name=name;
  this.age=age;
 }
 @Override
 public String toString() {
  return "Student [name=" + name + ", age=" + 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 int compareTo(Student s) {
  int x=this.name.compareTo(s.name);
  x=(x==0)?new Integer(this.age).compareTo(new Integer(s.age)):x;
  return x;
 }
}

public static void main(String[] args) {
 TreeMap<Student,Integer> t=new TreeMap();
 t.put(new Student("张三",11), 1);
 t.put(new Student("李四",22), 2);
 t.put(new Student("王五",33), 3);
 t.put(new Student("赵六",22), 4);
 t.put(new Student("孙七",32),  5);
 Set<Map.Entry<Student,Integer>> s=t.entrySet();
   for(Map.Entry<Student,Integer> m2:s)
   {
    System.out.println(m2.getKey()+"   "+m2.getValue());
   }
}
}

      ------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值