欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hashMap

程序员文章站 2022-06-04 19:17:37
...
package hashMap;
import java.util.*;
import java.util.Map.Entry;
//map 是一种键值对 ,不能有重复的键 因此用set 作为其key最适合了
public class HashMapTest
{
	public static <K, V> void main(String[] args)
	{
//		HashMap hm=new HashMap();
//		hm.put(1,"123");
//		hm.put(2,"456");
//		hm.put(3,"123");
//		hm.put(4,"456");
//		hm.put(5,"123");
//		hm.put(6,"456");
//		System.out.println(hm.get(1));
//		System.out.println(hm);
//		//键试图操作
//		Set s=hm.keySet();
//		s.remove(1);
//		System.out.println(hm);
//		//值视图操作
//		Collection col=hm.values();
//		//col.add("789");
//		col.remove("456");
//		System.out.println(hm);
//		//映射集合
//		Set keyvalue=hm.entrySet();
//		for(Object o:key_value)
//		{
//			Entry e=(Entry) o;
//			e.setValue("test");
//			System.out.println(e.getKey()+"------"+e.getValue());
//		}
		//hashMap与hashtable得区别:1.hashMap可以有空键,空值,hashtabe不可以有
		//2.hashmap是线程不安全的,hashitable是线程安全的(单线程使用hashMap多线程使用hashtable)
		//3.hashMap的效率高
		
		
		
		
		
		HashMap<Integer, Manager> hm=new HashMap<Integer, Manager>();
		Manager m1=new Manager(123,"郑云飞",22);
		Manager m2=new Manager(456,"xuanzi",18);
		hm.put(123, m1);
		hm.put(456, m2);
		if(hm.containsKey(123))
		{
			System.out.println("有此人");
			Manager mm=m1;
			System.out.println(mm);
		}
		else
		{
			System.out.println("无此人");
		}
		
		//遍历HashMap中的key何value的值
		Iterator<Integer> it=hm.keySet().iterator();	
		while(it.hasNext())
		{
			//取出key
			Integer key=it.next();
			//通过key取出value
			Manager mm=hm.get(key);
			//输出信息
			System.out.println("编号:"+mm.getId());
			System.out.println("姓名:"+mm.getName());
			System.out.println("年龄:"+mm.getAge());
			
		}
		
		
		
		
		
		
		
		
		
	

	
	}
}

 

package hashMap;
class Manager
{
	private int id;
	private String name;
	private int age;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Manager(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public Manager(int id, String name)
	{
		super();
		this.id = id;
		this.name = name;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "编号:"+id+"姓名:"+name+"年龄:"+age;
	}
	public static int test(int id,String name,int age)
	{
	   System.out.println("编号:"+id+"姓名:"+name+"年龄:"+age);
	   return age;
	}
	
}

 

package hashMap;

import java.io.FileInputStream;
import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;
class MyReflect 
{
	//依据参数返回类对象 参数:
	public static Object getObject(String[] args,Class cl) throws Exception
	{
		int con_Args_Count = Integer.parseInt(args[1]);//构造函数参数个数
		Class[] con_Class = new Class[con_Args_Count];//形参列表
		Object[] con_Object = new Object[con_Args_Count];//实参列表
		Object o = null;//获取对象的实例
		for(int i=0;i<con_Class.length;i++)
		{
			if(args[2+i].equals("int"))
			{
				con_Class[i] = int.class;
				con_Object[i] = Integer.parseInt(args[2+con_Args_Count+i]);
			}
			if(args[2+i].equals("String"))
			{
				con_Class[i] = String.class;
				con_Object[i] = args[2+con_Args_Count+i];
			}
			if(args[2+i].equals("char"))
			{
				con_Class[i] = char.class;
				con_Object[i] = args[2+con_Args_Count+i].charAt(0);
			}
			if(args[2+i].equals("float"))
			{
				con_Class[i] = float.class;
				con_Object[i] = Float.parseFloat(args[2+con_Args_Count+i]);
			}
			if(args[2+i].equals("boolean"))
			{
				con_Class[i] = boolean.class;
				con_Object[i] = Boolean.parseBoolean(args[2+con_Args_Count+i]);
			}
		}
		Constructor con = cl.getDeclaredConstructor(con_Class);//获取class对象所表示的类或接口的指定构造方法
		o = con.newInstance(con_Object);//创建对象
		return o;
	}

	//依据参数执行方法 并返回结果 参数:
	public static Object invokeMethod(String[] args,Object o,Class cl) throws Exception
	{
		int con_Args_Count = Integer.parseInt(args[1]);
		int me_Args_Count = Integer.parseInt(args[3+2*con_Args_Count]);
		Class[] me_Class = new Class[me_Args_Count];
		Object[] me_Object = new Object[me_Args_Count];
		String me_Name = args[2+2*con_Args_Count];

		for(int i=0;i<me_Class.length;i++)
		{
			Object[] oo = turn(args[4+2*con_Args_Count+i],args[4+2*con_Args_Count+me_Args_Count+i]);
			me_Class[i] = (Class)oo[0];
			me_Object[i] = oo[1];
		}
		Method m = cl.getDeclaredMethod(me_Name,me_Class);
		Object result = m.invoke(o,me_Object);
		return result;
	}

