常见数据结构Collection的特点及常用方法

一、集合以及图的结构:
  • Collection
    • Set
      • HashSet
      • LinkedHashSet
      • TreeSet
    • List
      • ArrayList
      • LinkedList
    • Queue
 
二、Collection
 
Collection特点:
1.存储的数据不重复;
2.可以存储对象
 
声明代码:
Collection collection=new ArrayList();//Collection不能实例化
 
常用方法:
 
 
三、Set
 
常用实现类:
HashSet、LinkedHashSet、TreeSet
 
Set特点:
1.不允许存在重复元素
2.可以存储对象元素
 
申明代码:
Set set=new HashSet();
Set set=new LinkedHashSet();
Set set=new TreeSet();
 
HashSet set=new HashSet();
LinkedHashSet set=new LinkedHashSet();
TreeSet set=new TreeSet();
 
Set常见方法:
 
Set遍历方法:
1.迭代器遍历
Iterator it = set.iterator();
while (it.hasNext()) {
  String str = it.next();
  System.out.println(str);
}
2.For循环遍历
for (String str : set) {
      System.out.println(str);
}
3.TreeSet倒叙遍历方法:
TreeSet VaildSet=new TreeSet<>(new Comparator() {
    @Override
    public int compare(String o1, String o2) {
        int num=o2.compareTo(o1);
        return num;
    }
});
 
for (String str : set) {
      System.out.println(str);
}
HashSet、LinkedHashSet、TreeSet区别:
1.HashSet中元素无序不重复;
2.LinkedHashSet中元素保持为输入的顺序;
3.TreeSet中元素保持为升序顺序;
 
 
四、List
 
常用实现类:
ArrayList、LinkedList
 
List特点:
1.允许存在重复元素;
2.允许用户直接指定存储位置,用户可用下标直接访问;
3.可以存储对象元素;
 
申明代码:
List arraylist=new ArrayList();
List linkedlist=new LinkedList();
 
List常见方法:
 
List遍历方法:
1.通过Key值索引获得对象
for (int i=0;i){
    System.out.println(list.get(i));
}
2.For循环遍历
for (String str:list){
    System.out.println(str);
}

 

3.迭代器遍历
Iterator iterator = list.iterator();
while (iterator.hasNext()){
    String next = iterator.next();
    System.out.println(next);
}
 
ArrayList、LinkedList区别:
1.ArrayList 易查询,如果用例中增删操作较多则不适用;
2.LinkedList 易增删,如果用例中增删操作较多则适用LinkedList存储.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

你可能感兴趣的:(常见数据结构Collection的特点及常用方法)