熟练使用Arrays-数组-ArrayList-HashMap等常用Java类型的方法

	//HashMap类常用函数
		Map map = new HashMap<Character, Integer>(); //char对应包装类是 Character,不用String
		for(int k=0;k<26;k++){
			map.put((char)(k+97), 0);
		}
		Set keys = map.keySet(); //键值集合
		System.out.println(keys);
		//[f, g, d, e, b, c, a, n, o, l, m, j, k, h, i, w, v, u, t, s, r, q, p, z, y, x]
		
		int cnt = (int) map.get('d');
		System.out.println(cnt);
		
		exist = map.containsKey('A');
		System.out.println(exist); //not eixst
		
		exist = map.containsValue(0);
		System.out.println(exist); //exist
		
		Collection res = map.values(); //Value集合
		
		map.remove('a'); 
		map.put('a',1); //两步,实现更新'a'的统计值
		
		map.size();
		map.isEmpty();
//		map.clear();
		
		
		//遍历键,通过键取值
		Set set = map.keySet();
		for (Object key : set) {
			System.out.println("键:"+key+"  值:"+map.get(key));
		}
		//遍历键集合
		Iterator it=map.keySet().iterator();
		while(it.hasNext()){
			System.out.println("键:"+it.next());
		}
		//遍历键值集合
		Iterator it2=map.entrySet().iterator();
		while(it2.hasNext()){
			System.out.println(it2.next());
		}


	//ArrayList类常用函数
		int[] input0 = {2,3,4,5,7,4,5};
		ArrayList out = new ArrayList(); 
		for(int i = 0; i< input0.length;i++){
			out.add(input0[i]);
		}
		System.out.println("res——"+out);//res——[2, 3, 4, 5, 7, 4, 5]
		System.out.println(out.subList(2, 4));//[4,5], 前开后闭的序号切片取值
		
		Set test = new HashSet(out); //以List为参数构造Set对象
		System.out.println(test.size());
		
		Set in = new HashSet(); //以Set为参数构造List对象
		in.add(3);in.add(1);in.add(4);
		List test2 = new ArrayList(in);
		System.out.println(test2.size());
		
		int pos = out.indexOf(5);
		if(pos == -1){//if(arr.contains(5))
			System.out.print("not exist");
		}else{
			System.out.println("exist,pos="+pos+",last_pos="+out.lastIndexOf(5)); //返回第一个出现的元素
		}
		Object[] out_ = out.toArray(); //ArrayList转换成Object数组,具体操作某元素再进行转型

//String中常用函数
		Integer.parseInt("10");
		String str = "hello-world";
	
		String[] strs = str.split("-"); 
		System.out.println(strs[0]+","+strs[1]);
		
		String _stra = str.concat("!");
		System.out.println(_stra+","+str);
		
		byte[] bytes = str.getBytes();
		char[] chars = str.toCharArray();
		
		int index = str.indexOf("world");
		System.out.println(index);
		
		boolean exist = str.endsWith("!");
		boolean first_ = str.startsWith("he");
		System.out.println(first_+","+exist);
		
		
		System.out.println("hellohello".indexOf("hello")+","+("hellohello".lastIndexOf("hello"))); //0,5
		

	//Arrays类常用函数
		int[] data = {2,3,4,4,4,4,5};
		List  arr = Arrays.asList(data); //将普通数组转成成List		
		
		System.out.println(arr);
//		int size = arr.size();
//		for(int k =0 ; k < size;k++){
//			System.out.println(arr.get(k));
//		}
		Arrays.sort(data); //Arrays.XXX(arr) 静态函数参数都是简单数组,不可以是List
		for(int item : data){
			System.out.println(item);
		}
		Arrays.binarySearch(data, 8);
		Arrays.copyOfRange(data, 3, 6);


你可能感兴趣的:(熟练使用Arrays-数组-ArrayList-HashMap等常用Java类型的方法)