Введение в программирование
Коллекции
for (Iterator<E> i = c.iterator(); i.hasNext(); ) { final E element = i.next(); ... }
for (Iterator<E> i = c.iterator(); i.hasNext(); ) { if (!p(i.next()) { i.remove(); } }
for (T element : collection) { ... }
for (Iterator<T> i = collection.iterator(); i.hasNext(); ) { T element = i.next(); ... }
public void dump(Collection<String> c) { for (Iterator<String> i = c.iterator(); i.hasNext(); ) { final String word = i.next(); System.out.print(word + ", "); } System.out.println(); }
String[] lines = c.toArray(new String[c.size()]);
Set<String> words = new HashSet<String>(); int total = read(args[0], words); System.out.println("Words total: " + total); System.out.println("Unique words: " + words.size()); dump(words);
List<E> list = new ArrayList<E>(); … for (int i = list.size() - 1; i >= 0; i--) { System.out.println(list.get(i)); }
List<E> list = new LinkedList<E>(); … for (ListIterator<E> li = list.listIterator(list.size()); li.hasPrevious(); ) { System.out.println(li.previous()); }
Действие | Исключение | Код возврата |
---|---|---|
Вставка | add{First|Last} | offer{First|Last} |
Доступ | get{First|Last} | peek{First|Last} |
Удаление | remove{First|Last} | poll{First|Last} |
Map<String, Integer> map = new HashMap<String, Integer>(); while (scanner.hasNext()) { String word = scanner.next(); Integer count = map.get(word); map.put(word, count == null ? 1 : count + 1)); }
for ( Map.Entry<String, Integer> entry : map.entrySet() ) { System.out.println( entry.getKey() + " " + entry.getValue()); }
SortedSet<T> words = new TreeSet<String>(); read(args[0], words); dump(words);
SortedSet<T> words = new TreeSet<String>( String.CASE_INSENSITIVE_ORDER); read(args[0], words); dump(words);
List<String> list = new ArrayList<String>(); read(args[0], list); Collections.reverse(list); Collections.shuffle(list); Collections.sort(list); Collections.sort(list, String.CASE_INSENSITIVE_ORDER); Collections.fill(list, "temp"); System.out.println(Collections.min(list)); System.out.println(Collections.min(list, String.CASE_INSENSITIVE_ORDER));