- package com.test01;
-
- import java.util.ArrayList;
- import java.util.Collection;
- /*
- 添加元素---boolean add(E e);
- 移除元素---boolean remove(Object c);
- 判断元素是否存在---boolean contains(Object c);
- */
- public class S {
- public static void main(String[] args) {
- // 创建Collectiom的对象
- Collection
s = new ArrayList<>(); -
- // 添加元素---boolean add(E e);
- s.add("Hello");
- s.add("world");
-
- // 重写toString()方法
- System.out.println(s);
-
- // 移除元素---boolean remove(Object c);
- s.remove("Hello");
- System.out.println(s);
-
- // 判断元素是否存在---boolean contains(Object c);
- System.out.println(s.contains("world"));
-
- // 判断集合是否为空 boolean isEmpty();
- System.out.println(s.isEmpty());
-
- // 返回长度---int size();
- System.out.println(s.size());
-
- // 清空元素---void claer();
- s.clear();
- }
- }
- package com.test01;
-
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Iterator;
-
- public class S {
- public static void main(String[] args) {
- // 创建Collection的对象
- Collection
s = new ArrayList<>(); -
- s.add("1");
- s.add("2");
- s.add("3");
- // 返回集合中元素的迭代器,通过集合中iterator()方法得到
- Iterator
it = s.iterator(); -
- // 通用遍历方法
- // boolean hasNext()----如果集合中有更多的元素,则返回true
- // E next()----返回迭代器中的下一个元素
- while(it.hasNext()){
- String a = it.next();
- System.out.println(a);
- }
- }
- }
遍历与Collection类似//可能会出现并发修改异常
多了一种遍历方法
快捷键为list.fori,回车
- package com.test01;
-
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Iterator;
- import java.util.List;
-
- public class S {
- public static void main(String[] args) {
- List
list = new ArrayList<>(); - list.add("1");
- list.add("2");
- list.add("3");
-
- // void add(int idnex,E c)----在指定索引添加元素
- list.add(1,"100");
-
- // 也是重写了toString()方法
- System.out.println(list);
-
- // E remove(int x)-----删除指定元素,并返回此元素
- System.out.println(list.remove(1));
- System.out.println(list);
-
- // E set(int x,E a)----修改指定元素,并返回此元素
- System.out.println(list.set(1,"200"));
- System.out.println(list);
-
- // E get(int x)----得到指定元素
- System.out.println(list.get(2));
- for (int i = 0; i < list.size(); i++) {
- System.out.println(list.get(i));
- }
- }
- }
- package com.test01;
-
- import java.util.*;
-
- public class S {
- public static void main(String[] args) {
- List
list = new ArrayList<>(); - list.add("1");
- list.add("2");
- list.add("3");
- ListIterator
it = list.listIterator(); -
- // //正向,不常用
- // while(it.hasNext()){
- // System.out.println(it.next());
- // }
- // System.out.println("----------");
- //
- // //反向,不常用
- // while(it.hasPrevious()){
- // System.out.println(it.previous());
- // }
- //
- // System.out.println("--------");
- //add()方法,重点掌握
- while(it.hasNext()){
- String s = it.next();
- if(s.equals("2")){
- it.add("10");
- }
-
- }
- System.out.println(list);
- }
- }
- public class S {
- public static void main(String[] args) {
- List
list = new ArrayList<>(); - list.add("1");
- list.add("2");
- list.add("4");
- for(String i : list){
- System.out.println(i);
- }
- }
- }
ArrayList----底层是数组
LinkedList---底层是链表
- public class dome {
- public static void main(String[] args) {
- LinkedList
linkedList = new LinkedList<>(); -
- //boolean addFirst(E e)在头加入元素,removeFirst()同理,getFirst()
- linkedList.addFirst("1");
- //在尾加入元素,removeLast()同理,getLast()
- linkedList.addLast("3");
-
- for(String it : linkedList){
- System.out.println(it);
- }
-
- }
- }
需要重写hascode()
底层是哈希表
3种遍历不能用普通循环
不保证存储和输出顺序一致
- /*
- 存储和输出顺序一致
- 保证唯一性
- */
- public class S {
- public static void main(String[] args) {
- LinkedHashSet
linkedHashSet = new LinkedHashSet<>(); - linkedHashSet.add("1");
- linkedHashSet.add("2");
- linkedHashSet.add("3");
- linkedHashSet.add("3");
- for(String it : linkedHashSet){
- System.out.println(it);
- }
- }
- }
- /*
- 元素有序,这里的有序是按照一定的规则进行排序,取决于构造方法,默认根据自然元素进行排序
- 不能用普通for
- 元素具有唯一性
- */
- public class S {
- public static void main(String[] args) {
- TreeSet
integers = new TreeSet<>(); - integers.add(1);
- integers.add(2);
- integers.add(2);
- integers.add(4);
- for(Integer it : integers){
- System.out.println(it);
- }
- }
- }
- public class Student implements Comparable
{ - private String name;
- private int age;
- public Student(){
-
- }
- public Student(String name,int age){
- this.name=name;
- this.age=age;
- }
- public void show(){
- System.out.println(name+age+"岁");
- }
-
- 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;
- }
-
- @Override
- public int compareTo(Student o) {
- // return 0;不加入
- // return 1;加入
- // return -1;加前面
- int num = this.age-o.age;
- int num1 = num==0?this.name.compareTo(o.name):num;
- return num1;
- }
- }
- public class dome {
- public static void main(String[] args) {
- TreeSet
treeSet = new TreeSet<>(); - /* //可以用这个代替
- TreeSet
treeSet = new TreeSet<>(new Comparator() { - @Override
- public int compare(Student o1, Student o2) {
- int num = o1.getAge()-o2.getAge();
- int num1 = num==0?o1.getName().compareTo(o2.getName()):num;
- return num1;
- }
- });
- */
- Student s1 = new Student("lihua",10);
- Student s2 = new Student("yaoming",30);
- Student s3 = new Student("hh",20);
- Student s4 = new Student("hh",20);
- treeSet.add(s1);
- treeSet.add(s2);
- treeSet.add(s3);
- treeSet.add(s4);
- for(Student it : treeSet){
- System.out.println(it.getName()+","+it.getAge());
- }
- }
- }
- public class dome {
- public static void main(String[] args) {
- //创建对象
- Map
map = new HashMap<>(); - //添加功能
- map.put("1","wa");//添加元素
- map.put("2","rrrr");
- map.put("3","weewee");
- map.put("2","qqqq");//注意
-
- //获取功能
- System.out.println(map.get("1"));//获取值
- Set
set = map.keySet();//获取所有键 - Collection
collection = map.values();//获取所有值 -
- System.out.println(map);
-
- //基本方法
- System.out.println("-------");
- System.out.println(map.remove("2"));//返回值,删除
- System.out.println(map.remove("6"));//返回为空
- System.out.println(map);
- System.out.println("-------------");
-
- System.out.println(map.containsKey("3"));//是否包含此key
- System.out.println(map.containsValue("wa"));//是否包含vaul
-
- System.out.println(map.isEmpty());//判断是否为空
-
- System.out.println(map.size());//返回长度
- map.clear();//清空map
-
- }
- }
1.
- public class dome {
- public static void main(String[] args) {
- Map
map = new HashMap<>(); - map.put("1","a");
- map.put("2","b");
- map.put("3","c");
-
- Set
set = map.keySet(); -
- for(String it : set){
- System.out.println(it+","+map.get(it));
- }
- }
- }
2.
- Set
> entries = map.entrySet(); - for(Map.Entry
ma : entries){ - System.out.println(ma.getKey()+","+ma.getValue().getName()+","+ma.getValue().getAge());
- }
- public class dome {
- public static void main(String[] args) {
- ArrayList
> arrayList = new ArrayList<>(); - HashMap
map1 = new HashMap<>(); - HashMap
map2 = new HashMap<>(); - map1.put("1","2");
- map1.put("2","3");
- map2.put("a","b");
- map2.put("b","c");
- arrayList.add(map1);
- arrayList.add(map2);
- for(HashMap
it : arrayList){ - Set
set = it.keySet(); - for(String me : set){
- System.out.println(me+","+it.get(me));
- }
- }
- }
- }
- public class dome {
- public static void main(String[] args) {
- HashMap
> map = new HashMap<>(); - ArrayList
arrayList1 = new ArrayList(); - arrayList1.add("张三");
- arrayList1.add("李四");
- map.put("编号:1",arrayList1);
- ArrayList
arrayList2 = new ArrayList<>(); - arrayList2.add("王五");
- arrayList2.add("赵六");
- map.put("编号:2",arrayList2);
- Set
>> entries = map.entrySet(); - for(Map.Entry
> me : entries){ - System.out.println(me.getKey());
- ArrayList
ss = me.getValue(); - for(String it : ss){
- System.out.println(it);
- }
- }
- }
- }
是TreeSet与HashMap结合
- public class dome {
- public static void main(String[] args) {
- List
list = new ArrayList<>(); - list.add(12);
- list.add(34);
- list.add(23);
- list.add(78);
-
- //排序
- Collections.sort(list);
- System.out.println(list);
-
- //反转
- Collections.reverse(list);
- System.out.println(list);
-
- //随机排序
- Collections.shuffle(list);
- System.out.println(list);
- }
- }
- public class dome {
- public static void main(String[] args) {
- ArrayList
arrayList = new ArrayList<>(); - Student s1 = new Student("zhangsan",12);
- Student s2 = new Student("lisi",12);
- Student s3 = new Student("lihua",234);
- arrayList.add(s1);
- arrayList.add(s2);
- arrayList.add(s3);
- Collections.sort(arrayList, new Comparator
() {//也可用Student重写方法 - @Override
- public int compare(Student o1, Student o2) {
- int num = o1.getAge()-o2.getAge();
- int num1 = num==0?o1.getName().compareTo(o2.getName()):num;
- return num1;
- }
- });
- for(Student it : arrayList){
- System.out.println(it.getName()+","+it.getAge());
- }
- }
- }