面试题

HashMap排序

题目

已知一个HashMap<Integer,User>集合, User有name(String)和age(int)属性。请写一个方法实现对HashMap的排序功能,该方法接收HashMap<Integer,User>为形参,返回类型为HashMap<Integer,User>,要求对HashMap中的User的age正序进行排序。排序时key=value键值对不得拆散。


HashMap是不能保证存取顺序一致的,但是其子类LinkedHashMap可以保证,利用多态实现。

我的代码如下

User类

package com.pojo;

public class User {
    private String name;
    private int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = 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 String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

业务代码

package com.collection.list;

import com.pojo.User;

import java.util.*;

public class ListDemo {
    public static void main(String[] args) {
        HashMap<Integer, User> hashMap = new HashMap<Integer, User>() {
            {
                put(1, new User("小明", 12));
                put(2, new User("小张", 16));
                put(3, new User("小赵", 11));
                put(4, new User("小黄", 10));
                put(5, new User("小王", 10));
                put(6, new User("小胡", 11));
                put(7, new User("小梦", 12));
            }
        };


        hashMap = sortMap(hashMap);
        System.out.println(hashMap);
    }

    private static HashMap<Integer, User> sortMap(HashMap<Integer, User> hashMap) {


        TreeSet<Map.Entry<Integer, User>> entries1 = new TreeSet<Map.Entry<Integer, User>>(new Comparator<Map.Entry<Integer, User>>() {
            @Override
            public int compare(Map.Entry<Integer, User> o1, Map.Entry<Integer, User> o2) {
                //一定要注意避免数据丢失,假如写成return o1.getValue().getAge() > o2.getValue().getAge()就会造成数据丢失
                return o2.getValue().getAge() - o1.getValue().getAge() > 0 ? -1 : 1;
            }
        });
        entries1.addAll(hashMap.entrySet());

        LinkedHashMap linkedHashMap = new LinkedHashMap<Integer, User>();
        for (Map.Entry<Integer, User> integerUserEntry : entries1) {
            linkedHashMap.put(integerUserEntry.getKey(), integerUserEntry.getValue());
        }

        return linkedHashMap;
    }
}

 用面向对象的方法求出数组中重复 value 的个数

题目

int[] arr = {1,4,1,4,2,5,4,5,8,7,8,77,88,5,4,9,6,2,4,1,5};
1 出现: 1
3 出现: 2
8 出现: 3
2 出现: 4
我的解答
package com.array;

import java.util.HashMap;
import java.util.Map;

/**
 * 用面向对象的方法求出数组中重复 value 的个数
 * int[] arr = {1,4,1,4,2,5,4,5,8,7,8,77,88,5,4,9,6,2,4,1,5};
 * 1 出现: 1 次
 * 3 出现: 2 次
 * 8 出现: 3 次
 * 2 出现: 4 次
 */

public class ArrayDemo {

    public static void main(String[] args) {
        int[] arr = {1,4,1,4,2,5,4,5,8,7,8,77,88,5,4,9,6,2,4,1,5};
        HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
        for (int i : arr) {
            if(hashMap.containsKey(i)){
                hashMap.put(i,hashMap.get(i)+1);
            }else {
                hashMap.put(i,1);
            }
        }

        for (Map.Entry<Integer, Integer> integerIntegerEntry : hashMap.entrySet()) {
            System.out.println(integerIntegerEntry.getKey()+"出现: "+integerIntegerEntry.getValue()+" 次");
        }
    }


}

工厂模式和单例模式的区别

工厂模式使用时,返回的对象之间没有关系。
单例模式返回始终度是同一个对象。单例模式的构造函数 一般是私有的,不允许实例化,通过内部静态方法实例化自己,对同类型的对象始终返回同一个。






 




 

posted @ 2020-04-11 20:15  手握钢叉的猹  阅读(213)  评论(0编辑  收藏  举报