Snippets tagged “collections”
17 snippets use this tag.
- Count the number of occurrences of a character in a stringPython
You can use the .count() method to count the number of occurrences of a character in a string.
- How do I count the occurrences of a list item?Python
There are a few ways you can count the occurrences of a list item in Python:
- How do I join two lists in Java?Java
There are several ways to join two lists in Java. Here are a few options:
- How do I use a PriorityQueue?Java
A PriorityQueue is a queue data structure that pops the element with the highest priority first.
- How does collections.defaultdict work?Python
collections.defaultdict is a subclass of the built-in dict class in Python.
- How to convert array to list in JavaJava
To convert an array to a list in Java, you can use the Arrays.asList() method. This method returns a fixed-size list backed by the specified array. Here's an example:
- How to count the number of occurrences of an element in a ListJava
To count the number of occurrences of an element in a List in Java, you can use the Collections.frequency(Collection, Object) method, which returns the number of times the specified element appears in the collection.
- How to create ArrayList from array in JavaJava
To create an ArrayList from an array in Java, you can use the ArrayList constructor that takes an Collection as an argument.
- How to get maximum value from the Collection (for example ArrayList)?Java
In Java, default methods are methods that are defined in an interface and have a default implementation. They were introduced in Java 8 as a way to add new functionality to interfaces without breaking backward compatibility.
- How to sort a List/ArrayList?Java
To sort a List or ArrayList in Java, you can use the sort method of the Collections class from the java.util package. The sort method takes a List and sorts it in ascending order according to the natural ordering of its elements.
- How to sort an ArrayList in JavaJava
To sort an ArrayList in Java, you can use the Collections.sort method.
- Java: Get first item from a collectionJava
To get the first item from a collection in Java, you can use the iterator() method to get an iterator for the collection, and then call the next() method on the iterator to get the first element.
- Python - Count elements in listPython
You can use the len() function to count the number of elements in a list.
- Random shuffling of an arrayJava
To shuffle an array randomly in Java, you can use the Collections.shuffle method.
- Sorting HashMap by valuesJava
To sort a HashMap by values in Java, you can use the Map.Entry interface and the Comparator interface to create a comparator that compares the values in the map.
- What are "named tuples" in Python?Python
"Named tuples" in Python are a subclass of the built-in tuple type, but with the added ability to access elements by name rather than just by index.
- Why do I need to override the equals and hashCode methods in Java?Java
The equals() and hashCode() methods are two methods that are defined in the Object class, which is the superclass of all classes in Java.