Технологии Java
Collections Framework
public int read(String file, Collection<String> c)
throws IOException {
Scanner scanner =
new Scanner(Paths.get(file), "UTF-8");
int read = 0;
while (scanner.hasNext()) {
read++;
c.add(scanner.next());
}
return read;
}
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()]); String[] lines = c.toArray(String[]::new);
public class Point {
protected int x;
protected int y;
public boolean equals(Object o) {
if (o instanceof Point) {
Point that = (Point) o;
return this.x == that.x && this.y == that.y;
}
return false;
}
}
public class ColorPoint extends Point {
protected int c;
public boolean equals(Object o) {
if (o instanceof ColorPoint) {
ColorPoint that = (ColorPoint) o;
return this.x == that.x && this.y == that.y && this.c == that.c;
}
return false;
}
}
public class ColorPoint extends Point {
protected int c;
public boolean equals(Object o) {
if (o instanceof ColorPoint) {
ColorPoint that = (ColorPoint) o;
return this.x == that.x && this.y == that.y && this.c == that.c;
}
return super.equals(o);
}
}
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<String> words = new TreeSet<>(); read(args[0], words); dump(words);
SortedSet<String> words = new TreeSet<>(
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));