.


:




:

































 

 

 

 


2 1




[a, b, X, c, d, e, f, a, g]

[a, b, X, c, d, e, f, W, g]

[b, X, c, d, e, f, W, g] a

LinkedList<E> . , , , .

LinkedList<E> void addFirst(E ob), void addLast(E ob), E getFirst(),
E getLast(), E removeFirst(), E removeLast() , , .

LinkedList<E> Queue<E>, , . Queue<E> E element(), boolean offer(E o), E peek(), E poll() , E remove() , LinkedList<E>.

Queue<E>: E element() , ; boolean offer(E o) , ; E peek() , , null , ; E poll() , null , ; E remove() .

element() remove() peek() poll() , , .

/* # 5: : DemoLinkedList.java */

package chapt10;

import java.util.*;

 

public class DemoLinkedList {

public static void main(String[] args){

LinkedList<Number> a = new LinkedList<Number>();

for (int i = 10; i <= 15; i++)

a.add(i);

for (int i = 16; i <= 20; i++)

a.add(new Float(i));

ListIterator<Number> list = a.listIterator(10);

System. out. println("\n"+ list.nextIndex()

+ "- ");

list.next(); // !

System. out. println(list.nextIndex()

+ "- ");

list.remove(); //

while (list.hasPrevious())

System. out. print(list.previous()+" "); /*

*/

//

a.removeFirst();

a.offer(71); //

a.poll(); //

a.remove(); //

a.remove(1); //

System. out. println("\n" + a);

 

Queue<Number> q = a; //

for (Number i: q) //

System. out. print(i + " ");

System. out. println(":size= " + q.size());

 

//

for (int i = 0; i < 5; i++) {

Number res = q.poll();

}

System. out. print("size= " + q.size());

}

}

:

19.0 18.0 17.0 16.0 15 14 13 12 11 10

[13, 15, 16.0, 17.0, 18.0, 19.0, 71]

13 15 16.0 17.0 18.0 19.0 71:size= 7

size= 2

Comparator<T> , . int compare(T ob1,
T ob2)
, , . public static <T> void sort(List<T> list, Comparator<? super T> c) Collections, , -comparator, .

/* # 6: : UniqSortMark.java */

package chapt10;

import java.util.Comparator;

 

public class Student implements Comparator<Student> {

private int idStudent;

private float meanMark;

 

public Student(float m, int id) {

meanMark = m;

idStudent = id;

}

public Student() {

}

public float getMark() {

return meanMark;

}

public int getIdStudent() {

return idStudent;

}

//

public int compare(Student one, Student two) {

Return

(int)(Math.ceil(two.getMark() - one.getMark()));

}

}

package chapt10;

import java.util.*;

 

public class UniqSortMark {

public static void main(String[] args) {

ArrayList<Student> p = new ArrayList<Student>();

p.add(new Student(3.9f, 52201));

p.add(new Student(3.65f, 52214));

p.add(new Student(3.71f, 52251));

p.add(new Student(3.02f, 52277));

p.add(new Student(3.81f, 52292));

p.add(new Student(9.55f, 52271));

//

try {

Collections.sort(p, Student. class. newInstance ());

} catch (InstantiationException e1) {

//

e1.printStackTrace();

} catch (IllegalAccessException e2) {

e2.printStackTrace();

}

for (Student ob: p)

System.out.printf("%.2f ", ob.getMark());

}

}

:

9,55 3,90 3,81 3,71 3,65 3,02

boolean equals(Object obj) Comparator<T>, , true compare() 0.

id Student , Comparator .

/* # 7: : StudentId.java */

package chapt10;

 

public class StudentId implements Comparator<Student> {

public int compare(Student one, Student two) {

return two.getIdStudent() - one.getIdStudent();

}

}

id StudentId:

Collections.sort(p, StudentId.class. newInstance ());

, .

Deque

Deque , , . , . . , - (null false ). Deque, . .

Deque. addFirst(), addLast() . add() Queue addLast() Deque.

/* # 8: Deque: DequeRunner.java */

package chapt10;

import java.util.*;

 

