集合和数组区别
数组: 存储相同数据类型的容器。
特点:
长度固定
只能存储相同数据类型的。
数组可以存储基本数据类型,也可以存储引用数据类型。
集合特点:
长度不是固定的。
可以存储不同的数据类型。
只能存储引用数据类型。
package CollectionSet;
import java.util.ArrayList;
import java.util.Collection;
/*
* 数组: 存储相同数据类型的容器。
* 特点:
* 1.长度固定
* 2.只能存储相同数据类型的。
*
*
* Collection: 集合层次结构中的根界面 。 集合表示一组被称为其元素的对象。
* 一些集合允许重复元素,而其他集合不允许。
* ---List:
* -----ArrayList:数组结构。
* -----LinkedList
* -----Vector
*
* ---Set
* -----HashSet
* -----TreeSet
* 特点:
* 1.长度不是固定的。
* 2.可以存储不同的数据类型。
*/
public class demoCollection {
public static void main(String[] args) {
/*多态: 接口引用指向实现类对象 调用的都是子类的方法。
* 编译看父类,运行也看父类(除了非静态成员函数)
*/
Collection collection=new ArrayList();
/*
* 增加:add,addAll
*/
collection.add("aa");//添加字符串
collection.add(21);//添加Integer类型。
Collection collection2=new ArrayList();
collection2.add("bb");
collection2.add(22);
collection.add(collection2);
System.out.println("collection"+collection);
/*
* 查询:contains,containsAll
*
*/
boolean bn=collection.contains("aa");
System.out.println("如果此集合包含指定的元素:"+bn);
boolean bn2=collection.containsAll(collection2);
System.out.println("如果此集合包含指定 集合中的所有元素:"+bn2);
int size=collection.size();
System.out.println("size="+size);
/*
* 判断:equals
*/
boolean bn3=collection.equals(collection2);
System.out.println("两个集合是否相等:"+bn3);
int num=collection.hashCode();//返回对象的哈希码值
System.out.println("num="+num);
collection.clear(); //清空集合中所有的数据
System.out.println("collection="+collection);
/*
* 删除:clear,remove
*/
boolean bn5=collection.remove("小琪");
System.out.println("bn5="+bn5);
System.out.println("collection="+collection);
boolean bn6=collection2.removeAll(collection);
System.out.println("bn6="+bn6);
System.out.println("collection="+collection);
}
}
Collection和Map区别
Collection: 集合层次结构中的根界面。 集合就是存储对象最常用的一种方式。
一些集合允许重复元素,而其他集合不允许。
package CollectionSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/*
* Collection: 是集合,一次存储一个元素 输出值[]
* 特点:
* 1.添加 用add
*
*
* Map:是集合,一次存储的是键值对.并且健唯一。输出值{}
* 特点:
* 1.添加用put
* ---HashMap:键唯一。
*
*
* ---TreeMap:键可以排序
*
*
*
*/
public class demoMap {
public static void main(String[] args) {
// Map<String,Integer> map=new HashMap<String,Integer>();
//
// map.put("aa", 20);
// map.put("bb", 22);
// map.put("aa1", 20);
//
//
// //判断:
// boolean bn=map.containsKey("aa");
// System.out.println("判断键aa存在:"+bn);
//
// boolean bn2=map.containsValue(20);
// System.out.println("判断值20存在:"+bn2);
//
//
// //查询
// Object value=map.get("aa1");//根据键获取值
// System.out.println("value="+value);
//
// System.out.println(map);
//
// test1_HashMap();
//test2_TreeMap();
//test1();
//test2();
test3();
}
public static void test1_HashMap(){
//键是唯一的。
HashMap<String, Integer> map=new HashMap<String,Integer>();
map.put("凉枫", 20);
map.put("烟雨", 22);
map.put("凉枫1", 20);
System.out.println(map);
}
public static void test2_TreeMap(){
TreeMap<Integer, String> treeMap=new TreeMap<Integer, String>();
treeMap.put(10, "纸箱和猫");
treeMap.put(30, "南浔");
treeMap.put(20, "每天");
treeMap.put(50, "东吉");
System.out.println(treeMap);
}
//3.直接获取键和值集合
public static void test3(){
Map<String,Integer> map=new HashMap<String,Integer> ();
map.put("凉枫", 20);
map.put("烟雨", 22);
map.put("凉枫1", 20);
/* Set<Map.Entry<K,V>> set集合里面存储了Map.Entry对象
* Map.Entry:就是一个接口名,
*/
//把map集合存储的元素给set集合。
Set<Map.Entry<String,Integer>> set=map.entrySet();
//把set集合的元素存到迭代器里面
Iterator<Map.Entry<String,Integer>> iterator=set.iterator();
while(iterator.hasNext()){
//接口主要有两个方法,一个是获取键,一个是获取值
Map.Entry<String,Integer> me=iterator.next();
String key=me.getKey();
Integer value=me.getValue();
System.out.println("key="+key+" value="+value);
}
}
//2.直接获取健得集合,然后通过键获取值
public static void test2(){
Map<String,Integer> map=new HashMap<String,Integer>();
map.put("凉枫", 20);
map.put("烟雨", 22);
map.put("凉枫1", 20);
//返回键的集合
Set<String> set=map.keySet();
for(String key: set){
//通过键获取值
Integer value=map.get(key);
System.out.println("key="+key+" value="+value);
}
}
//1.直接获取值得集合
public static void test1(){
Map<String,Integer> map=new HashMap<String,Integer>();
map.put("凉枫", 20);
map.put("烟雨", 22);
map.put("凉枫1", 20);
//直接获取值得集合
Collection<Integer> collection=map.values();
for(Integer obj: collection){
System.out.println("value="+obj);
}
}
}
List:
---ArrayList:数组结构。
---LinkedList:
---Vector
Set:
-----HashSet
-----TreeSet
Map:是集合,一次存储的是键值对.并且健唯一。输出值{}
特点:
1.添加用put
-
---HashMap:键唯一。 ---TreeMap:键可以排序
Collection知识点
List:有序的(相当于身份证,不是排序),可以存储相同的元素。
ArrayList:可调整大小的数组的实现List接口,此实现不同步,并允许所有元素,
包括null
适合查询
ArrayList 采用的是数组形式来保存对象的,这种方式将对象放在连续的位
置中,所以最大的缺点就是插入删除时非常麻烦;
–LinkedList:双链表实现了List和Deque接口。此实现不同步, 实现所有可选列表操
作,并允许所有元素(包括null )。
适合增删
LinkedList 采用的将对象存放在独立的空间中,而且在每个空间中还保
存下一个链接的索引 但是缺点就是查找非常麻烦 要丛第一个索
引开始;
Vector: 被ArrayList替换了,同步的,线程安全的。
Vector类实现了可扩展的对象数组。 像数组一样,它包含可以使用整
数索引访问的组件。
Vector是同步的。 如果不需要线程安全的实现,建议使用ArrayList代替Vector
package CollectionSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
List:有序的(相当于身份证,不是排序)存储相同的元素。
--ArrayList:可调整大小的数组的实现List接口,此实现不同步,并允许所有元素,包括null
适合查询
ArrayList 采用的是数组形式来保存对象的,这种方式将对象放在连续的位置中,所以最大的缺点就是插入删除时非常麻烦;
--LinkedList:双链表实现了List和Deque接口。此实现不同步, 实现所有可选列表操作,并允许所有元素(包括null )。
适合增删
LinkedList 采用的将对象存放在独立的空间中,而且在每个空间中还保存下一个链接的索引 但是缺点就是查找非常麻烦 要丛第一个索引开始;
--Vector: 被ArrayList替换了,同步的。
Vector类实现了可扩展的对象数组。 像数组一样,它包含可以使用整数索引访问的组件。
Vector是同步的。 如果不需要线程安全的实现,建议使用ArrayList代替Vector
public class demoList {
public static void main(String[] args) {
test_ArrayList();
}
public static void test_ArrayList() {
ArrayList arrayList=new ArrayList();
arrayList.add("朋友1");
arrayList.add("朋友2");
arrayList.add("朋友3");
System.out.println("arrayList="+arrayList);
//添加
arrayList.add(3, "朋友4");//在3下标添加指定的元素
System.out.println("arrayList="+arrayList);
//查询
Object object=arrayList.get(1);
System.out.println("obj="+object);
int index=arrayList.indexOf("朋友3");
System.out.println("index="+index);
//遍历 把集合的元素存储到迭代器里面
Iterator iterator=arrayList.iterator();
while (iterator.hasNext()) {
Object object2 = iterator.next();
System.out.println("object2="+object2);
}
System.out.println("===================");
ListIterator listIterator=arrayList.listIterator();
listIterator.add("小丑");
listIterator.add("玖玖");
// 小丑 玖玖 ↑ 冯二1 南浔 冯二2 冯二3
while(listIterator.hasPrevious()){//有没有前一个元素
Object object2=listIterator.previous();//返回前一个元素
System.out.println("object2="+object2);
}
}
}
Set:无序(不是排序,是指不能存储相同的元素)的,不能存储相同的元素
--HashSet:存储的元素唯一 此类实现Set接口,由哈希表(实际为HashMap
实例)支持。底层是散列表(链表加+数组的结构)
保证元素唯一的原理:重写hashCode方法和equals方法。
--TreeSet:存储的元素唯一,可以排序。
底层是红黑树数据结构。
排序的原理:
第一种:元素自身具备可比性(实现comparable接口,然后重
写compareTo方法)。
第二种:集合具备可比性(创建一个子类去实现Comparator接
口,然后重写compare方法,初始化集合对象的时候
初始化比较器对象)
package CollectionSet;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
/*
* Set:无序(不是排序,是指不能存储相同的元素)的,不能存储相同的元素
* --HashSet:存储的元素唯一 此类实现Set接口,由哈希表(实际为HashMap实例)支持
*
* --TreeSet:存储的元素唯一,可以排序
*/
public class demoSet {
public static void main(String[] args) {
Set set=new HashSet();
//test1();
test2();
}
public static void test1() {
HashSet hashSet=new HashSet();
hashSet.add("同学1");
hashSet.add("同学2");
hashSet.add("同学3");
hashSet.add("同学4");
System.out.println("hashSet"+hashSet);
//唯一的,怎么去除 hashCode和equals的原理
hashSet.add(new Person("aa"));
hashSet.add(new Person("bb"));
System.out.println(hashSet);
}
public static void test2() {
TreeSet treeSet=new TreeSet();
treeSet.add(new QQ(21));
treeSet.add(new QQ(21));
treeSet.add(new QQ(21));
System.out.println(treeSet);
}
}
class Person{
private String name;
public Person(String name) {
this.name=name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return name;
}
}
class QQ implements Comparable<QQ>{
private Integer qq;
public QQ(Integer qq){
this.qq=qq;
}
public String toString(){
return qq+"";
}
@Override
public int compareTo(QQ o) {
int num=qq.compareTo(o.qq);
System.out.println(4+" qq:"+qq+" o.qq="+o.qq);
return num;//1 30大 结果 21 30
}
}
Hashset
package CollectionSet;
import java.util.HashSet;
/*
* 保证对象的唯一性:
* 对象具备该特点:
* 1.重写hashCode方法
* 2.重写equals方法
* 3.重写toString方法。
*
*
*
*总结: 当返回的hashCode相同的时候,才会执行equals。
* 当返回的hashCode不同的时候,不会执equals,就可以共存。
* equals返回true,不可以共存,返回false可以共存。
*/
//2
//3
//7 num=2735916
//4
//5
//7 num=2735885
//6
//[YUXI, YUYI]
//7
public class demoHashSet {
public static void main(String[] args) {
HashSet<Person1> person1s=new HashSet<Person1>();
System.out.println(2);
Person1 person1=new Person1("YUYI");
System.out.println(3);
person1s.add(person1);//默认调用了hashCode方法
System.out.println(4);
Person1 person2=new Person1("YUXI");
System.out.println(5);
person1s.add(person2);
System.out.println(6);//默认调用了hashCode方法
System.out.println(person1s);
System.out.println(7);
}
}
/*
* 保证名字唯一。
*/
class Person1{
private String name;
public Person1(String name) {
this.name=name;
}
//这一步已经告诉了我们相同还是不相同。
public int hashCode(){
int num=name.hashCode();
System.out.println(8+" num="+num);
return num;
}
//当返回的hashCode值相同的时候,才会执行equals方法。
//return 返回false,就代表共存。返回true,代表不共存。
public boolean equals(Object object) {
Person1 person1=(Person1)object;
System.out.println("person1.name="+person1.name);
System.out.println(1);
return true;
}
public String toString(){
return name;
}
}
排序需要的比较器接口
Comparator:比较器接口,这是让集合具备可比性。
它的方法是compare方法
Comparable:是一个比较器接口,这是让元素(存储的元素)自身具备可比性
它的方法是compareTo方法
5.泛型
泛型是jdk1.5以后出现的知识点
泛型的格式:
-
集合类名<参数类型> 对象名=new 集合类型<参数类型>();
为什么使用<>: 因为()被方法给用了 {}被类体和方法体给用了,只有<>
为什么使用泛型:
减少了强制转换。
统一数据类型,提高安全性。
把运行期遇到的问题提到编译期。
优化了程序的设计。
package CollectionSet;
import java.util.ArrayList;
import java.util.Iterator;
/*
* 泛型的格式:
* 集合类名<参数类型> 对象名=new 集合类型<参数类型>();
*
* 为什么使用<>: 因为()被方法给用了 {}被类体和方法体给用了,只有<>
*
* 为什么使用泛型:
* 1.减少了强制转换。
* 2.统一数据类型,提高安全性。
* 3.把运行期遇到的问题提到编译期。
*
*
*/
public class demoGeneric {
public static void main(String[] args) {
ArrayList<String> arrayList=new ArrayList<String>();
arrayList.add("冯二1");
arrayList.add("冯二2");
Iterator<String> iterator=arrayList.iterator();
while(iterator.hasNext()){
String s=iterator.next();
System.out.println(s);
}
System.out.println("================");
for(String obj: arrayList){
//强制类型转换了
// String string=(String) obj;
System.out.println(obj);
}
System.out.println("=============");
test1();
}
public static void test1(){
//说明了集合只能存储对象,集合只能存储引用数据类型。
ArrayList<Integer> arrayList=new ArrayList<Integer>();
arrayList.add(10);
arrayList.add(20);
System.out.println(arrayList);
}
}
6.Map知识点
Map:是集合,一次存储的是键值对.并且健唯一。输出值{}
特点:
1.添加用put
---HashMap:键唯一。底层是散列表(链表加+数组的结构)
保证元素唯一的原理:重写hashCode方法和equals方法。
---TreeMap:键可以排序
底层是红黑树数据结构。
排序的原理:
第一种:元素自身具备可比性(实现comparable接口,然后重
写compareTo方法)。
第二种:集合具备可比性(创建一个子类去实现Comparator接
口,然后重写compare方法,初始化集合对象的时候
初始化比较器对象)
---HashTable: 跟HashMap一致,不一样的地方(
1.HashMap线程不安全,不同步,效率
高;HashTable是线程安全,同步,效率低。
2.HashMap可以存储键值对
都可以为null;HashTable键值对都不能为null)