马士兵J2SE-第七章-容器

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器镜像服务 ACR,镜像仓库100个 不限时长
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
简介:
 
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

 

 

 

 

 

 

 

 


 



 

目录
相关文章
|
2月前
|
Kubernetes Linux 虚拟化
一文详解容器技术简介和基本原理
本文全面阐述了容器技术的发展历程、关键技术、架构和当前的行业生态,特别是容器技术在云环境中的应用和演进。
|
4月前
|
Java 应用服务中间件 持续交付
Java面试题:简述Docker等容器化技术的原理及其在Java应用部署中的作用。
Java面试题:简述Docker等容器化技术的原理及其在Java应用部署中的作用。
71 0
|
6月前
|
前端开发 JavaScript 虚拟化
第四章(原理篇) 前端容器技术
第四章(原理篇) 前端容器技术
|
6月前
|
Java 数据库连接 Spring
K8S+Docker理论与实践深度集成java面试jvm原理
K8S+Docker理论与实践深度集成java面试jvm原理
|
6月前
|
存储 安全 算法
Java知识面试题复习(六)集合容器概述
Java知识面试题复习(六)集合容器概述
56 0
|
存储 搜索推荐 Java
《JavaSE-第六章》之容器数组
《JavaSE-第六章》之容器数组
124 0
|
算法 程序员 C++
序言 容器
序言 容器
75 0
|
存储 Linux Docker
语言流氓子跟你聊3分钟容器核心概念cgroup
还是容器,之前咱们简单聊了一下namespace,可以把它想象成用来存储进程固定参数的盒子。如果两个进程希望彼此更进一步了解对方,那就把他们的参数放在一个盒子里,仅此而已。
|
人工智能 Kubernetes Cloud Native
阿里又一个“逆天”容器框架!这本Kubernetes进阶手册简直太全了
容器技术这样一个新生事物,完全重塑了整个云计算市场的形态。在这个市场里,不仅有 Google、Microsoft 等技术巨擘们厮杀至今,更有无数的国内外创业公司前仆后继。而在国内,甚至连以前对开源基础设施领域涉足不多的 BAT、蚂蚁、滴滴这样的巨头们,也都从 AI、云计算、微服务、基础设施等维度多管齐下,争相把容器和 Kubernetes 项目树立为战略重心之一。
|
Kubernetes Cloud Native Linux
容器和K8S基础概念(上)| 学习笔记
快速学习容器和K8S基础概念(上)
容器和K8S基础概念(上)| 学习笔记

相关实验场景

更多