马士兵J2SE-第七章-容器

简介:
 
class Name {
	private String firstName,lastName;
	public Name(String firstName,String lastName) {
		this.firstName=firstName;
		this.lastName=lastName;
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public String toString() {
		return firstName+" "+lastName;
	}
}



public class test {
	public static void main(String[] args) {
		Name name1=new Name("f1","l1");
		Name name2=new Name("f2","l2");
		Name name3=new Name("f3","l3");
		System.out.println(name1);
		System.out.println(name2);
		System.out.println(name3);
	}
}


输出:

 

f1 l1
f2 l2
f3 l3

 

 

import java.util.*;

public class test {
	public static void main(String[] args) {
		Collection c=new ArrayList();
		
		c.add("hello");
		c.add(new Name("f1","l1"));
		c.add(new Integer(100));
		
		System.out.println(c.size());
		System.out.println(c);
	}
}

class Name {
	private String firstName,lastName;
	public Name(String firstName,String lastName) {
		this.firstName=firstName;
		this.lastName=lastName;
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public String toString() {
		return firstName+" "+lastName;
	}
}
输出:

3
[hello, f1 l1, 100]


import java.util.*;

public class test {
	public static void main(String[] args) {
		Collection c=new HashSet();
		
		c.add("hello");
		c.add(new Name("f1","l1"));
		c.add(new Integer(100));
		
		c.remove("hello");
		c.remove(new Integer(100));
		
		System.out.println(c.remove(new Name("f1","l1")));
		System.out.println(c.size());
		System.out.println(c);
	}
}

class Name {
	private String firstName,lastName;
	public Name(String firstName,String lastName) {
		this.firstName=firstName;
		this.lastName=lastName;
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public String toString() {
		return firstName+" "+lastName;
	}
}

输出:

false
1
[f1 l1]


import java.util.*;

public class test {
	public static void main(String[] args) {
		Collection c=new LinkedList();
		
		
		c.add(new Name("f1","l1"));
		c.add(new Name("f2","l2"));
		
		System.out.println(c.contains(new Name("f2","l2")));
		c.remove(new Name("f2","l2"));
		System.out.println(c);
		
	}
}

class Name {
	private String firstName,lastName;
	public Name(String firstName,String lastName) {
		this.firstName=firstName;
		this.lastName=lastName;
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	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);
	}

	public int hashCode() {
		return firstName.hashCode();
	}
	
}


输出:

true
[f1 l1]


 

Iterator方法之remove

package com.zzk.test;

import java.util.*;

public class test {
	public static void main(String[] ars) {
		Collection c=new HashSet();
		c.add(new Name("ff1f","l1l1"));
		c.add(new Name("f2","l2"));
		c.add(new Name("fff3","lll3"));
		for(Iterator i=c.iterator();i.hasNext();) {
			Name name=(Name)i.next();
			if(name.getFirstName().length()<3) {
				i.remove();
			}
		}
		System.out.println(c);
	}
}


class Name {
	private String firstName,lastName;
	public Name(String firstName,String lastName) {
		this.firstName=firstName;
		this.lastName=lastName;
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public String toString() {
		return firstName+" "+lastName;
	}
}

输出:

[fff3 lll3, ff1f l1l1]



增强的for循环:

package com.zzk.test;

import java.util.*;


public class test {
	public static void main(String[] args) {
		int[] arr={1,2,3,4,5};
		for(int i:arr) {
			System.out.println(i);
		}
		
		Collection c=new ArrayList();
		c.add(new String("aaa"));
		c.add(new String("bbb"));
		c.add(new String("ccc"));
		for(Object o:c) {
			System.out.println(o);
		}
	}
}

输出:

1
2
3
4
5
aaa
bbb
ccc



Set方法举例:

package com.zzk.test;

import java.util.*;
 
public class test {
	public static void main(String[] args) {
		Set s1=new HashSet();
		Set s2=new HashSet();
		s1.add("a");s1.add("b");s1.add("c");
		s2.add("d");s2.add("a");s2.add("b");
		
		Set sn=new HashSet(s1);
		sn.retainAll(s2);
		System.out.println(s2);
		
		Set su=new HashSet(s1);
		su.addAll(s2);
		System.out.println(su);
	}
}


输出:

[d, b, a]
[d, b, c, a]

 

 