public class DequeRunner {

public static void printDeque(Deque<?> d){

for (Object de: d)

System. out. println(de + "; ");

}

public static void main(String[] args) {

Deque<String> deque = new ArrayDeque<String>();

deque.add(new String("5"));

deque.addFirst("A");

//deque.addLast(new Integer(5));//

System. out. println(deque.peek());

System. out. println("Before:");

printDeque (deque);

deque.pollFirst();

System. out. println(deque.remove(5));

System. out. println("After:");

printDeque (deque);

}

}

:

A

Before:

A;

5;

False

After:

5;

Set<E> , . SortedSet<E> Set<E> , , . NavigableSet .

. 10.2.

HashSet<E>
AbstractSet<E> Set<E>, - . (-) , . . -, - .

:

HashSet()

HashSet(Collection <? extends E> c)

HashSet(int capacity)

HashSet(int capacity, float loadFactor), capacity -.

/* # 9: : DemoHashSet.java */

package chapt10;

import java.util.*;

import java.io.*;

public class DemoHashSet {

public static void main(String[] args) {

HashSet<String> words = new HashSet<String>(100);

// LinkedHashSet TreeSet

long callTime = System. nanoTime ();

try {

BufferedReader in =

new BufferedReader(

new FileReader("c://pushkin.txt"));

String line = "";

while ((line = in.readLine())!= null) {

StringTokenizer tokenizer =

new StringTokenizer(line,

" (){}[]<>#*!?.,:;-\'\"/");

while (tokenizer.hasMoreTokens()) {

String word = tokenizer.nextToken();

words.add(word.toLowerCase());

}

}

} catch (IOException e) {

System. err. println(e);

}

Iterator<String> it = words.iterator();

while (it.hasNext())

System. out. println(it.next());

long totalTime = System. nanoTime ()- callTime;

System. out. println(" : " + words.size()

+ ", " + totalTime + " ");

}

}

TreeSet<E> . . , Comparator Comparable. , -, , .

:

TreeSet()

TreeSet(Collection <? extends E> c)

TreeSet(Comparator <? super E> c)

TreeSet(SortedSet <E> s)

TreeSet<E> ( ) E first() E last(). SortedSet<E> subSet(E from, E to),

SortedSet<E> tailSet(E from)

SortedSet<E> headSet(E to)

.
Comparator <? super E> comparator()
Comparator, null, .

/* # 10: : DemoTreeSet.java */

package chapt10;

import java.util.*;

 

public class DemoTreeSet {

public static void main(String[] args) {

ArrayList<String> c = new ArrayList<String>();

boolean b;

for (int i = 0; i < 6; i++)

c.add((int) (Math. random () * 71) + "Y ");

System. out. println(c + "");

TreeSet<String> set = new TreeSet<String>(c);

System. out. println(set + "");

b = set.add("5 Element"); // (b=true)

b = set.add("5 Element"); // (b=false)

//

System. out. println(set + "add");

System. out. println(set.comparator()); //null!!!

//

System. out. println(set.last() + " "

+ set.first());

}

}

:

[44Y, 56Y, 49Y, 26Y, 49Y, 2Y ]

[2Y, 26Y, 44Y, 49Y, 56Y ]

[2Y, 26Y, 44Y, 49Y, 5 Element, 56Y ]add

Null

Y 2Y

. . . , , , comparator() null.

String StringBuilder
StringBuffer, TreeSet . StringBuilder Comparable<T> :

/* # 11: , TreeSet: Message.java */

package chapt10;

import java.util.*;

public class Message implements Comparable<Message> {

private StringBuilder str;

private int idSender;

 

public Message(StringBuilder s, int id) {

super ();

this. str = s;

idSender = id;

}

public String getStr() {

return str.toString();

}

public int getId() {

return idSender;

}

public int compareTo(Message a0) {

return (idSender - a0.getId());

}

}

.

EnumSet<E extends Enum<E>> AbstractSet. enum. enum, . , long. . , .

. EnumSet<E> noneOf(Class<E> elemType) c ,
allOf(Class<E> elementType) , . of(E first, E... rest) , .
complementOf(EnumSet<E> s) , , . range(E from, E to) , , . null NullPointerException.

