java集合之ArrayList排序和HashMap遍历

ArrayList如何排序呢?

Collection.sort();

 如果是Integer类型的话 可以使用Collection.sort();

public class ArrayListTest {
    public static void main(String[] args) {
        List<Integer> strList = Arrays.asList(1,55,2,9,3,4,7);
        Collections.sort(strList);
         System.out.println(strList.toString());
    }
}

如何是对象的话,可以使用 Collection.sort(list,new  Comparator<Student>(){});

也可以直接使用 list.sort(new  Comparator<Student>(){});这种方式

public class Student {
    private int id;
    private String name;
    private int age;
    private int height;


    public Student(int id, String name, int age, int height) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.height = height;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

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

 

public class ArrayListTest {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<Student>();
        list.add(new Student(1, "A", 20, 180));
        list.add(new Student(2, "B", 21, 175));
        list.add(new Student(3, "C", 22, 190));
        list.add(new Student(4, "D", 21, 170));
        list.add(new Student(5, "E", 20, 185));
      
        list.sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                //先按照年龄排,再按照身高排
                if(o1.getAge() > o2.getAge()) {
                    return 1;
                }
                else if (o1.getAge() < o2.getAge()){
                    return -1;
                }
                else {
                    if (o1.getHeight() >= o2.getHeight()) {
                        return 1;
                    }
                    else {
                        return -1;
                    }
                }
            }
        });

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).toString());
        }
    }
}

 

HashMap如何遍历呢?

 

public class HashMapTest {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        for (int i = 0; i < 20; i++) {
            map.put(i,"值"+i);

        }
        //第一种 map.entrySet()
        for (Map.Entry<Integer,String> entry: map.entrySet()) {
            System.out.println("第一种 "+ entry.getKey());
            System.out.println("第一种 "+ entry.getValue());
        }
        //第二种  显示调用map.entrySet()的集合迭代器
        Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer, String> entry = iterator.next();
            System.out.println("第二种 "+ entry.getKey());
            System.out.println("第二种 "+ entry.getValue());
        }
        //第三种for each map.keySet(),再调用get获取
        for (Integer key: map.keySet()
             ) {
            System.out.println("第三种 "+ map.get(key));
        }
        //第四种  for each map.entrySet(),用临时变量保存map.entrySet()
        Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
        for (Map.Entry<Integer, String> entry: entrySet
        ) {
            System.out.println("第四种 "+ entry.getKey());
            System.out.println("第四种 "+ entry.getValue());
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值