- HashMap<Integer , String> map=new HashMap<Integer, String>();
- map.put(1, "alice");
- map.put(2, "bob");
- map.put(3, "lily");
-
- for(Entry<Integer, String> entry:map.entrySet()){
- System.out.println(entry);
- }
-
- for(Integer key:map.keySet()){
- System.out.println(key+" = "+map.get(key));
- }
-
- for(String value:map.values()){
- System.out.println(value);
- }
泛型 的好处:
1. 把运行时出现 的问题提早至了编译时。
2. 避免了无谓的强制类型转换。
注意: 在泛型中没有多态的概念,两边的数据必需要一致。 或者是只写一边 的泛型类型。
.net
推荐使用: 两边的数据类型都写上一致的。设计
泛型:对象
- import java.util.ArrayList;
-
- public class demo1 {
- public static void main(String[] args) {
- ArrayList<String> list=new ArrayList<String>();
- list.add("aa");
- list.add("bb");
-
- for (int i = 0; i < list.size(); i++) {
- String str=list.get(i);
- System.out.println(str.toUpperCase());
- }
- }
- }
泛型方法:
当函数中使用了一个不明确的数据类型,那么在函数上就能够进行泛型的定义。
public <泛型的声明> 返回值类型 函数名( 泛型 变量名 ){
}
注意:
1. 在方法上的自定义泛型的具体数据类型是调用该方法的时候传入实参的时候肯定的。
2. 自定义泛型使用的标识符只要符合标识符的命名规则便可。
- public class demo2 {
- public static void main(String[] args) {
- Integer i=print(12);
- String str=print("abc");
-
-
- }
- public static <T> T print(T o){
- return o;
- }
- }
泛型类:
泛型类的定义格式:
class 类名<声明自定义的泛型>{
}
注意的事项:
1. 在类上自定义的泛型的具体数据类型是在建立对象的时候指定的。
2. 在类上自定义了泛型,若是建立该类的对象时没有指定泛型的具体类型,那么默认是Object类型。
- class Father<T>{
- private T t;
- public Father() {
-
- }
- public Father(T t){
- super();
- this.t=t;
- }
- public void setT(T t) {
- this.t = t;
- }
- public T getT() {
- return t;
- }
- }
- class Son1<T> extends Father<T>{
-
- }
- class Son2 extends Father<String>{
-
- }
-
- public class demo3 {
- public static void main(String[] args) {
-
-
- Father<String> f1=new Father<String>("jack");
- System.out.println(f1.getT());
- Father<Integer> f2=new Father<Integer>(20);
- System.out.println(f2.getT());
-
-
- }
- }
泛型接口:
泛型接口的定义格式:
interface 接口名<声明自定义的泛型>{
}
在接口上自定义泛型要注意的事项:
1. 在接口上自定义泛型的具体数据类型是在实现该接口的时候指定的。
2. 若是一个接口自定义了泛型,在实现该接口的时候没有指定具体的数据类型,那么默认是Object数据类型。
- interface Inter<T>{
- void print(T t);
- }
- class myInter<T> implements Inter<T>{
- @Override
- public void print(T t) {
-
- System.out.println("myprint:"+t);
- }
- }
- class myInter2 implements Inter<String>{
- @Override
- public void print(String t) {
-
- System.out.println("myprint2:"+t);
- }
- }
- public class demo4 {
- public static void main(String[] args) {
- myInter<String> str=new myInter<String>();
- str.print("泛型");
- myInter2 str2=new myInter2();
- str2.print("只能传字符串");
- }
- }
1、List遍历
Java中List遍历有三种方法来遍历泛型,主要为:
1.for循环遍历
2.iterator遍历
3.foreach遍历
- package com.gmail.lsgjzhuwei;
-
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
-
- import org.junit.Test;
-
- public class test {
-
-
- @Test
- public void test1() {
- List<String> li = new ArrayList<String>();
-
- li.add("agad");
- li.add("1234");
- li.add("good");
-
- for (int i = 0; i < li.size(); i++) {
- String s = li.get(i);
- System.out.println(s);
- }
- System.out.println("-------------------");
- }
-
-
- @Test
- public void test2() {
- List<String> li = new ArrayList<String>();
-
- li.add("agad");
- li.add("1234");
- li.add("good");
-
- Iterator iterator = li.iterator();
- while (iterator.hasNext()) {
- String s = (String) iterator.next();
- System.out.println(s);
- }
- System.out.println("-------------------");
- }
-
-
- @Test
- public void test3() {
- List<String> li = new ArrayList<String>();
-
- li.add("agad");
- li.add("1234");
- li.add("good");
-
- foreach (String s : li) {
- System.out.println(s);
- }
-
- System.out.println("-------------------");
- }
- }
2、Map遍历
Map遍历只要有两种方法:
1.经过Map的KeySet进行遍历
2.经过Map的EntrySet进行遍历
- @Test
- public void test4() {
- Map<Integer, String> map = new HashMap<Integer, String>();
- map.put(1, "good");
- map.put(2, "morning");
-
- Set<Integer> set = map.keySet();
- for (Integer ky : set) {
- System.out.println(ky + ":" + map.get(ky));
- }
-
- System.out.println("-------------------");
- }
-
-
- @Test
- public void test5() {
- Map<Integer, String> map = new HashMap<Integer, String>();
- map.put(1, "good");
- map.put(2, "morning");
-
- Set<Map.Entry<Integer, String>> set = map.entrySet();
- for (Entry<Integer, String> entry : set) {
- System.out.println(entry.getKey() + ":" + entry.getValue());
- }
-
- System.out.println("-------------------");
- }
单例设计模式分为懒人模式和饿人模式,若是一个枚举中只有一个用例,它就至关于一个单例设计模式
-
-
class B {
-
-
private B() {
-
}
-
-
private static B b = new B();
-
-
public static B getInstance() {
-
return b;
-
}
-
}
-
-
-
class C {
-
-
private C() {
-
}
-
-
private static C c;
-
-
public static C getInstance() {
-
if(c == null){
-
c = new C();
-
}
-
return c;
-
}
-
}