 LIST方法

package com.zzk.test;

import java.util.*;
 
public class test {
	public static void main(String[] args) {
		List l1=new LinkedList();
		
		for(int i=0;i<=5;i++) {
			l1.add("a"+i);
		}
		
		System.out.println(l1);
		l1.add(3,"a100");
		System.out.println(l1);
		l1.set(6, "a200");
		
		System.out.println(l1);
		System.out.print((String)l1.get(2)+" ");
		System.out.println(l1.indexOf("a3"));
		
		l1.remove(1);
		System.out.println(l1);
		
	}
}


输出:

[a0, a1, a2, a3, a4, a5]
[a0, a1, a2, a100, a3, a4, a5]
[a0, a1, a2, a100, a3, a4, a200]
a2 4
[a0, a2, a100, a3, a4, a200]

 

 LIST常用算法:

package com.zzk.test;

import java.util.*;
 
public class test {
	public static void main(String[] args) {
		List l1=new LinkedList();
		List l2=new LinkedList();
		for(int i=0;i<=9;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);
		
		Collections.binarySearch(l1, "a5");
		System.out.println(l1);
		
	}
}

输出:

[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
[a9, a7, a6, a4, a0, a1, a3, a8, a5, a2]
[a2, a5, a8, a3, a1, a0, a4, a6, a7, a9]
[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]

 

 


Comparable接口:

package com.zzk.test;
import java.util.*;

public class test {
    public static void main(String[] args) {
       List l1=new LinkedList();
       l1.add(new Name("Karl","M"));
       l1.add(new Name("Strven","Lee"));
       l1.add(new Name("John","O"));
       l1.add(new Name("Tom","M"));
       System.out.println(l1);
       Collections.sort(l1);
       System.out.println(l1);
    }


}

class Name implements Comparable {
    private String firstName,lastName;
    public Name(String firstName, String lastName) {
        this.firstName = firstName; this.lastName = lastName;
    }
    public String getFirstName() {  return firstName;   }
    public String getLastName() {   return lastName;   }
    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);
		}
		public int hashCode() {
		    return firstName.hashCode();
		}
		
		
		
		public int compareTo(Object o) {//重写,要去copy!
        Name n = (Name)o;
        int lastCmp = 
            lastName.compareTo(n.lastName);
        return 
             (lastCmp!=0 ? lastCmp :
              firstName.compareTo(n.firstName));
    }
		
}


输出:

[Karl M, Strven Lee, John O, Tom M]
[Strven Lee, Karl M, Tom M, John O]

 

 

 Map接口

package com.zzk.test;
import java.util.*;

public class test {
	public static void main(String[] args) {
		Map m1=new HashMap();
		Map m2=new TreeMap();
		
		m1.put("one", new Integer(1));//key是String类型,value是Integer类型
		m1.put("two", new Integer(2));
		m1.put("three", new Integer(3));
		
		m2.put("A", new Integer(1));
		m2.put("B", new Integer(2));
		
		System.out.println(m1.size());//m1的size,3
		System.out.println(m1.containsKey("one"));//contains时需要equals,需要比较hashcode
		System.out.println(m2.containsValue(new Integer(1)));
		
		if(m1.containsKey("two")) {
			int i=((Integer)m1.get("two")).intValue();
			System.out.println(i);
		}
		
		Map m3=new HashMap();
		m3.putAll(m2);
		System.out.println(m3);
	}
}


输出:

3
true
true
2
{A=1, B=2}

 

 泛型

package com.zzk.test;
import java.util.*;

public class test {
	public static void main(String[] args) {
		List<String> c=new ArrayList<String>();
		c.add("aaa");
		c.add("bbb");
		c.add("ccc");
		c.add("aaa");
		
		for(int i=0;i<c.size();i++) {
			String s=c.get(i);
			System.out.println(s);
		}
		
		Collection<String> c2=new HashSet<String>();
		c2.add("aaa");
		c2.add("bbb");
		c2.add("ccc");
		c2.add("aaa");
		
		for(Iterator<String> it=c2.iterator();it.hasNext();) {
			String s=it.next();
			System.out.println(s);
		}
		
	}
}


class MyName implements Comparable<MyName> {
	int age;
	
