@liruiyi962464
2017-05-16T15:50:58.000000Z
字数 4061
阅读 453
java
- ArrayList:底层是由数组实现的,查询效率高,删除插入效率低
//产生一个ArrayList容器
ArrayList<String> arrayList = new ArrayList<>();
//通过add(index);添加数据
arrayList.add("leader");
arrayList.add("leader");
arrayList.add("Mahatma");
//通过下标获取单个元素。get(int index);
arrayList.get(下标);
//删除
arrayList.remove(下标);
arrayList.remove(要删除的元素);
//size(),indexOf(),
//遍历容器ArrayList。
//(1).通过get()和size();
for(int i = 0;i<arrayList.size();i++){
System.out.println(arrayList.get(i));
}
//(2).foreach循环遍历,也叫增强for循环。
for (String str : arrayList) {
System.out.println(str);
}
//(3).通过迭代器遍历。
//获得迭代器。
Iterator<String> it = arrayList.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
- LinkedList:底层是由链表实现的,查询效率低,删除插入效率高
//产生一个LinkedList容器
LinkedList<String> linkedList = new LinkedList<String>();
//栈:先进后出,有一个口
addFirst();removeFirst();
addLast();removeLast();
//队列:先进先出,有两个口
addFirst();removeLast();
addLast();removeFirst();
- HashSet:存放自定义的类的对象时,要重写equals()和hashCode()方法
HashSet<String> hashSet = new HashSet<String>();
HashSet<Person> hashSet = new HashSet<Person>();
//遍历
//foreach
for (Person person1 : hashSet2) {
System.out.println(person1);
}
//迭代器遍历
Iterator<Person> it = hashSet2.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
- TreeSet:存放自定义的类的对象时,要么该类实现Comparble接口,要么在创建容器时通过匿名内部类实现Compartor:
TreeSet<String> treeSet = new TreeSet<String>();
TreeSet<Person> treeSet = new TreeSet<Person>();
//自定义类实现实现Comparble
public int compareTo(Person o) {
if(this.getAge()>o.getAge()){
return 1;
}else if(this.getAge()<o.getAge()){
return -1;
}else{if(!this.getName().equals(0.getName)){
return this.getName().compareTo(o.getName());
}else
return 0;
}
}
//创建容器时,由匿名内部类实现
TreeSet<Person0216> treeSet = new TreeSet<Person0216>(new Comparator<Person0216>() {
@Override
public int compare(Person0216 o1, Person0216 o2) {
// TODO Auto-generated method stub
if(o1.getAge()>o2.getAge()){
return 1;
}else if(o1.getAge()<o2.getAge()){
return -1;
}else{
if(!o1.getName().equals(o2.getName())){
return o1.getName().compareTo(o2.getName());
}else{
return 0;
}
}
}
});
- HashMap:
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
hashMap.put(1, "a");
hashMap.put(1, "b");
//当俩个key一样时 覆盖旧的值
//key值是唯一确定的,出现重复时覆盖旧值
System.out.println(hashMap);
hashMap.put(2, "c");
hashMap.put(3, "c");
//value可以重复
System.out.println(hashMap);
//遍历。
//(1).通过Value值类遍历。
Collection<String> values = hashMap.values();
for (String str : values) {
System.out.println(str);
}
//(2).通过Key值类遍历。
Set<String> key = hashMap.keySet();
for (String str : key) {
System.out.println(str+"---->"+hashMap.get(str));
}
//(3).通过Entry(键值对)来遍历。
Set<Entry<String, String>> entry = hashMap.entrySet();
for (Entry<String, String> e : entry) {
System.out.println(e);
}
- TreeMap:如果Key是自定类的对象,Key值要么实现Comparble接口,要么在创建容器时通过匿名内部类实现Compartor
//添加
TreeMap<String, String> treeMap = new TreeMap<>();
treeMap.put("a", "tyu");
treeMap.put("b", "yuio");
treeMap.put("f", "yuiyj");
treeMap.put("c", "ioioo");
//treeMap.get(Key的值);
System.out.println(treeMap.get("f"));
遍历
treeMap.keySet();
treeMap.values();
treeMap.entrySet();
关键字 | 作用 |
---|---|
Iterable | 迭代器接口 |
Collection ( Iterble的子接口 ) | 类集接口 |
List ( Collection的子接口 ) | 列表接口 |
Set ( Collection的子接口 ) | 数据集接口 |
Queue ( Collection的子接口 ) | 队列 |
Map | 键-值对组合映射表 |
- 允许对象成为“foreach”语句的目标
- 类集接口Collection是Iterble的子接口
- 方法如下
//功能:返回一个在一组T类型的元素上进行迭代的迭代器
Iterator<T> it = treeSet.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
- 类集工具类
- 定义了若干用于类和映射的算法,这些方法被定义为静态方法
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("a");
arrayList.add("d");
arrayList.add("c");
arrayList.add("e");
arrayList.add("b");
//排序
Collections.sort(arrayList);
System.out.println(arrayList);
//查询 不存在返回-1
System.out.println(Collections.binarySearch(arrayList, "123"));
//反转输出
Collections.reverse(arrayList);
System.out.println(arrayList);
//随机输出
Collections.shuffle(arrayList);
System.out.println(arrayList);
//交换位置
Collections.swap(arrayList, 1, 3);
System.out.println(arrayList);
//替换
Collections.replaceAll(arrayList, "a" , "nishisbu");
System.out.println(arrayList);
//填充
Collections.fill(arrayList, "123456");
System.out.println(arrayList);
- List接口扩展了Collection,特点:有序且可重复的
- Set接口扩展了Collection,特点:无序且不可重复的
- 映射(map)是一个存储关键字/值对的对象,给定一个关键字,可查询得到它的值;关键字和值都可以是对象,映射不是Collection的子接口,所以它本身不能使用迭代器来进行遍历