#Java - Code Snippets for '#Arraylist' - 10 code snippet(s) found |
|
Sample 1. Usage of Apache Commons - ArrayListValuedHashMap | |
|
ListValuedMap<String,String> listValuedMap = new ArrayListValuedHashMap();
listValuedMap.put("United States", "Washington");
listValuedMap.put("Canada", "Ottawa");
listValuedMap.put("Canada", "Ottawa");
listValuedMap.put("South Africa", "Pretoria");
listValuedMap.put("South Africa", "Cape Town");
listValuedMap.put("South Africa", "Bloemfontein");
System.out.println(listValuedMap); // Values being added to the List and allow even duplicates
|
|
Like Feedback apache commons apache commons collections ArrayListValuedHashMap ListValuedMap |
|
|
Sample 2. Get all Permutations of List Elements using Apache Commons PermutationIterator | |
|
List<String> list = new ArrayList();
list.add("Washington");
list.add("Nevada");
list.add("California");
PermutationIterator permIterator = new PermutationIterator((Collection) list);
permIterator.forEachRemaining(System.out::print); // prints [Washington, Nevada, California][Washington, California, Nevada][California, Washington, Nevada][California, Nevada, Washington][Nevada, California, Washington][Nevada, Washington, California]
|
|
Like Feedback Permutations of collection elements Permutations of List elements Apache Commons PermutationIterator Iterator Collections .forEachRemaining System.out::print List ArrayList |
|
|
Sample 3. Sorting an arraylist using Collections.sort | |
|
List<String> myList = new ArrayList<String>();
myList.add("D");
myList.add("A");
myList.add("B");
myList.add("C");
myList.add("E");
System.out.println(myList); // Unsorted
Collections.sort(myList);
System.out.println(myList); // Sorted
|
|
Like Feedback list arraylist collections.sort collections |
|
|
Sample 4. Looping through elements of an ArrayList | |
|
public static void main(String[] args){
List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
for(Integer element: list){
System.out.println(element);
}
}
|
|
Like Feedback arraylist list collections |
|
|
|
Sample 5. Display Elements of a List using Java 8 Consumer | |
|
List myList = new ArrayList();
myList.add("A");
myList.add("B");
myList.add("C");
myList.forEach(System.out::println);
|
|
Like Feedback Print elements of a list java 8 consumer foreach list arraylist collections |
|
|
Sample 6. ArrayList of Optional Integers | |
|
List<Optional<Integer>> intList = new ArrayList<Optional<Integer>>();
// Add Elements
intList.add(Optional.empty());
intList.add(Optional.of(2));
intList.add(Optional.of(3));
intList.add(Optional.of(4));
|
|
Like Feedback optional java 8 list arraylist list of optional integers arraylist of optional java.util.Optional |
|
|
Sample 7. Initialize list using google guava Lists | |
|
import java.util.List;
import com.google.common.collect.Lists;
class GoogleListsTest{
public static void main(String[] args){
List list = Lists.newArrayList();
}
}
|
|
Like Feedback list google guava Lists Lists.newArrayList com.google.common.collect.Lists |
|
|
Sample 8. Internal Implementation of ArrayList#removeIf | |
|
@Override
public boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
int removeCount = 0;
final BitSet removeSet = new BitSet(size);
final int expectedModCount = modCount;
final int size = this.size;
for (int i=0; modCount == expectedModCount && i < size; i++) {
@SuppressWarnings("unchecked")
final E element = (E) elementData[i];
if (filter.test(element)) {
removeSet.set(i);
removeCount++;
}
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
// shift surviving elements left over the spaces left by removed elements
final boolean anyToRemove = removeCount > 0;
if (anyToRemove) {
final int newSize = size - removeCount;
for (int i=0, j=0; (i < size) && (j < newSize); i++, j++) {
i = removeSet.nextClearBit(i);
elementData[j] = elementData[i];
}
for (int k=newSize; k < size; k++) {
elementData[k] = null; // Let gc do its work
}
this.size = newSize;
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}
return anyToRemove;
}
|
|
Like Feedback Internal Implementation of ArrayList#removeIf java.util.function.Predicate java.util.Objects java.util.Objects java.util.ConcurrentModificationException.ConcurrentModificationException |
|
|
Sample 9. Display elements of a List using For loop | |
|
List myList = new ArrayList();
myList.add("A");
myList.add("B");
myList.add("C");
for(String str:myList){ // prints A B C
System.out.println(str);
}
|
|
Like Feedback Print elements of a list for loop arraylist list collections |
|
|
|
Sample 10. Display elements of a List using For Loop and index | |
|
List myList = new ArrayList();
myList.add("A");
myList.add("B");
myList.add("C");
for(int index=0;index < myList.size(); index++){
System.out.println(myList.get(index));
}
|
|
Like Feedback Print elements of a list arraylist list collections for loop |
|
|