集合
集合概念:
对象的容器,实现了对对象常用的操作,类似数组功能。
和数组的区别:
位置:
java.util.*;
Collection接口
Collection接口总体框架:
Collection接口的使用01
Collection接口的使用02
Collection collection = new ArrayList();
Student s1 = new Student("提莫",18);
Student s2 = new Student("小鱼人",13);
collection.add(s1);
collection.add(s2);
System.out.println("元素个数"+collection.size());
System.out.println(collection.toString());
collection.remove(new Student("提莫",18));
System.out.println("删除之后元素个数"+collection.size());
System.out.println(collection.toString());
for (Object object: collection
) {
Student student = (Student) object;
System.out.println(student.toString());
}
Iterator it = collection.iterator();
while (it.hasNext()){
Student ss = (Student) it.next();
System.out.println(ss.toString());
}
System.out.println(collection.contains(s2));
System.out.println(collection.isEmpty());
List子接口
特点:
有序、有下标、元素可以重复。
方法:
- void add(int index, Object o) //在index位置插入对象o
- boolean addAll(int index, Collection c) //将一个集合中的元素添加到此集合中的index位置
- Object get(int index) //返回集合中指定位置的元素
- List subList(int fromIndex, int toIndex) //返回formIndex和toIndex之间的集合元素
List接口的使用01
List list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add(0,"grape");
System.out.println(list.size());
System.out.println(list.toString());
list.remove("apple");
System.out.println(list.size());
System.out.println(list.toString());
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
for (Object object :list
) {
System.out.println(object);
}
Iterator it = list.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
ListIterator lit = list.listIterator();
while (lit.hasNext()){
System.out.println(lit.nextIndex()+":"+lit.next());
}
while (lit.hasPrevious()){
System.out.println(lit.previousIndex()+":"+lit.previous());
}
System.out.println(list.contains("grape"));
System.out.println(list.isEmpty());
System.out.println(list.get(0));
System.out.println(list.indexOf("banana"));
List接口的使用02
List list = new ArrayList();
list.add(10);
list.add(200);
list.add(30);
list.add(40);
list.add(50);
list.add(10);
System.out.println(list.size());
System.out.println(list.toString());
List subList = list.subList(0,2);
System.out.println(subList.toString());
List实现类
ArrayList:
-
数据结构实现,查询快、增删慢; -
JDK1.2版本,运行效率快,线程不安全。 -
源码分析:
-
ArrayList的使用:
ArrayList arrayList = new ArrayList<>();
Student s1 = new Student("teemo",18);
Student s2 = new Student("lily",10);
Student s3 = new Student("dingding",12);
Student s4 = new Student("papa",20);
Student s5 = new Student("kaka",14);
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
arrayList.add(s4);
arrayList.add(s5);
System.out.println(arrayList.size());
System.out.println(arrayList.toString());
arrayList.remove(new Student("teemo",18));
System.out.println(arrayList.size());
System.out.println(arrayList.toString());
Iterator it = arrayList.iterator();
while (it.hasNext()){
Student student = (Student) it.next();
System.out.println(student.toString());
}
ListIterator lit = arrayList.listIterator();
while (lit.hasNext()){
Student student = (Student) lit.next();
System.out.println(student.toString());
}
while (lit.hasPrevious()){
Student student = (Student) lit.previous();
System.out.println(student);
}
System.out.println(arrayList.contains(new Student("kaka",14)));
System.out.println(arrayList.isEmpty());
System.out.println(arrayList.indexOf(new Student("kaka",14)));
System.out.println(arrayList.get(1));
Vector:
-
数组结构实现,查询快、增删慢; -
JDK1.0版本,运行效率慢,线程安全。
LinkedList:
LinkedList linkedList = new LinkedList();
Student s1 = new Student("teemo",18);
Student s2 = new Student("lily",10);
Student s3 = new Student("dingding",12);
Student s4 = new Student("papa",20);
Student s5 = new Student("kaka",14);
linkedList.add(s1);
linkedList.add(s2);
linkedList.add(s3);
linkedList.add(s4);
linkedList.add(s5);
System.out.println(linkedList.size());
System.out.println(linkedList.toString());
for (int i = 0; i < linkedList.size(); i++) {
System.out.println(linkedList.get(i));
}
for (Object o : linkedList) {
Student student = (Student)o;
System.out.println(student.toString());
}
Iterator it = linkedList.iterator();
while (it.hasNext()){
Student s = (Student) it.next();
System.out.println(s.toString());
}
ListIterator lit = linkedList.listIterator();
while (lit.hasNext()){
Student s = (Student) lit.next();
System.out.println(s.toString());
}
while (lit.hasPrevious()){
Student s = (Student) lit.previous();
System.out.println(s.toString());
}
System.out.println(linkedList.contains(new Student("lily",10)));
System.out.println(linkedList.indexOf(s2));
Set子接口
特点
无序、无下标、元素不可重复
方法
全部继承自Collection中的方法
Set接口的使用
public class SetDemo01 {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("apple");
set.add("grape");
set.add("watermellon");
set.add("strawberry");
System.out.println(set.size());
System.out.println(set.toString());
set.remove("apple");
System.out.println(set.toString());
for (String string: set
) {
System.out.println(string);
}
Iterator<String> it = set.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
System.out.println(set.contains("strawbery"));
System.out.println(set.isEmpty());
}
Set实现类
HashSet
- 基于HashCode实现,元素不重复;
- 当存入元素的哈希码相同时,会调用equals方法进行确认,如结果为true,则拒绝后者存入。
- HashSet的使用01:
public class HashSetDemo01 {
public static void main(String[] args) {
HashSet<String> hashSet = new HashSet<>();
hashSet.add("teemo");
hashSet.add("papa");
hashSet.add("dingdang");
hashSet.add("meow");
System.out.println(hashSet.size());
System.out.println(hashSet.toString());
hashSet.remove("teemo");
System.out.println(hashSet.size());
System.out.println(hashSet.toString());
for (String string: hashSet
) {
System.out.println(string);
}
Iterator<String> it = hashSet.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
System.out.println(hashSet.contains("papa"));
System.out.println(hashSet.isEmpty());
}
}
public class HashSetDemo02 {
public static void main(String[] args) {
HashSet<Person> persons = new HashSet<>();
Person p1 = new Person("teemo", 18);
Person p2 = new Person("dami", 18);
Person p3 = new Person("dingding", 18);
Person p4 = new Person("papa", 18);
persons.add(p1);
persons.add(p2);
persons.add(p3);
persons.add(p4);
persons.add(new Person("papa",18));
System.out.println(persons.size());
System.out.println(persons.toString());
for (Person p:persons
) {
System.out.println(p.toString());
}
Iterator<Person> it = persons.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
System.out.println(persons.contains(p2));
System.out.println(persons.isEmpty());
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
TreeSet
- 基于排列顺序实现元素不重复;
- 实现了SortedSet接口,对集合元素自动排序;
- 元素对象的类型必须实现comparable接口,指定排序规则;
- 通过CompareTo方法确定是否为重复元素。
- TreeSet的使用01:
public class TreeSetDemo01 {
public static void main(String[] args) {
TreeSet<String> treeSet = new TreeSet<>();
treeSet.add("abc");
treeSet.add("apple");
treeSet.add("strawberry");
System.out.println(treeSet.size());
System.out.println(treeSet.toString());
for (String s : treeSet) {
System.out.println(s);
}
Iterator<String> it = treeSet.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
System.out.println(treeSet.contains("abc"));
}
}
public class TreeSetDemo02{
public static void main(String[] args) {
TreeSet<Person> persons = new TreeSet<>();
Person p1 = new Person("teemo", 20);
Person p2 = new Person("dami", 18);
Person p3 = new Person("dingding", 18);
Person p4 = new Person("papa", 15);
Person p5 = new Person("papa", 10);
persons.add(p1);
persons.add(p2);
persons.add(p3);
persons.add(p4);
persons.add(p5);
persons.add(new Person("papa",18));
System.out.println(persons.size());
System.out.println(persons.toString());
persons.remove(new Person("teemo",20));
System.out.println(persons.size());
for (Person person : persons) {
System.out.println(person.toString());
}
Iterator<Person> it = persons.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
System.out.println(persons.contains(new Person("teemo",20)));
}
}
public class Person implements Comparable<Person>{
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.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;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public int compareTo(Person o) {
int n1 = this.getName().compareTo(o.getName());
int n2 = this.getAge()-o.getAge();
return n1==0?n2:n1;
}
}
- TreeSet的使用(Comparator定制比较规则):
public class TreeSetDemo03 {
public static void main(String[] args) {
TreeSet<Person> persons = new TreeSet<>(new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
int n1 = o1.getAge()-o2.getAge();
int n2 = o1.getName().compareTo(o2.getName());
return n1==0?n2:n1;
}
});
Person p1 = new Person("teemo", 18);
Person p2 = new Person("dami", 15);
Person p3 = new Person("dingding", 18);
Person p4 = new Person("papa", 20);
persons.add(p1);
persons.add(p2);
persons.add(p3);
persons.add(p4);
persons.add(new Person("papa",18));
System.out.println(persons.size());
System.out.println(persons.toString());
}
}
- TreeSet的使用(实现字符串按照长度进行排序):
public class TreeSetDemo04 {
public static void main(String[] args) {
TreeSet<String> treeSet = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int n1 = o1.length()-o2.length();
int n2 = o1.compareTo(o2);
return n1==0?n2:n1;
}
});
treeSet.add("hello");
treeSet.add("apple");
treeSet.add("strawberry");
treeSet.add("abc");
System.out.println(treeSet.size());
System.out.println(treeSet.toString());
}
}
泛型
泛型概念:
Java泛型是JDK1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递。
常见形式:
泛型类
public class MyGeneric<T> {
T t;
public void show(T t){
System.out.println(t);
}
public T getT(){
return t;
}
}
泛型接口
public interface MyInterface<T> {
String name = "teemo";
T server(T t );
}
泛型方法
public class MyGenericMethod {
public <T> T show(T t){
System.out.println("generic method!!" + t);
return t;
}
}
语法
<T,…> T称为类型占位符,表示一种引用类型。
好处
- 提高代码的重用性;
- 防止类型转换异常,提高代码的安全性。
泛型集合
泛型集合概念
参数化类型、类型安全的集合,强制集合元素的类型必须一致。
特点
- 编译时即可检查,而非运行时抛出异常;
- 访问时,不必类型转换(拆箱);
- 不同泛型之间引用不能相互赋值,泛型不存在多态。
Map接口
特点:
存储一对数据(Key-Value),无序、无下标,键不可重复,值可重复。
方法:
-
V put(K key, V value) //将对象存入到集合中,关联键值。key重复则覆盖原值。 -
Object get(Object key) //根绝键获取对应的值。 -
Set //返回所有Key。 -
Collection values() //返回包含所有值的Collection集合。 -
Set<Map.Entry<K,V>> //键值匹配的Set集合。
Map接口的使用
public class MapDemo01 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("cn","zhongguo");
map.put("uk","yingguo");
map.put("usa","meiguo");
System.out.println(map.size());
System.out.println(map.toString());
for (String s : map.keySet()) {
System.out.println(s+":"+map.get(s));
}
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "---" + entry.getValue());
}
System.out.println(map.containsKey("usa"));
System.out.println(map.containsValue("yingguo"));
}
}
Map实现类:
HashMap
- JDK1.2版本,线程不安全,运行效率快;允许用null作为key或value。
- HashMap集合的使用:
public class HashMapDemo01 {
public static void main(String[] args) {
HashMap<Student,String> students = new HashMap<>();
Student s1 = new Student("teemo",001);
Student s2 = new Student("lily",002);
Student s3 = new Student("papa",003);
students.put(s1,"beijing");
students.put(s2,"tianjin");
students.put(s3, "henan");
students.put(new Student("teemo",001),"hefei");
System.out.println(students.size());
System.out.println(students.toString());
for (Student key : students.keySet()) {
System.out.println(key.toString()+"---"+students.get(key));
}
for (Map.Entry<Student, String> entry : students.entrySet()) {
System.out.println(entry.getKey()+"---"+entry.getValue());
}
System.out.println(students.containsKey(new Student("teemo",001)));
System.out.println(students.containsValue("henan"));
}
}
Hashtable
- JDK1.0版本,线程安全,运行效率慢;不允许null作为key或value。
Properties
- Hashtable的子类,要求key和value都是String。通常用于配置文件的读取。
TreeMap
public class TreeMapDemo01 {
public static void main(String[] args) {
TreeMap<Student, String> students = new TreeMap<Student, String>();
Student s1 = new Student("teemo",001);
Student s2 = new Student("lily",002);
Student s3 = new Student("papa",003);
students.put(s1,"beijing");
students.put(s2,"tianjin");
students.put(s3, "henan");
students.put(new Student("teemo",001), "hefei");
System.out.println(students.size());
System.out.println(students.toString());
students.remove(s2);
System.out.println(students.size());
for (Student key : students.keySet()) {
System.out.println(key.toString()+"---"+students.get(key));
}
for (Map.Entry<Student, String> entry : students.entrySet()) {
System.out.println(entry.getKey()+"---"+entry.getValue());
}
System.out.println(students.containsKey(new Student("teemo",001)));
System.out.println(students.containsValue("henan"));
}
}
Collections工具类
概念
集合工具类,定义了除了存取以外的集合常用方法。
方法
- public static void reverse(List<?> list) //反转集合中元素的顺序
- public static void shuffle(List<?> list) //随机重置集合元素的顺序
- public static void sort(List<?> list) //升序排序(元素类型必须实现Comparable接口)
Collections的使用
public class CollectionsDemo {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(200);
list.add(2);
list.add(50);
list.add(3);
System.out.println("before sorted:"+list.toString());
Collections.sort(list);
System.out.println("after sorted: "+list.toString());
int i = Collections.binarySearch(list, 50);
System.out.println(i);
List<Integer> dest = new ArrayList<>();
for (int j = 0; j < list.size(); j++) {
dest.add(0);
}
Collections.copy(dest,list);
System.out.println(dest.toString());
Collections.reverse(list);
System.out.println(list);
Collections.shuffle(list);
System.out.println(list);
Integer[] arr = list.toArray(new Integer[10]);
System.out.println(arr.length);
System.out.println(Arrays.toString(arr));
String[] name = {"aa","bb","cc"};
List<String> list1 = Arrays.asList(name);
System.out.println(list1);
Integer[] num = {10,2,500,30,4};
List<Integer> list2 = Arrays.asList(num);
System.out.println(list2);
}
}
集合总结
- 集合的概念:对象的容器,和数组类似,定义了对多个对象进行操作的常用方法。
- List集合:有序、有下标、元素可以重复。(ArrayList、LinkedList、Vector)
- Set集合:无序、无下标、元素不可重复。(HashSet、TreeSet)
- Map集合:存储一对数据,无序、无下标,键不可重复,值可重复。(HashMap、HashMap、TreeMap)
- Collections:集合工具类,定义了除了存取以外的集合常用方法。
|