集合
在java基础中,集合可大致分为以下几个类别
学生对象案例代码(后面会用到)
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
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;
}
}
Collection单列
Collection集合
前言:之所以称Collection为单列集合是因为对比Map集合,每一个元素是单独存在的,不存在配对关系
集合概述
添加元素
Collection<String> c=new ArrayList<String>();
System.out.println(c.add("hello"));
System.out.println(c.add("world"));
移除元素
Collection<String> c=new ArrayList<String>();
c.add("hello");
c.add("world");
c.add("java");
System.out.println(c.remove("world"));
判断集合存在指定元素
Collection<String> c=new ArrayList<String>();
c.add("hello");
c.add("world");
c.add("java");
System.out.println(c.contains("world"));
清空集合元素
Collection<String> c=new ArrayList<String>();
c.add("hello");
c.clear();
判断是否存在元素
Collection<String> c=new ArrayList<String>();
c.add("hello");
System.out.println(c.isEmpty());
集合长度
System.out.println(c.size());
遍历集合
public class Collection遍历元素 {
public static void main(String[] args) {
Collection<String> c=new ArrayList<String>();
c.add("hello");
c.add("world");
c.add("java");
Iterator<String> it = c.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}}}
集合方法
顺序排列
Collection.sort(list);
反转顺序
Collection.reverse(list);
随机排列
Collections.shuffle(list);
List集合
list集合特点
1.有序:存储元素和取出顺序一致
2.可重复:元素可以重复
集合概述
添加元素
List<String> list=new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
指定位置插入元素
List<String> list=new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
list.add(1,"javaee");
删除指定元素(返回被删除的元素)
List<String> list=new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
System.out.println(list.remove(1));
修改指定索引处元素(返回被修改的元素)
List<String> list=new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
System.out.println(list.set(1,"javaee"));
输出列表 System.out.println(list);
遍历
三种遍历方式(结合学生对象案例) 其中增强for循环最为常用
List<Student> list=new ArrayList<Student>();
Student s1=new Student("Betty","30");
Student s2=new Student("Btty","30");
Student s3=new Student("Bety","30");
list.add(s1);
list.add(s2);
list.add(s3);
Iterator<Student> it=list.iterator();
while(it.hasNext()){
Student s=it.next();
System.out.println(s.getName()+","+s.getAge());
}
System.out.println("--------");
for(int i=0;i< list.size();i++){
Student s = list.get(i);
System.out.println(s.getName()+","+s.getAge());
}
System.out.println("--------");
for(Student s:list){
System.out.println(s.getName()+","+s.getAge());
}
ArrayList集合
ArrayList是以数组为底层的集合,增删慢,查找快
Linked是以链表为底层的集合,增删快,查找慢
集合概述
删除指定元素(返回布尔值)
ArrayList<String> array=new ArrayList<>();
array.add("hello");
array.add("world");
array.add("java");
System.out.println(array.remove("world"));
删除指定索引处元素
ArrayList<String> array=new ArrayList<>();
array.add("hello");
array.add("world");
array.add("java");
System.out.println(array.remove(0));
修改指定索引处元素
ArrayList<String> array=new ArrayList<>();
array.add("hello");
array.add("world");
array.add("java");
System.out.println(array.set(1,"javaee"));
输出指定索引处元素
ArrayList<String> array=new ArrayList<>();
array.add("hello");
array.add("world");
array.add("java");
System.out.println(array.get(1));
返回集合中元素个数
ArrayList<String> array=new ArrayList<>();
array.add("hello");
array.add("world");
array.add("java");
System.out.println(array.size());
集合遍历
三种遍历方式(结合学生对象案例) 其中增强for循环最为常用
ArrayList<Student> array=new ArrayList<Student>();
Student s1=new Student("Betty","30");
Student s2=new Student("Btty","30");
Student s3=new Student("Bety","30");
array.add(s1);
array.add(s2);
array.add(s3);
Iterator<Student> it=array.iterator();
while(it.hasNext()){
Student s = it.next();
System.out.println(s);
}
System.out.println("--------");
for (int i=0;i< array.size();i++){
Student s = array.get(i);
System.out.println(s.getName()+","+s.getAge());
}
System.out.println("--------");
for (Student s:array){
System.out.println(s.getName()+","+s.getAge());
}
LinkedList集合
集合概述
列表开头结尾添加元素
LinkedList<String> linkedList=new LinkedList<String>();
linkedList.add("hello");
linkedList.add("world");
linkedList.add("java");
linkedList.addFirst("javaee");
linkedList.addLast("hi");
列表开头结尾获取元素
LinkedList<String> linkedList=new LinkedList<String>();
linkedList.add("hello");
linkedList.add("world");
linkedList.add("java");
System.out.println(linkedList.getFirst());
System.out.println(linkedList.getLast());
列表开头结尾去除元素
LinkedList<String> linkedList=new LinkedList<String>();
linkedList.add("hello");
linkedList.add("world");
linkedList.add("java");
System.out.println(linkedList.removeFirst());
System.out.println(linkedList.removeLast());
Set集合
集合概述
1.无序
2.不能用for循环遍历
3.没有重复元素
Set<String> set=new HashSet<String>();
set.add("hello");
set.add("world");
set.add("java");
for(String s:set){
System.out.println(s);
}
HashSet集合
集合概述及遍历
1.底层结构为哈希表
2.对顺序无保证
3.没有带索引方法,不可以用普通for
4.由于是Set集合,无重复元素
HashSet<String> hs=new HashSet<String>();
hs.add("hello");
hs.add("world");
hs.add("java");
for(String s:hs){
System.out.println(s);
}
TreeSet集合
集合概述及遍历
1.元素有序,按照方法来
2.没有带索引方法,不可以用普通for
TreeSet():按照元素的自然排序进行排序
TreeSet(Comparator comparator):按照指定的比较器进行排序
3.由于是Set集合,无重复元素
TreeSet<Integer> ts=new TreeSet<Integer>();
ts.add(10);
ts.add(40);
ts.add(30);
ts.add(50);
ts.add(20);
for(Integer i:ts){
System.out.println(i);
}
Map双列
Map集合被叫做双列集合,假设集合中3个元素1,2,3,在单个元素,比如在元素1中,存在键,值这样的组合。也就是元素1–a(键),b(值),控制台可以通过访问键来调取特定的值
Map集合
集合概述
根据键删除指定元素(返回值)
Map<String,String> map=new HashMap<String, String>();
map.put("张无忌","赵敏");
map.put("郭靖","杨蓉");
map.put("杨过","小龙女");
System.out.println(map.remove("张无忌"));
判断集合是否存在指定的键(返回布尔值)
Map<String,String> map=new HashMap<String, String>();
map.put("张无忌","赵敏");
map.put("郭靖","杨蓉");
map.put("杨过","小龙女");
System.out.println(map.containsKey("郭靖"));
清空集合
map.clear();
判断是否为空
boolean isEmpty();
集合遍历
1.根据键获取值 2.获取所有键的集合 3.获取所有值的集合
public static void main(String[] args) {
Map<String,String> map=new HashMap<String, String>();
map.put("张无忌","赵敏");
map.put("郭靖","杨蓉");
map.put("杨过","小龙女");
System.out.println(map.get("张无忌"));
Set<String> keySet=map.keySet();
for(String key:keySet){
System.out.println(key);
}
Collection<String> values=map.values();
for(String value:values){
System.out.println(value);
}
}
HashedMap
集合遍历
public static void main(String[] args) {
HashMap<String,Student> hm=new HashMap<String,Student>();
Student s1= new Student("Betty",20);
Student s2= new Student("Peter",21);
Student s3= new Student("Jimmy",22);
hm.put("001",s1);
hm.put("002",s2);
hm.put("003",s3);
Set<String> keySet=hm.keySet();
for(String key:keySet){
Student value=hm.get(key);
System.out.println(key+","+value.getName()+","+value.getAge());
}
System.out.println("---------");
Set<Map.Entry<String, Student>> entrySet = hm.entrySet();
for (Map.Entry<String, Student> me:entrySet){
String key = me.getKey();
Student value = me.getValue();
System.out.println(key+","+value.getName()+","+value.getAge());
} }
HashedMap集合不重复
首先要重写Student(学生)内部的equals(),HashedCode()方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
之后在主程序中写
public static void main(String[] args) {
HashMap<Student,String> hm=new HashMap<Student,String>();
Student s1= new Student("Betty",20);
Student s2= new Student("Peter",21);
Student s3= new Student("Jimmy",22);
hm.put(s1,"西安");
hm.put(s2,"武汉");
hm.put(s3,"郑州");
Set<Student> keySet=hm.keySet();
for (Student key:keySet){
String value = hm.get(key);
System.out.println(key.getName()+","+key.getAge()+","+value);
}
}
复合集合
ArrayList嵌套HashMap
public static void main(String[] args) {
ArrayList<HashMap<String,String>> array=new ArrayList<HashMap<String,String>>();
HashMap<String,String> hm1=new HashMap<String,String>();
hm1.put("孙策","大桥");
hm1.put("周瑜","小乔");
array.add(hm1);
HashMap<String,String> hm2=new HashMap<String,String>();
hm2.put("22","33");
hm2.put("44","55");
array.add(hm2);
HashMap<String,String> hm3=new HashMap<String,String>();
hm3.put("66","77");
hm3.put("88","99");
array.add(hm3);
for(HashMap<String,String> hm:array){
Set<String> keySet=hm.keySet();
for(String key: keySet){
String value=hm.get(key);
System.out.println(key+","+value);
}
}
}
HashedMap嵌套ArrayList
public static void main(String[] args) {
HashMap<String, ArrayList<String>> hm=new HashMap<String, ArrayList<String>>();
ArrayList<String> sgyy=new ArrayList<String>();
sgyy.add("赵云");
sgyy.add("诸葛亮");
hm.put("三国演义",sgyy);
ArrayList<String> xyj=new ArrayList<String>();
xyj.add("唐僧");
xyj.add("孙悟空");
hm.put("西游记",xyj);
ArrayList<String> shz=new ArrayList<String>();
shz.add("武松");
shz.add("鲁智深");
hm.put("水浒传",shz);
Set<String> keySet=hm.keySet();
for(String key:keySet){
System.out.println(key);
ArrayList<String> value=hm.get(key);
for(String s:value){
System.out.println("\t"+s);
}
}
}
LinkedHashSet
集合特点 1.哈希值和链表实现set机头,具有可预测的迭代次序 2.由链表保证元素有序,元素储存和取出是一致的 3.由哈希值代表元素唯一,没有重复元素
public static void main(String[] args) {
LinkedHashSet<String> linkedHashSet=new LinkedHashSet<String>();
linkedHashSet.add("hello");
linkedHashSet.add("world");
linkedHashSet.add("java");
for(String s:linkedHashSet){
System.out.println(s);
}
|