Aug 9, 2018
Python
collections.Counter()
collections.Counter()Documentation: https://docs.python.org/2/library/collections.html#collections.Counter
A counter is a container that stores elements as dictionary keys, and their counts are stored as dictionary values.
Sample code:
>>> from collections import Counter
>>>
>>> myList = [1,1,2,3,4,5,3,2,3,4,2,1,2,3]
>>> print Counter(myList)
Counter({2: 4, 3: 4, 1: 3, 4: 2, 5: 1})
>>>
>>> print Counter(myList).items()
[(1, 3), (2, 4), (3, 4), (4, 2), (5, 1)]
>>>
>>> print Counter(myList).keys()
[1, 2, 3, 4, 5]
>>>
>>> print Counter(myList).values()
[3, 4, 4, 2, 1]Related practice:
https://www.hackerrank.com/challenges/collections-counter/problem
Java
ArrayList<>() vs LinkedList<>()
ArrayList<>() vs LinkedList<>()ArrayList: Implemented with the concept of dynamic array.
LinkedList: Implemented with the concept of doubly linked list.
ArrayList and LinkedList both implements List interface and maintains insertion order. Both are non synchronized classes.
But there are many differences between ArrayList and LinkedList classes that are given below.
ArrayList
LinkedList
ArrayList internally uses dynamic array to store the elements.
LinkedList internally uses doubly linked list to store the elements.
Manipulation with ArrayList is slow because it internally uses array. If any element is removed from the array, all the bits are shifted in memory
Manipulation with LinkedList is faster than ArrayList because it uses Doubly Linked List so no bit shifting is required in memory
ArrayList class can act as a list only because it implements List only
LinkedList class can act as a list and queue both because it implements List and Deque interfaces
ArrayList is better for storing and accessing data
LinkedList is better for manipulating data
Comparison between ArrayList and LinkedList:
Insertions are easy and fast in LinkedList as compared to ArrayList because there is no risk of resizing array and copying content to new array if array gets full which makes adding into ArrayList of O(n) in worst case, while adding is O(1) operation in LinkedList in Java. ArrayList also needs to be update its index if you insert something anywhere except at the end of array.
Removal also better in LinkedList than ArrayList due to same reasons as insertion.
LinkedList has more memory overhead than ArrayList because in ArrayList each index only holds actual object (data) but in case of LinkedList each node holds both data and address of next and previous node.
Both LinkedList and ArrayList require O(n) time to find if an element is present or not. However we can do Binary Search on ArrayList if it is sorted and therefore can search in O(Log n) time.
remove an element from ArrayList
using remove() method
using Iterator.remove() method
using remove() method
ArrayList provides two overloaded remove() method.
remove(int index): Accept index of object to be removed.remove(Obejct obj): Accept object to be removed.
Output :
We can see that the passed parameter is considered as index. How to remove elements by value.
Output :
Check whether a string is not null and not empty
String.isEmpty():
Be sure to use the parts of&&in this order, because java will not proceed to evaluate the second part if the first part of&&fails, thus ensuring you will not get a null pointer exception fromstr.isEmpty()ifstris null.
Beware, it's only available since Java SE 1.6. You have to checkstr.length() == 0on previous versions.
To ignore whitespace as well:
Aside:
String.trim():
String.toCharArray()
String.toCharArray()The java String.toCharArray() method converts the given string into a sequence of characters. The returned array length is equal to the length of the string.
Syntax:
Example:
Output:
String.valueOf()
This method has the following variants, which depend on the passed parameters. This method returns the String representation of the passed argument.
valueOf(boolean b)− Returns the string representation of the boolean argument.valueOf(char c)− Returns the string representation of the char argument.valueOf(char[] data)− Returns the string representation of the char array argument.valueOf(char[] data, int offset, int count)− Returns the string representation of a specific subarray of the char array argument.valueOf(double d)− Returns the string representation of the double argument.valueOf(float f)− Returns the string representation of the float argument.valueOf(int i)− Returns the string representation of the int argument.valueOf(long l)− Returns the string representation of the long argument.valueOf(Object obj)− Returns the string representation of the Object argument.
Example:
Output:
String.valueOf() vs. Object.toString()
String.valueOf() vs. Object.toString()According to the Java documentation, String.valueOf() returns:
if the argument is null, then a string equal to
"null"; otherwise, the value ofobj.toString()is returned.
So there shouldn't really be a difference except for an additional method invocation.
Also, in the case of Object.toString(), if the instance is null, a NullPointerException will be thrown, so arguably, it's less safe.
Map.entrySet()
Map.entrySet()The entrySet() method is used to get a Set view of the mappings contained in this map. The method call returns a set view of the mappings contained in this map.
Example:
Output:
Map.Entryis a key and its value combined into one class. This allows you to iterate overMap.entrySet()instead of having to iterate overMap.keySet(), then getting the value for each key. A better way to write what you have is:
ArrayList.addAll()
ArrayList.addAll()boolean addAll(Collection c):
This method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress(implies that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty).
Example:
Output:
boolean addAll(int index, Collection c):
This method inserts all of the elements in the specified collection into this list, starting at the specified position. It shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that they are returned by the specified collection’s iterator.
Example:
Output:
References
GeeksforGeeks
Stack Overflow
tutorialspoint
Last updated