Java基础-集合

集合

集合选择:
在这里插入图片描述
collection接口
在这里插入图片描述
Map
在这里插入图片描述

List

/**list接口
 * 1.有序,添加取出顺序相同
 * 2.可以重复添加
 */
List list=new ArrayList();
List list=new LinkedList();
List list=new Vector();

list.add("jack"); 
list.add(index,"marry"); //指定插入

list.indexOf("jack"); //首次出现位置
list.lastIndexOf("jack"); //最后一次出现位置

list.set(1,"tom"); //修改

list.get(index); //获取

list.subList(0,2); //返回[0,2)的元素

list.remove(0); //删除第一个
list.remove("jack"); //指定删除

list.contains("jack"); //查找是否存在

list.size(); //元素个数

list.isEmpty(); //判断是否为空

list.clear(); //清空

ArrayList list2=new ArrayList();
list2.add("marry");
list.addAll(list2); //添加集合

list.contains(list2); //判断多个存在

list.removeAll(list2); //删除集合


//迭代器
Iterator it=list.iterator(); //生成迭代器
while(it.hasNext()){ //itit+enter生成
    Object obj=it.next();
    System.out.println(obj);
}
it=list.iterator();//重置迭代器,第二次迭代

//增强for:集合和数组,简化版迭代器
for(Object o:list){ //I+enter快速生成
    System.out.println(o);
}


//list的排序
List list=new LinkedList();
list.add(new Book("西游记",10));
list.add(new Book("三国演义",20));

public static void sort(List list){
    int size=list.size();
    for(int i=0;i<size-1;i++){
        for(int j=0;j<size-1-i;j++){
            //取出对象,向下转型
            Book book1=(Book)list.get(j);
            Book book2=(Book)list.get(j+1);
            if(book1.getprice()>book2.getprice()){
                //交换
                list.set(j,book2);
                list.set(j+1,book1);
            }
        }
    }
}

ArrayList

//底层是数组存储
//保存在transient Object[]elementData //不会被序列化
ArrayList() //空数组从10开始扩容1.5倍
ArrayList(size)  //从size开始扩容1.5倍

Vector

在这里插入图片描述

LinkedList

在这里插入图片描述
模拟linked list

//结点定义
class Node{
	public Object item; //数据域
	public Node pre;
	public Node next;
	public Node(Object val){
		this.item=val;
	}
}
Node n1=new Node("n1");
Node n2=new Node("n2");
Node n3=new Node("n3");

n1.next=n2;
n2.next=n3;

n3.pre=n2;
n2.pre=n1;
//用于遍历输出
Node first=n1;
Node last=n3;

//双向输出
while(first!=null){
	System.out.println(first);
	first=first.next;
}
while(last!=null){
	System.out.println(last);
	last=last.pre;
}

//1,2中插入
Node n4=new Node("n4");
n4.next=n2;
n4.pre=n1;

n2.pre=n4;
n1.next=n4;

//双向输出
first=n1;
while(first!=null){
	System.out.println(first);
	first=first.next;
}
last=n3;
while(last!=null){
	System.out.println(last);
	last=last.pre;
}

在这里插入图片描述

Set

HashSet

//添加
//不保证存放和取出顺序一致
Set set =new HashSet();
set.add();

//遍历
Iterator it=set.iterator(); //生成迭代器
while(it.hasNext()){ //itit+enter生成
    Object obj=it.next();
    System.out.println(obj);
}
for(Object o:set){
	 System.out.println(o);
}
//不能存放相同的元素或对象

//以下也是不同对象
set.add(new Dog("tom"));
set.add(new Dog("tom"));

//以下是相同对象
set.add(new String("fwj"));
set.add(new String("fwj"));

//HashSet底层是HashMap,HashMap底层是数组+链表+红黑树
Node[]table=new Node(size);

Node n1=new Node("n1",null);
table[0]=n1;
Node n2=new Node("n2",null);
n1.next=n2; //形成链表,挂载到n1上

class Node{
	Object item;
	Node next;
	public Node(Object item,Node next){
		this.item=item;
		this.next=next;
	}
}

HashSet源码
在这里插入图片描述
扩容机制
在这里插入图片描述

LinkedHashSet

底层是数组+双向链表,LinkedHashMap
顺序插入,顺序取出
在这里插入图片描述

TreeSet

有序添加:

TreeSet treeSet=new TreeSet(new Comparator(){
	public int compare(Object o1,Object o2){
		return ((String)o1).compareTo((String)o2));
	}
});
    treeSet.add(a);
    treeSet.add(b);

去重
在这里插入图片描述

Map

HashMap

在这里插入图片描述
k-v键值对保存在HashMap$Node中,将地址
赋给entrySet来遍历

在这里插入图片描述
遍历
1.keySet()+get(key)
在这里插入图片描述
2.values()
在这里插入图片描述
3.entrySet()+getKey()+getValue()
在这里插入图片描述map方法

    Map map=new HashMap();
    map.put(a,b);//添加或者更改
    map.remove(a);
    map.get(a);//对应的值
    map.size();
    map.isEmpty();
    map.clear();
    map.containsKey(a);

在这里插入图片描述

//for循环遍历
Set keyset=map.keySet();
for(Object key:keyset){
    worker workers=(worker)map.get(key); //取出worker对象
    if(workers.getsal()>18000) //get方法获取对象属性
        System.out.println(workers);
    }

在这里插入图片描述
在这里插入图片描述

Hashtable

线程安全,效率较低
键值均不能为null
扩容机制:
在这里插入图片描述

TreeMap

TreeMap treeMap=new TreeMap(new Comparator(){
	public int compare(Object o1,Object o2){
		//字典序
		return ((String)o1).compareTo((String)o2);
		//长度排序
		return (String)o1.length-(String)o2.length;
	}
});

Collection

Collections.reverse(list);

Collections.sort(list);
Collections.sort(list,new Comparator(){
	public int compare(Object o1,Object o2){
		return ((String)o1).length-((String)o2).length;
	}
});

Collections.shuffle(list);//随机打乱

Collections.swap(list,index1,index2);

Object max=Collections.max(list);//集合最大值
Object max=Collections.max(list,new Comparator(){
	public int compare(Object o1,Object o2){
		return ((String)o1).length-((String)o2).length;
	}
});

Collections.frequency(list,"tom");//频率

Collections.copy(list1,list2);//list2拷贝到list1

Collections.replaceAll(list,"a","b");//将a替换为b
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值