Java 集合初步了解

ArrayList

单线程可用,线程没有Vector安全

ArrayList al = new ArrayList();
for(Object o : al){
	system.out.print(o);
}

泛型 指定存入类型后,AllayList只能存入制定的数据类型

ArrayList<Integer> al = new ArrayList<Integer>();

vector

多线程可用,线程更安全
其他方法与ArrayList类似

LinkedList

频繁插入删除可用—>链表

能添加无序的,能重复的数据


HashSet —哈希值

TreeSet —树

只能添加无序的,不能重复的数据

自定义数据类型存储

    ArrayList<Student> arr = new ArrayList<Student>();
    vector<Student> arr = new vector<Student>();
    LinkedList<Student> arr = new LinkedList<Student>();
    HashSet <Student> = new HashSet <Student>();
    TreeSet  <Student> = new TreeSet  <Student>();

    class Student{
		private String Name;
		private int Age;
		public void Student(String Name,Int Age){
			this.Name = Name;
			this.Age = Age;
		}
	}
    

HashMap

只能添加无序的,不能重复的key
什么是HashMap?
HashMap 内部就是键值对,需要一个key 对应一个value 或 一个key 对应 一个集合

HashMap<k,v> map = new HashMap<k,v>();
map.put("name","comi");

遍历HashMap,获取map所有的key
返回set

	for(Object o:map.keyset()){
		system.out.print(o);
	}

遍历HashMap,获取map所有的value
map.value()返回collection

	for(Object o:map.value()){
		system.out.print(o);
	}

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章