List
ArrayList:
1.List接口的实现类
2.底层的结构是数组,内存空间是连续的
3.元素有下标,有序,允许存放重复的数据
4.通常进行的是根据下标进行操作
5.查询快,增删慢,适合查询较多的场景【数据量比较大时】
LinkedList:
1.List接口的实现类
2.底层的结构是链表,内存空间是不连续的
3.元素有下标,有序,允许存放重复的数据
4.通常进行的是首尾节点相关的操作
5.查询慢,增删快,适合增删较多的场景【数据量比较大时】
注意:LinkedList查首尾操作还是挺快的
/*本类用于LinkedList的相关测试*/
public class TestLinkedList {
public static void main(String[] args) {
//1.创建集合对象
LinkedList<String> list=new LinkedList<>();
//2.添加数据
list.add("孙悟空");
list.add("猪八戒");
list.add("唐三藏");
list.add("沙师弟");
list.add("白龙马");
System.out.println(list);
System.out.println(list.contains("孙悟空"));
//3.1自行测试继承的方法
//3.2测试LinkedList独有方法
list.addFirst("黄毛怪");//添加首元素
list.addLast("玉兔精");//添加尾元素
System.out.println(list);
System.out.println(list.getFirst());//获取首元素
System.out.println(list.getLast());//获取尾元素
list.removeFirst();//移除首元素
System.out.println(list);
list.removeLast();//删除尾元素
System.out.println(list);
//4.其他测试
LinkedList<String> list2=new LinkedList<>();
list2.add("水浒传");
list2.add("三国演义");
list2.add("西游记");
list2.add("红楼梦");
System.out.println(list2);
System.out.println(list2.element());//查看首元素
/*查询系列*/
System.out.println(list2.peek());//查看首元素
System.out.println(list2.peekFirst());//查看首元素
System.out.println(list2.peekLast());//查看尾元素
System.out.println(list2);
/*别名:新增系列*/
System.out.println(list2.offer("遮天"));//添加尾元素
System.out.println(list2.offerFirst("斗破苍穹"));//添加首元素
System.out.println(list2.offerLast("钢铁是怎样炼成的"));//添加尾元素
System.out.println(list2);
/*别名:移除系列*/
System.out.println(list2.poll());//删除首元素
System.out.println(list2.pollFirst());//删除首元素
System.out.println(list2.pollLast());//删除尾元素
System.out.println(list2);
}
}
Map
map中的数据以键值对的形式存储
1.map中存放的都是无序的数据
2.map中的value可以重复--比如我们可以存入两个白骨精
3.map中的key不允许重复,如果重复,新的value会把旧value覆盖
map中没有迭代方式,他的迭代一般就是借助set的迭代
map集合的迭代方式一:
/*我们想要遍历map中的数据,但是map集合本身没有自己的迭代器
所有需要先将map集合转为Set集合以后,在使用set的迭代器进行迭代
代码:Set<Key>=map.keySet();
作用:将map中所有的key值取出,存入set集合中,此处set的泛型是Integer
*/
Set<Integer> set = map.keySet();
Iterator<Integer> it = set.iterator();
while (it.hasNext()){
// System.out.println(it.next());
Integer key = it.next();
String value = map.get(key);
System.out.println("("+key+"-"+value+")");
}
map集合的迭代方式二:
/*遍历map集合,需要先把map集合转换为set集合
* 本方案是把一对键值对看成是一个Entry
* 代码:Map.Entry<Key,value>=map.entrySet();
* Map.Entry<K,V>,这里是Map.Entry<Integer,String>*/
Set<Map.Entry<Integer, String>> set2 = map.entrySet();
Iterator<Map.Entry<Integer, String>> it2 = set2.iterator();
while (it2.hasNext()){
// System.out.println(it2.next());
Map.Entry<Integer, String> entry = it2.next();
Integer i=entry.getKey();
String s=entry.getValue();
System.out.println("*"+i+"*"+s);
}
Set
1.set集合中的元素都是没有顺序的
2.set集合中的元素不能重复
3.set集合可以存null值,但是最多只有一个
(set的方法都是继承自Collection的方法)
注意:如果HashSet中的“<>”/泛型为自定义的对象。例:HashSet<Student> set = new HashSet<>();
若想实现数据的去重,需重写equals和hashCode方法
/*本类用于测试Set接口*/
public class TestSet {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("紫霞仙子");
set.add("至尊宝");
set.add("牛魔王");
set.add("紫霞仙子");
set.add(null);
System.out.println(set);
/*1.set集合中的元素都是没有顺序的
2.set集合中的元素不能重复
3.set集合可以存null值,但是最多只有一个*/
System.out.println(set.contains("紫霞仙子"));//true,判断set集合中是否有该元素
System.out.println(set.equals("紫霞仙子"));
System.out.println(set.isEmpty());
System.out.println(set.remove(null));
System.out.println(set);
System.out.println(set.size());
System.out.println(set);
System.out.println(Arrays.toString(set.toArray()));
//4.1创建set2集合,并向集合中存入数据
Set<String> set2 = new HashSet<>();
set2.add("小兔纸");
set2.add("小脑斧");
set2.add("小海疼");
set2.add("小牛犊");
System.out.println(set.addAll(set2));
System.out.println(set);
System.out.println(set.containsAll(set2));
System.out.println(set.removeAll(set2));
System.out.println(set);
System.out.println(set.retainAll(set2));
System.out.println(set);
Iterator<String> it = set2.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
}
}
总结
list一般与数组下标的操作相关
set一般用于去重操作
map一般用于带有映射关系的数组