/* # 12: c enum-: UseEnumSet.java */

package chapt10;

import java.util.EnumSet;

 

enum Faculty{ FFSM, MMF, FPMI, FMO, GEO }

 

public class UseEnumSet {

public static void main(String[] args) {

/* set1 enum ,

*/

EnumSet <Faculty> set1 =

EnumSet. range (Faculty. MMF, Faculty. FMO);

/* set2 ,
set1*/

EnumSet <Faculty> set2 =

EnumSet. complementOf (set1);

System. out. println(set1);

System. out. println(set2);

}

}

:

[MMF, FPMI, FMO]

[FFSM, GEO]

NavigableSet. first() . subSet(E fromElement, E toElement) , fromElement toElement, . headSet(E element) tailSet(E element, boolean inclusive) , element . inclusive true, .

/* # 13: c NavigableSet: NavigableSetTest.java */

package chapt10;

import java.util.*;

 

public class NavigableSetTest {

public static void main(String[] args) {

HashSet<String> city = new HashSet<String>();

city.add("Minsk");

city.add("Mosow");

city.add("Polotsk");

city.add("Brest");

NavigableSet<String> ns = new TreeSet<String>(city);

System. out. println("All: " + ns);

System. out. println("First: " + ns.first());

System. out. println("Between Minsk and Polotsk: "

+ ns.subSet("Minsk","Polotsk"));

System. out. println("Before Minsk: "

+ ns.headSet("Minsk"));

System. out. println("After Minsk: "

+ ns.tailSet("Minsk", false));

}

}

:

All: [Brest, Minsk, Mosow, Polotsk]

First: Brest

Between Minsk and Polotsk: [Minsk, Mosow]

Before Minsk: [Brest]

After Minsk: [Mosow, Polotsk]

, -. () , . - hashCode() equals() . , null.

:

AbstractMap<K,V> Map<K,V>;

HashMap<K,V> AbstractMap<K,V>, -, -;

TreeMap<K,V> AbstractMap<K,V>, , .

WeakHashMap<K,V> , .

LinkedHashMap<K,V> . , - .

. 10.3.

IdentityHashMap<K,V> - - System.identityHashCode() , hashCode(), .

:

Map<K,V> ;

Map.Entry<K,V> -;

SortedMap<K,V> ;

NavigableMap<K,V> .

Map<K,V> :

void clear() ;

boolean containsKey(Object key) true, key ;

boolean containsValue(Object value) true, value ;

Set<Map.Entry<K,V>> entrySet() , ;

Set<K> keySet() ;

V get(Object obj) , obj;

V put(K key, V value) key value . . ;

void putAll(Map <? extends K,? extends V> t) t ;

V remove(Object key) - key;

Collection<V> values() , .

Map.Entry<K,V> :

K getKey() ;

V getValue() ;

V setValue(V obj) obj .

-
.

/* # 14: - :

DemoHashMap.java */

package chapt10;

import java.util.*;

 

public class DemoHashMap {

public static void main(String[] args){

HashMap<Integer, String> hm =

new HashMap<Integer, String>(5);

for (int i = 11; i < 15; i++)

hm.put(i, i + "EL");

System. out. println(hm);

hm.put(12, "14EL");

System. out. println(hm + " ");

String a = hm.get(12);

System. out. println(a + " - '12'");

/* -

Map.Entry<K,V> */

Set<Map.Entry<Integer, String>> setvalue =

hm.entrySet();

System. out. println(setvalue);

Iterator<Map.Entry<Integer, String>> i =

setvalue.iterator();

while (i.hasNext()) {

Map.Entry<Integer, String> me = i.next();

System. out. print(me.getKey()+": ");

System. out. println(me.getValue());

}

}

}

{13=13EL, 14=14EL, 12=12EL, 11=11EL}

{13=13EL, 14=14EL, 12=14EL, 11=11EL}

14EL - '12'

[13=13EL, 14=14EL, 12=14EL, 11=11EL]

EL

EL

EL





:


: 2016-04-03; !; : 489 |


:

:

: , .
==> ...

2028 - | 1650 -


© 2015-2024 lektsii.org - -

: 0.19 .