	@Override
	public int compareTo(MyName mn) {
		// TODO Auto-generated method stub
		if(this.age>mn.age) return 1;
		else if(this.age<mn.age) return -1;
		else return 0;
	}
}


输出:

aaa
bbb
ccc
aaa
aaa
ccc
bbb

 

 

 泛型与打包

package com.zzk.test;
import java.util.*;

public class test {
	public static void main(String[] args) {
		Map<String,Integer> m1=new HashMap<String,Integer>();
		m1.put("one", 1);
		m1.put("two", 2);
		m1.put("three", 3);
		
		System.out.println(m1.size());
		System.out.println(m1.containsKey("one"));
		
		if(m1.containsKey("two")) {
			int i=m1.get("two");
			System.out.println(i);
		}
	}
}


输出:

3
true
2

 

 

 

 

 

 

 

 


 



 

目录
相关文章
|
JSON Kubernetes Docker
K8S 1.20 弃用 Docker 评估之 Docker 和 OCI 镜像格式的差别
K8S 1.20 弃用 Docker 评估之 Docker 和 OCI 镜像格式的差别
|
JSON 供应链 API
1688平台获取店铺所有商品列表API接口技术详解
本文详细介绍如何调用1688平台“获取店铺所有商品列表”API,涵盖认证流程、请求参数、响应处理及Python代码实现,助力开发者高效集成商品数据,适用于电商数据分析与库存管理。
1211 0
|
7月前
|
存储 搜索推荐 安全
搜索引擎 SEO 投毒与 ClickFix 钓鱼攻击机理及防御研究
本文剖析2026年KISA披露的SEO投毒+ClickFix钓鱼攻击:黑产利用搜索引擎排名与广告机制,伪造KakaoTalk等高仿下载页,诱导用户执行恶意PowerShell命令,实现无文件窃密。揭示其高隐蔽、强诱导、难检测特性,提出覆盖平台、网络、终端、用户的闭环防御体系。(239字)
447 0
|
11月前
|
自然语言处理 算法 测试技术
大模型应用:基于本地大模型的中文命名实体识别技术实践与应用
本文探讨了基于本地部署的大模型在命名实体识别(NER)任务中的应用优势。通过通用领域中文NER和医疗领域专用NER两个典型案例,展示了本地大模型在数据安全、响应速度和识别精度方面的显著优势。通用领域采用RoBERTa模型在CLUENER2020数据集上微调,可识别10类实体;医疗领域基于BERT架构的专用模型,在CMEEE数据集上训练,准确识别疾病、症状等医疗实体。本地部署不仅满足合规要求,还能通过领域自适应提升专业文本识别效果,为各行业智能化转型提供可靠技术方案。
937 14
|
人工智能 关系型数据库 MySQL
基于阿里云的PolarDB MySQL版实现AI增强数据管理
本文将介绍如何利用阿里云的PolarDB MySQL版结合AI技术,实现数据管理的自动化和智能化。
1301 0
|
SQL 存储 Oracle
PostgreSQL 分页, offset, 返回顺序, 扫描方法原理(seqscan, index scan, index only scan, bitmap scan, parallel xx scan),游标
PostgreSQL 分页, offset, 返回顺序, 扫描方法原理(seqscan, index scan, index only scan, bitmap scan, parallel xx scan),游标
4338 0
|
缓存 安全 PHP
PHP 在现代Web开发中的角色与挑战
【8月更文挑战第11天】随着互联网技术的飞速发展,PHP作为一门历史悠久的服务器端脚本语言,在现代Web开发中扮演着不可忽视的角色。本文将深入探讨PHP的现状、面临的挑战以及如何适应现代Web开发的新趋势。通过分析PHP的核心优势和存在的局限性,我们旨在为开发者提供一个关于如何使用PHP进行高效、安全且可维护的Web应用开发的全面视角。
|
数据采集 SQL 关系型数据库
在 Postgres 中使用 Intersection
【8月更文挑战第11天】
644 0
在 Postgres 中使用 Intersection
|
监控 Linux Docker
linux 上安装kapacitor
linux 上安装kapacitor
341 3
|
机器学习/深度学习 并行计算 算法框架/工具
Anaconda+Cuda+Cudnn+Pytorch(GPU版)+Pycharm+Win11深度学习环境配置
Anaconda+Cuda+Cudnn+Pytorch(GPU版)+Pycharm+Win11深度学习环境配置