栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【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 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:

public class HashSetTest {
    public static void main(String[] args) {
        HashSet 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 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 ts = new TreeSet<>(new Comparator() {
            @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 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{
    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 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 set = map.keySet();
        for(Integer key : set){
            System.out.println(key +"="+ map.get(key));
        }
        //第二种方式,将Map集合转换为Set集合
        Set> nodes = map.entrySet();
        for(Map.Entry nood : nodes){
            Integer keys = nood.getKey();
            String values = nood.getValue();
            System.out.println(keys +"="+ values);
        }

    }
}

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/936389.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号