Collection集合方法介绍
Collection
<String> c
= new ArrayList<>();
boolean b
= c
.add("hello");
System
.out
.println(b
);
Collection
<String> c2
= new ArrayList<String>();
c2
.add("李现");
c2
.add("蔡旭");
c
.addAll(c2
);
boolean flag
= c2
.contains("蔡旭");
System
.out
.println(flag
);
flag
= c
.containsAll(c2
);
System
.out
.println(flag
);
boolean empty
= c1
.isEmpty();
System
.out
.println(empty
);
c
.remove("world");
System
.out
.println(c
);
c1
.removeAll(c2
);
System
.out
.println(c1
);
c1
.clear();
int size
= c
.size();
System
.out
.println(size
);
Object
[] array
= c
.toArray();
for(Object s
: array
) {
System
.out
.println(s
);
}
String
[] arr
= c
.toArray(new String[c
.size()]);
for(String s
: arr
) {
System
.out
.println(s
);
}
Iterator
<String> it
= c
.iterator();
while(it
.hasNext()) {
String e
= it
.next();
System
.out
.println(e
);
}
List集合的方法介绍
List
<String> list
= new ArrayList<>();
System
.out
.println(list
);
List
<String> subList
= list
.subList(1, list
.size());
System
.out
.println(subList
);
Vector集合演示
package com
.vector
;
import java
.util
.Enumeration
;
import java
.util
.Vector
;
import org
.junit
.Test
;
public class VectorTest {
@Test
public void testAdd() {
Vector
<String> v
= new Vector<>();
v
.addElement("abc");
v
.addElement("def");
v
.addElement("hig");
v
.addElement(null
);
System
.out
.println(v
);
Enumeration
<String> elements
= v
.elements();
while(elements
.hasMoreElements()) {
String element
= elements
.nextElement();
System
.out
.println(element
);
}
v
.removeElementAt(2);
System
.out
.println(v
);
}
}
Set集合的使用(HashSet)
package com
.set
;
import java
.util
.HashSet
;
import java
.util
.Set
;
import org
.junit
.Test
;
import com
.bean
.Person
;
public class HashSetTest {
@Test
public void test3() {
Set
<Person> set
= new HashSet<>();
set
.add(new Person("张飞", 20));
set
.add(new Person("关羽", 21));
set
.add(new Person("刘备", 22));
set
.add(new Person("刘备", 22));
System
.out
.println(set
);
}
@Test
public void test2() {
Set
<String> set
= new HashSet<String>();
set
.add("b");
set
.add("a");
set
.add("c");
set
.add("a");
System
.out
.println(set
);
}
@Test
public void test1() {
Set
<Integer> set
= new HashSet<>();
set
.add(2);
set
.add(1);
set
.add(3);
set
.add(2);
System
.out
.println(set
);
}
@Test
public void test0() {
Integer i
= 5000;
System
.out
.println(i
.hashCode());
String s
= "";
System
.out
.println(s
.length());
System
.out
.println(s
.hashCode());
System
.out
.println(new Person("张飞", 20).hashCode());
System
.out
.println(new Person("张飞", 20).hashCode());
}
}
LinkedList集合的方法介绍
package com
.linkedlist
;
import java
.util
.LinkedList
;
import org
.junit
.After
;
import org
.junit
.Before
;
import org
.junit
.Test
;
public class LinkedListTest {
LinkedList
<String> list
;
@Before
public void init() {
list
= new LinkedList<String>();
list
.add("java");
list
.add("spring");
System
.out
.println(list
);
}
@After
public void after() {
System
.out
.println(list
);
}
@Test
public void testAdd() {
list
.addFirst("first");
list
.addFirst("xxxx");
System
.out
.println();
list
.addLast("last");
list
.addLast("yyy");
System
.out
.println("---------------");
list
.offer("haha");
System
.out
.println("---------");
list
.offerFirst("1111");
list
.offerLast("33333");
System
.out
.println("---------");
list
.push("push");
}
@Test
public void testQuery() {
String e
= list
.element();
System
.out
.println(e
);
String first
= list
.getFirst();
System
.out
.println(first
);
String last
= list
.getLast();
System
.out
.println(last
);
System
.out
.println("-----------");
String peek
= list
.peek();
System
.out
.println(peek
);
String peekFirst
= list
.peekFirst();
System
.out
.println(peekFirst
);
String peekLast
= list
.peekLast();
System
.out
.println(peekLast
);
System
.out
.println("------------");
String pop
= list
.pop();
System
.out
.println(pop
);
}
@Test
public void testDelete() {
String removeFirst
= list
.removeFirst();
System
.out
.println(removeFirst
);
String removeLast
= list
.removeLast();
System
.out
.println(removeLast
);
}
}
Map集合的方法介绍
package com
.map
;
import java
.util
.Collection
;
import java
.util
.HashMap
;
import java
.util
.Iterator
;
import java
.util
.Map
;
import java
.util
.Set
;
import org
.junit
.Test
;
public class HashMapTest {
@Test
public void testAdd() {
Map
<String, String> map
= new HashMap<>();
map
.put("aa", "aaaa");
String v
= map
.put("bb", "bbbb");
System
.out
.println(v
);
Map
<String, String> mp2
= new HashMap<String, String>();
mp2
.put("11", "1111");
mp2
.put("22", "2222");
map
.putAll(mp2
);
System
.out
.println(map
);
map
.put("cc", "value");
System
.out
.println(map
);
}
@Test
public void testDelete() {
Map
<String, String> map
= new HashMap<>();
map
.put("aa", "aaaa");
map
.put("bb", "bbbb");
map
.put("cc", "cccc");
System
.out
.println(map
);
map
.remove("bb");
System
.out
.println(map
);
map
.remove("aa", "aaaa");
System
.out
.println(map
);
}
@Test
public void testCodition() {
Map
<String, String> map
= new HashMap<>();
map
.put("aa", "aaaa");
map
.put("bb", "bbbb");
map
.put("cc", "cccc");
System
.out
.println(map
);
boolean flag
= map
.containsKey("aa");
System
.out
.println(flag
);
flag
= map
.containsValue("xxx");
flag
= map
.containsValue("aaaa");
System
.out
.println(flag
);
map
.clear();
flag
= map
.isEmpty();
System
.out
.println(flag
);
}
@Test
public void testGet() {
Map
<String, String> map
= new HashMap<>();
map
.put("aa", "aaaa");
map
.put("bb", "bbbb");
map
.put("cc", "cccc");
System
.out
.println(map
);
String val
= map
.get("aa");
System
.out
.println(val
);
val
= map
.get("xx");
System
.out
.println(val
);
int size
= map
.size();
System
.out
.println(size
);
}
@Test
public void testReplace() {
Map
<String, String> map
= new HashMap<>();
map
.put("aa", "aaaa");
map
.put("bb", "bbbb");
map
.put("cc", "cccc");
System
.out
.println(map
);
map
.replace("aa", "11111");
System
.out
.println(map
);
}
@Test
public void testIterator() {
Map
<String, String> map
= new HashMap<>();
map
.put("aa", "aaaa");
map
.put("bb", "bbbb");
map
.put("cc", "cccc");
Set
<String> keySet
= map
.keySet();
Iterator
<String> it
= keySet
.iterator();
while(it
.hasNext()) {
String key
= it
.next();
String value
= map
.get(key
);
System
.out
.println(key
+ "=" + value
);
}
System
.out
.println("------------");
for(String key
: keySet
) {
String value
= map
.get(key
);
System
.out
.println(key
+ "=" + value
);
}
System
.out
.println("-------------");
for(String key
: map
.keySet()) {
String value
= map
.get(key
);
System
.out
.println(key
+ "=" + value
);
}
System
.out
.println("================");
Collection
<String> values
= map
.values();
for (String val
: values
) {
System
.out
.println(val
);
}
System
.out
.println("================");
Set
<Map
.Entry
<String, String>> entrySet
= map
.entrySet();
Iterator
<Map
.Entry
<String, String>> it2
= entrySet
.iterator();
while(it2
.hasNext()) {
Map
.Entry
<String, String> entry
= it2
.next();
String key
= entry
.getKey();
String val
= entry
.getValue();
System
.out
.println(key
+ "=" + val
);
}
}
}
LinkedHashMap的使用
package com
.map
;
import java
.util
.LinkedHashMap
;
import org
.junit
.Test
;
public class LinkedHashMapTest {
@Test
public void test03() {
LinkedHashMap
<Book, Integer> lhm
= new LinkedHashMap<>();
lhm
.put(new Book(1, "从入门到放弃"), 33);
lhm
.put(new Book(2, "从入门到精通"), 11);
lhm
.put(new Book(3, "从精通到放弃"), 22);
System
.out
.println(lhm
);
}
@Test
public void test02() {
LinkedHashMap
<String, Integer> lhm
= new LinkedHashMap<>();
lhm
.put("bb", 33);
lhm
.put("aa", 11);
lhm
.put("cc", 22);
System
.out
.println(lhm
);
}
@Test
public void test01() {
LinkedHashMap
<Integer, Integer> lhm
= new LinkedHashMap<>();
lhm
.put(3, 33);
lhm
.put(1, 11);
lhm
.put(2, 22);
System
.out
.println(lhm
);
}
}
转载请注明原文地址: https://mac.8miu.com/read-509435.html