Friday, May 18, 2018

java special for collection size, array size, and string size


Size:
For Collections (eg: Map, List, etc ): usually it use collection.size(),
eg
        Map<Character, Integer> map = new HashMap<>();
        int s1 = map.size();
   
        List<Integer> list = Arrays.asList(a);
    int s2 = list.size();

But For Array, it uses length,
eg
        Integer[] array = new Integer[10];
        int s3 = array.length;


For String, it uses length(),

        String str = "string";
        int s4 = str.length();

No comments:

Post a Comment

java special for collection size, array size, and string size

Size: For Collections (eg: Map, List, etc ): usually it use collection.size(), eg         Map<Character, Integer> map ...