hbw-utils - ColUtils

132 阅读1分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

ColUtils功能:

  1. 封装各种集合的创建,指定默认初始长度
  2. 封装各种Map的创建,指定默认初始长度
  3. 封装LinkedBlockingQueue的创建,也可指定长度

github.com/houbbbbb/ut…

public class ColUtilsTest {

    @Test
    public void hashMapTest() {
        Map<String, Integer> map = ColUtils.newHashMap(String.class, Integer.class);
        map.put("a", 1);
        map.put("b", 2);
        System.out.println(map);
        System.out.println(map.getClass());
    }

    @Test
    public void concurrentHashMapTest() {
        Map<String, Integer> map = ColUtils.newConcurrentHashMap(String.class, Integer.class);
        map.put("a", 1);
        map.put("b", 2);
        System.out.println(map);
        System.out.println(map.getClass());
    }

    @Test
    public void propertiesTest() {
        Properties properties = ColUtils.newProperties();
        properties.put("a", "b");
        properties.put("c", "d");
        System.out.println(properties);
        System.out.println(properties.getClass());
    }

    @Test
    public void hashTableTest() {
        Hashtable<String, Integer> hashtable = ColUtils.newHashTable(String.class, Integer.class);
        hashtable.put("a", 1);
        hashtable.put("b", 2);
        System.out.println(hashtable);
        System.out.println(hashtable.getClass());
    }

    @Test
    public void treeMapTest() {
        Map<Integer, String> map = ColUtils.newTreeMap(Integer.class, String.class);
        map.put(2, "a");
        map.put(1, "b");
        System.out.println(map);
        System.out.println(map.getClass());

        Map<Count, String> map2 = ColUtils.newTreeMap(Count.class, String.class, new Comparator<Count>() {
            @Override
            public int compare(Count o1, Count o2) {
                return o1.getAge().compareTo(o2.getAge());
            }
        });
        map2.put(new Count("a", 5), "a");
        map2.put(new Count("b", 6), "b");
        map2.put(new Count("c", 4), "c");
        System.out.println(map2);
        System.out.println(map2.getClass());
    }

    static class Count {
        public Count() {
        }

        public Count(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        private String name;
        private Integer age;

        public String getName() {
            return name;
        }

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

        public Integer getAge() {
            return age;
        }

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

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

    @Test
    public void arrayListTest() {
        List<String> ls = ColUtils.newArrayList(String.class);
        ls.add("a");
        ls.add("b");
        System.out.println(ls);
        System.out.println(ls.getClass());

        ls = ColUtils.newArrayList(String.class, "c", "d");
        System.out.println(ls);
        System.out.println(ls.getClass());

        ls = ColUtils.newArrayList(String.class, ls);
        System.out.println(ls);
        System.out.println(ls.getClass());
    }

    @Test
    public void linkedListTest() {
        List<String> ls = ColUtils.newLinkedList(String.class);
        ls.add("a");
        ls.add("b");
        System.out.println(ls);
        System.out.println(ls.getClass());

        ls = ColUtils.newLinkedList(String.class, "c", "d", "e");
        System.out.println(ls);
        System.out.println(ls.getClass());

        ls = ColUtils.newLinkedList(String.class, ls);
        System.out.println(ls);
        System.out.println(ls.getClass());
    }

    @Test
    public void vectorTest() {
        List<String> ls = ColUtils.newVector(String.class);
        ls.add("ab");
        ls.add("cd");
        System.out.println(ls);
        System.out.println(ls.getClass());

        ls = ColUtils.newVector(String.class, "k", "l", "m");
        System.out.println(ls);
        System.out.println(ls.getClass());

        ls = ColUtils.newVector(String.class, ls);
        System.out.println(ls);
        System.out.println(ls.getClass());
    }

    @Test
    public void hashSetTest() {
        Set<String> set = ColUtils.newHashSet(String.class);
        set.add("abc");
        set.add("def");
        System.out.println(set);
        System.out.println(set.getClass());

        set = ColUtils.newHashSet(String.class, "a", "c", "b", "a", "c");
        System.out.println(set);
        System.out.println(set.getClass());

        set = ColUtils.newHashSet(String.class, set);
        System.out.println(set);
        System.out.println(set.getClass());
    }

    @Test
    public void treeSetTest() {
        Set<String> set = ColUtils.newTreeSet(String.class);
        set.add("a");
        set.add("b");
        System.out.println(set);
        System.out.println(set.getClass());

        set = ColUtils.newTreeSet(String.class, "b", "a");
        System.out.println(set);
        System.out.println(set.getClass());

        set = ColUtils.newTreeSet(String.class, set);
        System.out.println(set);
        System.out.println(set.getClass());

        Set<Count> set1 = ColUtils.newTreeSet(Count.class, new Comparator<Count>() {
            @Override
            public int compare(Count o1, Count o2) {
                return o1.getAge().compareTo(o2.getAge());
            }
        });
        set1.add(new Count("name", 3));
        set1.add(new Count("b", 5));
        set1.add(new Count("c", 4));
        System.out.println(set1);
        System.out.println(set1.getClass());

        set1 = ColUtils.newTreeSet(Count.class, new Comparator<Count>() {
            @Override
            public int compare(Count o1, Count o2) {
                return o1.getAge().compareTo(o2.getAge());
            }
        }, new Count("d", 5), new Count("u", 3), new Count("e", 7));
        System.out.println(set1);
        System.out.println(set1.getClass());
    }

    @Test
    public void linkedBlockingQueueTest() {
        LinkedBlockingQueue<String> queue = ColUtils.newLinkedBlockingQueue(String.class);
        queue.offer("a");
        queue.offer("b");
        queue.offer("c");
        System.out.println(queue);
        System.out.println(queue.getClass());

        queue = ColUtils.newLinkedBlockingQueue(String.class, 2);
        queue.offer("a");
        queue.offer("b");
        queue.offer("c");
        System.out.println(queue);
        System.out.println(queue.getClass());
    }
}

在这里插入图片描述