马士兵J2SE-第七章-容器

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介:
 
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

 

 

 

 

 

 

 

 


 



 

目录
相关文章
|
8月前
|
存储 搜索推荐 Java
《JavaSE-第六章》之容器数组
《JavaSE-第六章》之容器数组
|
8月前
|
存储 算法 C++
【C++】C++ 基础进阶【四】STL 容器基础
STL 标准模板库 简介 容器基础
51 0
【C++】C++ 基础进阶【四】STL 容器基础
|
10月前
|
存储 算法 C++
【C++知识点】STL 容器总结)(一)
【C++知识点】STL 容器总结(一)
93 0
|
10月前
|
算法 程序员 C++
序言 容器
序言 容器
42 0
|
11月前
|
存储 Linux Docker
语言流氓子跟你聊3分钟容器核心概念cgroup
还是容器,之前咱们简单聊了一下namespace,可以把它想象成用来存储进程固定参数的盒子。如果两个进程希望彼此更进一步了解对方,那就把他们的参数放在一个盒子里,仅此而已。
|
存储 Kubernetes 负载均衡
容器和K8S基础概念(下)| 学习笔记
快速学习容器和K8S基础概念(下)
256 0
容器和K8S基础概念(下)| 学习笔记
|
Kubernetes Cloud Native Linux
容器和K8S基础概念(上)| 学习笔记
快速学习容器和K8S基础概念(上)
174 0
容器和K8S基础概念(上)| 学习笔记
|
Kubernetes Cloud Native 安全
从零开始入门 K8s | 理解 RuntimeClass 与使用多容器运行时
本文整理自《CNCF x Alibaba 云原生技术公开课》第 30 讲,点击直达课程页面。关注“阿里巴巴云原生”公众号,回复关键词“入门”,即可下载从零入门 K8s 系列文章 PPT。
从零开始入门 K8s | 理解 RuntimeClass 与使用多容器运行时
|
容器 Perl Kubernetes
从零开始入门 K8s| 详解 Pod 及容器设计模式
作者|张磊 阿里云容器平台高级技术专家,CNCF 官方大使 一、为什么需要 Pod 容器的基本概念 我们知道 Pod 是 Kubernetes 项目里面一个非常重要的概念,也是非常重要的一个原子调度单位,但是为什么我们会需要这样一个概念呢?在使用容器 Docker 的时候,也没有这个说法。
|
容器 Docker Go
从零开始入门 K8s | 详解 K8s 容器基本概念
作者| 阿里巴巴高级开发工程师 傅伟 一、容器与镜像 什么是容器? 在介绍容器的具体概念之前,先简单回顾一下操作系统是如何管理进程的。 首先,当我们登录到操作系统之后,可以通过 ps 等操作看到各式各样的进程,这些进程包括系统自带的服务和用户的应用进程。
17276 0