	public static void main(String[] args) throws Exception
	{
		 Properties p=new Properties();
		 p.load(new FileInputStream("test.ini"));
		 Enumeration e=p.propertyNames();
		 ArrayList al=new ArrayList();
		 while(e.hasMoreElements())
		 {
			String key=(String) e.nextElement();
			al.add(key);
		 }
		Collections.sort(al);
		
	}

	public static Object[] turn(String cs,String os)
	{
		Object[] o = new Object[2];
		if(cs.equals("int"))
		{
			o[0] = int.class;
			o[1] = Integer.parseInt(os);
		}
		if(cs.equals("String"))
		{
			o[0] = String.class;
			o[1] = os;
		}
		if(cs.equals("char"))
		{
			o[0] = char.class;
package hashMap;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Properties;

public class PropertiesTest
{
	 public static void main(String[] args) throws FileNotFoundException, IOException
	{
		 Properties p=new Properties();
	//Properties p=System.getProperties();
//	System.out.println("当前系统:"+p.getProperty("os.name"));
//	System.out.println("当前系统版本:"+p.getProperty("os.version"));
//	System.out.println("当前java版本:"+p.getProperty("java.version"));
//	System.out.println("用户名:"+p.getProperty("user.name"));
//	
		p.load(new FileInputStream("test.ini"));
		//枚举器
		Enumeration e=p.propertyNames();
		while(e.hasMoreElements())
		{	
		  String key=(String) e.nextElement();
		  //为了能读中文()
		  System.out.println(key+"=="+new String(p.getProperty(key).getBytes("latin1"),"gbk"));
		}
    }
}

	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
 
package hashMap;

import java.lang.reflect.*;
import java.io.*;
class ReflectTest
{
	public static void main(String[] args) throws Exception
	{
		//BankCard bc=new BankCard("12","123456","zhangsan",123);
		
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		//获取待产生对象的类的名称
		System.out.println("请输入类名:");
		String className = br.readLine();
		//加载该类至内存 并获得该类的自描述Class对象
		Class cl = Class.forName(className);
		//通过自描述对象产生对象(假如该类有无参构造)
		Object o = cl.newInstance();
		System.out.println(o.getClass().getName());
		//获得待执行方法名
		System.out.println("请输入方法名:");
		String methodName = br.readLine();
		//通过类的自描述对象获得方法的描述对象
		if(methodName.equals("welcome"))
		{
		  Method m = cl.getMethod(methodName);
		  m.invoke(o);
		}
		//通过方法的自描述对象执行该方法
		//System.out.println("对象m:"+m);
		else if(methodName.equals("insertCard"))
		{
			Method m=cl.getMethod(methodName,BankCard.class);
		    m.invoke(o,bc);
			
		}
		/**
		*调用主函数
		*
		**/
		else if (methodName.equals("main"))
		{
			Method m = cl.getMethod(methodName,new Class[]{String[].class});
		    m.invoke(o,(Object)new String[]{"",""});
		}
		else if(methodName.equals("test"))
		{
			Method m=cl.getMethod(methodName,new Class[]{int.class,int.class});
			m.invoke(o,5,6);
		}
		//获得一个类的所有公共非构造方法
		System.out.println("All public method");
		Method[] mo = cl.getMethods();
		for(Method mm:mo)
		{
			System.out.println(mm);
			
		}
		System.out.println("All method:");
		//获得一个类中所有非构造方法
		Method[] me = cl.getDeclaredMethods();
		for(Method mm:me)
		{
			System.out.println(mm);
		}

		//所有的公共构造
		System.out.println("公共构造:");
		Constructor[] c = cl.getConstructors();
		for(Constructor cc:c)
		{
			System.out.println(cc);
		}

		//所有构造
		System.out.println("所有构造:");
		Constructor[] cs = cl.getDeclaredConstructors();
		for(Constructor cs1:cs)
		{
			System.out.println(cs1);
		}
}
};


class Test
{
	int a,b;
	Test(int a,int b)
	{
		this.a=a;
		this.b=b;
	}
	public void test(int a,int b)
	{
		a=9;b=8;
		System.out.println("a="+a+"b="+b);
		//System.exit(0);
	}
	public void test(int a)
	{
		a=10;
		System.out.println(a);
		System.exit(0);
	}
};
  o[1] = os.charAt(0); } if(cs.equals("float")) { o[0] = float.class; o[1] = Float.parseFloat(os); } if(cs.equals("boolean")) { o[0] = boolean.class; o[1] = Boolean.parseBoolean(os); } return o; } }

 

相关标签: OS OO 多线程