java练习题2--Collection
2020-05-16 本文已影响0人
杜艳_66c4
1、 基础
package collectiondemo;
import java.util.ArrayList;
import java.util.Collection;
/**
* Created by apple on 20/5/3.
*/
public class Test1 {
public static void main(String[] args) {
Collection c = new ArrayList();
//可以放不同类型的对象,但是不能是基础数据类型。因为基础在栈上,随时可能消失
c.add("hello");
c.add(new Name("ghj","jh"));
c.add(new Integer(100));
System.out.println(c.size());
System.out.println(c);
boolean h = c.remove("hello");
boolean i = c.remove(new Integer(100));
boolean g = c.remove(new Name("ghj","jh"));
System.out.println(h + " " + i + " " + g);
System.out.println(c);
}
}
class Name{
private String firstName;
private String lastName;
public Name(String firstName,String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public String getFirstName() {
return firstName;
}
public String toString(){
return firstName + lastName;
}
}
输出:
3
[hello, ghjjh, 100]
true true false
[ghjjh]
重写equals方法
package collectiondemo;
import java.util.ArrayList;
import java.util.Collection;
/**
* Created by apple on 20/5/3.
*/
public class Test1 {
public static void main(String[] args) {
Collection c = new ArrayList();
//可以放不同类型的对象,但是不能是基础数据类型。因为基础在栈上,随时可能消失
c.add("hello");
c.add(new Name("ghj","jh"));
c.add(new Integer(100));
System.out.println(c.size());
System.out.println(c);
boolean h = c.remove("hello");
boolean i = c.remove(new Integer(100));
boolean g = c.remove(new Name("ghj","jh"));
System.out.println(h + " " + i + " " + g);
System.out.println(c);
}
}
class Name{
private String firstName;
private String lastName;
public Name(String firstName,String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public String getFirstName() {
return firstName;
}
public String toString(){
return firstName + lastName;
}
public boolean equals (Object obj){
if (obj instanceof Name) {
Name name = (Name)obj;
return(firstName.equals(name.firstName)) &&
(lastName.equals(name.lastName));
}
return super.equals(obj); //交给name的父类Object处理,object的equals方法实现方式:当前对象和传进来的
//对象是不是一个对象,类似于==
}
public int hashCode(){
return firstName.hashCode();
}
}
输出
3
[hello, ghjjh, 100]
true true true
[]
Iterator接口
package collectiondemo;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
/**
* Created by apple on 20/5/3.
*/
public class TestIterator {
public static void main(String[] args) {
Collection c = new HashSet();
c.add(new Name("f1","a1"));
c.add(new Name("f20098","a245667"));
c.add(new Name("f333","a5654"));
Iterator i = c.iterator();
while (i.hasNext()){
//next()返回的是一个object,强制转换为相应类型
Name n = (Name)i.next();
System.out.println(n.getFirstName());
}
for (Iterator j = c.iterator(); j.hasNext(); ) {
Name name = (Name)j.next();
if (name.getFirstName().length() <3){
j.remove();
//如果换成c.remove(name) ,会产生例外
}
}
System.out.println(c);
}
}
输出:
f20098
f333
f1
[f20098 a245667, f333 a5654]
enhance的For循环
package collectiondemo;
import java.util.ArrayList;
import java.util.Collection;
/**
* Created by apple on 20/5/3.
*/
public class EnhanceFor {
public static void main(String[] args) {
int arr[] = {1,3,4,6};
for (int i:arr
) {
System.out.println(i);
}
Collection c = new ArrayList();
c.add(new String("ad"));
c.add(new String("hg"));
for (Object o:c
) {
System.out.println(o);
}
//打印o出来,相当于调用了o的toString方法,而toString 方法重写了。
}
}
输出:
1
3
4
6
ad
hg
Set
package collectiondemo;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
/**
* Created by apple on 20/5/3.
*/
public class SetDemo {
public static void main(String[] args)
{
Set c1 = new HashSet();
Set c2 = new HashSet();
c1.add("a");
c1.add("b");
c1.add("c");
c2.add("d");
c2.add("a");
c2.add("b");
Set n = new HashSet(c1);
n.retainAll(c2); //交集
Set su = new HashSet(c1);
su.addAll(c2);
System.out.println(n);
System.out.println(su);
}
}
输出:
[a, b]
[a, b, c, d]
List
package collectiondemo;
import java.util.LinkedList;
import java.util.List;
/**
* Created by apple on 20/5/3.
*/
public class ListDemo {
public static void main(String[] args) {
List l = new LinkedList() ;
for (int i = 0; i < 6; i++) {
l.add("a" + i);
}
System.out.println(l);
l.add(3,"a100");
l.add(5,"a2");
System.out.println(l);
l.set(6,"a90");
System.out.println(l);
System.out.println((String)l.get(2) + " ");
System.out.println(l.indexOf("a2"));
System.out.println(l.lastIndexOf("a2"));
}
}
输出:
[a0, a1, a2, a3, a4, a5]
[a0, a1, a2, a100, a3, a2, a4, a5]
[a0, a1, a2, a100, a3, a2, a90, a5]
a2
2
5
Collections
package collectiondemo;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* Created by apple on 20/5/3.
*/
public class CollectionsDemo {
public static void main(String[] args) {
List l1 = new LinkedList();
List l2 = new LinkedList();
for (int i = 0; i < 9 ; i++) {
//System.out.println("a" + i);
l1.add("a" + i);
}
System.out.println(l1);
Collections.shuffle(l1);
System.out.println(l1);
Collections.reverse(l1);
System.out.println(l1);
Collections.sort(l1);
System.out.println(l1);
System.out.println(Collections.binarySearch(l1,"a5"));
}
}
输出
[a0, a1, a2, a3, a4, a5, a6, a7, a8]
[a0, a7, a3, a4, a2, a5, a1, a8, a6]
[a6, a8, a1, a5, a2, a4, a3, a7, a0]
[a0, a1, a2, a3, a4, a5, a6, a7, a8]
5
Comparable
package collectiondemo;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* Created by apple on 20/5/3.
*/
public class ComparebleDemo {
public static void main(String[] args) {
List l1 = new LinkedList();
l1.add(new Name("ma","li"));
l1.add(new Name("ks","ai"));
l1.add(new Name("oa","bi"));
System.out.println(l1);
Collections.sort(l1);
System.out.println(l1);
}
}
//Name继承Comparable,重写compareTo
public int compareTo(Object o){
Name n = (Name)o; //强制转换成name
int laC = lastName.compareTo(n.lastName); //lastname 是String类型,肯定实现了compareble接口
return (
laC != 0 ? laC:
firstName.compareTo(n.firstName)
);
}
泛型
package collectiondemo;
import java.util.ArrayList;
import java.util.List;
/**
* Created by apple on 20/5/3.
*/
public class BasicGeneric {
public static void main(String[] args) {
List<String> li = new ArrayList<String>();
li.add("aa");
li.add("bb");
for (int i = 0; i <li.size() ; i++) {
String s = li.get(i);
System.out.println(s);
}
System.out.println("li:" + li);
}
}
输出
aa
bb
li:[aa, bb]
泛型
package collectiondemo;
import com.sun.tools.internal.ws.wsdl.document.soap.SOAPUse;
import java.util.HashMap;
import java.util.Map;
/**
* Created by apple on 20/5/3.
*/
public class MapDemo {
public static void main(String[] args) {
/* Map m1 = new HashMap();
m1.put(1,"ab");
m1.put(2,"cd");
m1.put(3,"fg");
System.out.println(m1.size());
System.out.println(m1.containsKey(2));
if (m1.containsKey(1)){
int i = (Integer)m1.get(1);
System.out.println(i);
}
Map m3 = new HashMap(m1);
m3.putAll(m3);
*/
Map<String,Integer> m1 = new HashMap<String,Integer>();
m1.put("a1",5);
m1.put("a2",2);
System.out.println(m1.size());
System.out.println(m1.containsKey("a1"));
System.out.println(m1);
if (m1.containsKey("a1")) {
int i = m1.get("a1");
System.out.println(i);
}
}
}
输出
2
true
{a1=5, a2=2}
5