java 基本数据类型
byte、short、int、long、float、double、char、boolean。
原文: https://blog.youkuaiyun.com/weixin_44551646/article/details/94295677
List集合
List集合为列表类型,以线性方式存储对象。List集合中的元素允许重复,各元素的顺序就是对象插入的顺序。用户可以通过使用索引来访问List集合中的元素。
ArrayList集合
底层是使用数组实现,所以查询速度快,增删速度慢
LinkedList集合
是基于链表结构实现的,所以查询速度慢,增删速度快,提供了特殊的方法,对头尾的元素操作(进行增删查)
ArrayList与LinkedList
ArrayList和LinkedList顾名思义,ArrayList是Array(动态数组)的数据结构,相当于动态数组;LinkedList是Link(链表)的双向数据结构,也可当作堆栈、队列、双端队列。
对于随机访问List时(get和set操作),ArrayList比LinkedList的效率更高,因为LinkedList是线性的数据存储方式,所以需要移动指针从前往后依次查找。
对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据(可以在上述ArrayList代码中体现)。
两者缺点都为线性不安全
ArrayList和LinkedList线程不安全,在多线程中不建议使用。
上两个线程都不安全 vector 线程安全 但是比上面的两个增删改查都慢
Set集合
- Set集合中的对象不按特定的方式排序,只是简单的将对象加入到集合中,但是Set集合不能包括重复对象
- Set接口继承了Collection接口,因此也包含Collection接口的所有方法
哈希表:
Hashset集合
我们要想往hashSet里存入数据,就只能重写hashcode()和equals()方法!! hashset 存入的数据其实是根据一定的顺序排列的 这里的顺序跟存入的顺序不是一个概念
public class Student{
private String name;
private int id;
public Student(){}
public Student(String name, int id)
{
super();
this.name = name;
this.id = id;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (id != other.id)
return false;
if (name == null)
{
if (other.name != null)
return false;
}
else if (!name.equals(other.name))
return false;
return true;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
@Override
public String toString()
{
return "Student [name=" + name + ", id=" + id + "]";
}
}
import java.util.HashSet;
import java.util.Iterator;
class Test{
public static void main(String []args) {
HashSet<Student> hs = new HashSet<>();
hs.add(new Student("yiyi",1));
hs.add(new Student("feifei",2));
hs.add(new Student("lili",3));
System.out.println(hs.add(new Student("wawa",4)));
System.out.println(hs.add(new Student("wawa",4)));
System.out.println(hs); //第一种打印方式,利用重写的toString()方法和Println()直接打印
System.out.println("------------");
for(Student s : hs) { //第二种打印方式,增强for循环
System.out.println(s);
}
System.out.println("------------"); //第三种打印方式,利用Iteator
Iterator<Student> it = hs.iterator();
while(it.hasNext()) {
Student s = it.next();
System.out.println(s);
}
}
}
true
false
[Student [name=feifei, id=2], Student [name=wawa, id=4], Student [name=yiyi, id=1], Student [name=lili, id=3]]
------------
Student [name=feifei, id=2]
Student [name=wawa, id=4]
Student [name=yiyi, id=1]
Student [name=lili, id=3]
------------
Student [name=feifei, id=2]
Student [name=wawa, id=4]
Student [name=yiyi, id=1]
Student [name=lili, id=3]
LinkedHashSet集合
LinkedHashSet继承自HashSet,特点是:有序,唯一,效率高
TreeSet集合
TreeSet为使用树来进行储存的Set接口提供了一个工具,对象按升序储存,访问和检索是很快的。在存储了大量的需要进行快速检索的排序信息的情况下,TreeSet是一个不错的选择。
Map集合
HashMap集合
Map集合没有继承Collection接口,其提供的是键到值的映射。Map不能包含相同的键,每个键只能映射一个值。键还决定了储存对象在映射中的储存位置。单一,无序
LinkedHashMap集合
用法跟HashMap基本一致,它是基于链表和哈希表结构的所以具有存取有序,键不重复的特性,存的顺序和遍历出来的顺序是一致的。
TreeMap集合
TreeMap与TreeSet类似,都需要重写比较器(外部比较器+内部比较器)
TreeMap集合特点:单一,有序