java集合之ArrayList排序和HashMap遍历

ArrayList如何排序呢?

Collection.sort();

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

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

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

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

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 list = new ArrayList();
        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() {
            @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 map = new HashMap<>();
        for (int i = 0; i < 20; i++) {
            map.put(i,"值"+i);

        }
        //第一种 map.entrySet()
        for (Map.Entry entry: map.entrySet()) {
            System.out.println("第一种 "+ entry.getKey());
            System.out.println("第一种 "+ entry.getValue());
        }
        //第二种  显示调用map.entrySet()的集合迭代器
        Iterator> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry 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> entrySet = map.entrySet();
        for (Map.Entry entry: entrySet
        ) {
            System.out.println("第四种 "+ entry.getKey());
            System.out.println("第四种 "+ entry.getValue());
        }
    }
}

 

你可能感兴趣的:(java)