Skip to content.

Sections
Personal tools
You are here: Home » 技術文書 » Java » コレクションAPI解答例

Document Actions
import java.util.*;
import java.math.*;

public class Exercise {
    static void assert(boolean c) {
        if (!c)
            throw new Error("Assertion failed.");
    }
    static public void main(String[] v) {
        e1();
        e2();
        e3();
        e4();
        e5();
        e6();
    }
    static public void e1() {
        Collection c = new ArrayList();
        c.add("1");
        c.add(null);
        c.add("2");
        c.add("");
        c.add("");
        c.add("3");
        c.removeAll(Arrays.asList(new String[] { null, "" }));
        System.out.println(c);
        //[1, 2, 3]

        assert(!c.contains(""));
        assert(!c.contains(null));
        assert(c.contains("1"));
        assert(c.contains("2"));
        assert(c.contains("3"));
    }
    static public void e2() {
        System.out.println("--e2--");
        Set s1 = new HashSet(Arrays.asList(new String[] {"1", "2", "3"}));
        Set s2 = new HashSet(Arrays.asList(new String[] {"0", "2", "4"}));

        // 和集合(s1 + s2)
        Set or = new HashSet(s1);
        or.addAll(s2);
        System.out.println(or);
        // [4, 3, 2, 1, 0]
        assert(or.contains("0"));
        assert(or.contains("1"));
        assert(or.contains("2"));
        assert(or.contains("3"));
        assert(or.contains("4"));


        // 差集合(s1 - s2)
        Set diff = new HashSet(s1);
        diff.removeAll(s2);
        System.out.println(diff);
        // [3, 1]
        assert(!diff.contains("0"));
        assert(diff.contains("1"));
        assert(!diff.contains("2"));
        assert(diff.contains("3"));
        assert(!diff.contains("4"));

        // 積集合(s1 * s2)
        Set and = new HashSet(s1);
        and.retainAll(s2);
        System.out.println(and);
        // [2]
        assert(!and.contains("0"));
        assert(!and.contains("1"));
        assert(and.contains("2"));
        assert(!and.contains("3"));
        assert(!and.contains("4"));

    }
    static public void e3() {
        System.out.println("--e3--");
        String[] words = { "1", "2", "1", "4", "3", "2" };
        String[] uniq = (String[])(new TreeSet(Arrays.asList(words))).toArray(new String[0]);

        for (int i = 0; i < uniq.length; i++)
            System.out.println(uniq[i]);
        // 1, 2, 3, 4

    }
    static public void e4() {
        System.out.println("--e4--");
        String[] words = { "c", "E", "A", "B", "d" };
        Arrays.sort(words);
        for (int i = 0; i < words.length; i++)
            System.out.println(words[i]);

        // or Collections.reverse(Arrays.asList(words));
        Arrays.sort(words, Collections.reverseOrder());
        for (int i = 0; i < words.length; i++)
            System.out.println(words[i]);

        Arrays.sort(words, String.CASE_INSENSITIVE_ORDER);
        for (int i = 0; i < words.length; i++)
            System.out.println(words[i]);

    }
    static public void e5() {
        System.out.println("--e5--");

        Set s = new TreeSet(new Comparator() {
            public int compare(Object o1, Object o2) {
                return (new BigInteger((String)o1)).compareTo(
                        new BigInteger((String)o2)
                        );
            }
        });

        s.add("12345678");
        s.add("123");
        s.add("34");
        System.out.println(s);
    }
    static public void e6() {
        System.out.println("--e6--");
        Map m = new HashMap();
        m.put("apple", "red");
        m.put("melon", "green");
        m.put("banana", "yellow");
        String[] keys = (String[])m.keySet().toArray(new String[0]);
        String[] values = (String[])m.values().toArray(new String[0]);

        for (int i = 0; i < keys.length; i++)
            System.out.println(keys[i]);
        // melon, apple, banana という出力

        for (int i = 0; i < values.length; i++)
            System.out.println(values[i]);
        // green, red, yellow という出力
    }
}