【java】集合的主要实现类基本操作总结

主要的集合类:
  ArrayList
  LinKeList
  HashSet(HashMap的key,存储在HashMap集合的元素需要同时重写hashcode + equals)
  TreeSet
  HashMap
  TreeMap
  Properties

Collection集合的基本操作:

https://mp.csdn.net/mp_blog/creation/editor/124871954

List:

https://mp.csdn.net/mp_blog/creation/editor/124903479 

public class ArrayListTest {
    public static void main(String[] args) {
        ArrayList<String> al = new ArrayList<>();
        al.add("I");
        al.add("Just");
        al.add("HAHA");
        //通过下标取出元素
        System.out.println(al.get(0));//输出I
        //遍历(通过下标遍历)
        for(int i = 0;i < al.size();i++){
            String elt = al.get(i);
            System.out.println(elt);
        }
        //遍历(迭代器,所有的Collection集合通用)
        Iterator it = al.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        //增强for循环
        for(String s : al){
            System.out.println(s);
        }
    }
}

HashSet:

/**
 * Set集合:无序不可重复
 */
public class HashSetTest {
    public static void main(String[] args) {
        HashSet<String> set = new HashSet<>();
        set.add("haha");
        set.add("heihei");
        set.add("xixi");
        //set集合中的元素没有下标,无法通过下标取出,若想取出可将Set转换成List
        //遍历(迭代器)
        Iterator it = set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        //遍历(增强for循环)
        for(String s : set){
            System.out.println(s);
        }

        Set<Student> students = new HashSet<>();
        students.add(new Student(1,"xiaoming"));
        students.add(new Student(3,"xiaohong"));
        students.add(new Student(2,"xiaohei"));
        students.add(new Student(2,"xiaohei"));
        System.out.println(students.size());//输出3,无序不可重复
        //遍历
        for(Student s : students){
            System.out.println(s);
        }
    }
}

class Student{
    int no;
    String name;
    public Student(int no, String name){
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return no == student.no && name.equals(student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(no, name);
    }
}

TreeSet:

 https://mp.csdn.net/mp_blog/creation/editor/124993295

**
 * Treeset:无序不可重复
 */
public class TreeSetTest {
    public static void main(String[] args) {
        //TreeSet泛型中写入String,Integer等类型时,遍历输出时默认为升序排序
        //若想修改排序规则,可在创建对象时传入比较器对象
        TreeSet<Integer> ts = new TreeSet<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;//降序
            }
        });
        ts.add(123);
        ts.add(3211);
        ts.add(21);
        ts.add(22);
        //遍历
        for(Integer i : ts){
            System.out.println(i);
        }

        //测试自定义类
        TreeSet<A> treeset = new TreeSet<>();
        treeset.add(new A(12,"xi"));
        treeset.add(new A(11,"sdw"));
        treeset.add(new A(2,"dsw"));
        System.out.println(treeset.size());//输出3
        for(A a : treeset){
            System.out.println(a);
        }

    }
}
class A implements Comparable<A>{
    int i;
    String name;
    public A(int i,String name){
        this.i = i;
        this.name = name;
    }

    @Override
    public String toString() {
        return "A{" +
                "i=" + i +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        A a = (A) o;
        return i == a.i && name.equals(a.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(i, name);
    }

    @Override
    public int compareTo(A o) {
        return this.i - o.i;//升序
    }
}

 HashMap:

https://mp.csdn.net/mp_blog/creation/editor/124936409

public class HashMapTest {
    public static void main(String[] args) {
        Map<Integer,String> map = new HashMap<>();
        map.put(1,"xiaoming");
        map.put(2,"xiaoh");
        map.put(3,"xiaobai");
        map.put(3,"lihua");//key与上面那个相同,lihua覆盖xiaobai
        //通过key取出value
        System.out.println(map.get(3));//输出 lihua
        //遍历
        //第一种方式(效率较低),通过获取全部key,然后遍历出value
        Set<Integer> set = map.keySet();
        for(Integer key : set){
            System.out.println(key +"="+ map.get(key));
        }
        //第二种方式,将Map集合转换为Set集合
        Set<Map.Entry<Integer,String>> nodes = map.entrySet();
        for(Map.Entry<Integer,String> nood : nodes){
            Integer keys = nood.getKey();
            String values = nood.getValue();
            System.out.println(keys +"="+ values);
        }

    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值