Java_day07

<pre name="code" class="html">//
// MATLAB Compiler: 4.18 (R2012b)
// Date: Wed May 14 15:27:44 2014
// Arguments: "-B" "macro_default" "-t" "-W" "cpplib:MyAdd" "-T" "link:lib"
// "-h" "libmmfile.mlib" "MyAdd.m" 
//

----------
/*
Collection:
  List:存储的数据是有序的,存储的对象是可以重复的
 ArrayList:底层使用的数据结构是数组,查找速度快,增删速度慢,线程不安全的
LinkedList:底层使用的数据结构是链表,查找速度慢,增删速度快,线程不安全的
 Vector:底层使用的数据结构是数组,查找速度快,增删速度慢,线程安全的
 Set:存储的数据是无序(集合中对象的顺序和存储的顺序不一致)的,存储的对象是不可以重复的
 HashSet:底层使用的数据结构是哈希表,线程不同步的
              保证对象唯一的原理:int hashCode()   boolean equals(Object obj)
			                     在添加对象时先用对象的哈希值和集合中已有对象的哈希值进行比较
								 如果哈希值都不相同,那么就直接把该对象加入集合,如果出现哈希值
								 相同的,那么再调用 equals方法,只有equals方法返回true才认为是相同
								 的对象,不加入集合

 哈希值相同不一定是同一个对象

TreeSet:底层使用的数据结构是二叉树,线程不同步的,会对存入集合的对象进行排序

排序的第一种方式:集合中对象所属的类实现Comparable接口中的 int compareTo(Object obj)方法
保证对象唯一:compareTo()方法的返回值为0时,认为该对象已经存在,不加入集合
当排序的主要条件相同时,需要指定排序的次要条件
排序的第二种方式:集合中对象的排序方式不是我们需要的,自定义比较方式
			                    定义一个类实现Comparator 接口中的 int compare(Object obj1,Object obj2)方法							
*/

```
import java.util.*;
public class Person
{
	public int hashCode()
	{
		return 66;
	}
	public boolean equals(Object obj)
	{
		return true;
	}
}
class Demo2 
{
	public static void main(String[] args) 
	{
		HashSet hs = new HashSet();
		//在使用add方法添加对象时已经保证了对象的唯一
		/*hs.add("java01");
		hs.add("java02");
		hs.add("java03");
		hs.add("java02");*/
		
		hs.add(new Person());
		hs.add(new Person());
		hs.add(new Person());
		sop(hs);
	}

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

```




----------

```
import java.util.*;
class Student
{
	private String name;
	private int age;

	Student(){}
	Student(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
    public int hashCode()
	{
		return name.hashCode()+age*36;
	}
    public boolean equals(Object obj)
	{
		if(!(obj instanceof Student))
			throw new RuntimeException("类型不对");
		Student stu = (Student)obj;
		return this.name.equals(stu.name)&&this.age==stu.age;
	}


	public String getName()
	{
		return this.name;
	}
	public int getAge()
	{
		return this.age;
	}
	public String toString()
	{
		return name+","+age;
	}
}
class Demo3 
{
	public static void main(String[] args) 
	{
		HashSet hs = new HashSet();
        //姓名年龄相同的认为是同一个对象,不能加入集合
		hs.add(new Student("zhangsan",23));
		hs.add(new Student("lisi",21));
		hs.add(new Student("wangwu",26));
		hs.add(new Student("zhangsan",23));

		sop(hs.contains(new Student("lisi",21)));//也是依据int hashCode()   boolean equals(Object obj)

		hs.remove(new Student("lisi",21));//也是依据int hashCode()   boolean equals(Object obj)

		sop(hs);
	}

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

```


----------

```
import java.util.*;
class Demo4 
{
	public static void main(String[] args) 
	{
		TreeSet ts = new TreeSet();
		//在添加时就已经进行排序了
		//因为String实现了Comparable接口中的 int compareTo(Object obj)方法
		//集合是依据字符串中的这个方法int compareTo(Object obj)来排序的
		ts.add("zwoeiuroie");
		ts.add("abcedf");// "abcedf".compareTo("zwoeiuroie")
		ts.add("wowieur");//"wowieur".compareTo("zwoeiuroie")  "wowieur".compareTo("abcedf")

		sop(ts);
	}

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

```


----------

```
import java.util.*;
class Student implements Comparable
{
	private String name;
	private int age;

	Student(){}
	Student(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
    
	public int compareTo(Object obj)
	{
	    if(!(obj instanceof Student))
			throw new ClassCastException();
		Student stu = (Student)obj;
        int num = this.age-stu.age;//根据年龄从小到大排序

		return num==0?this.name.compareTo(stu.name):num;
        
	}
	public String getName()
	{
		return this.name;
	}
	public int getAge()
	{
		return this.age;
	}
	public String toString()
	{
		return name+","+age;
	}
}
//自定一个排序方式
class ComByName implements Comparator
{
	public int compare(Object obj1,Object obj2)
	{
		if(!(obj1 instanceof Student))
			throw new ClassCastException("类型转换异常");
		if(!(obj2 instanceof Student))
			throw new ClassCastException("类型转换异常");

		Student stu1 = (Student)obj1;
		Student stu2 = (Student)obj2;

        int num = stu1.getName().compareTo(stu2.getName());
		return num==0?stu1.getAge()-stu2.getAge():num;
	}
}
class Demo5 
{
	public static void main(String[] args) 
	{   
		ComByName cbn = new ComByName();//创建自定义的排序方式对象
		TreeSet ts = new TreeSet(cbn);//再创建集合对象时就得确定排序方式
		//自定义的排序方式优先被使用
		ts.add(new Student("zhangsan",23));
		ts.add(new Student("lisi",21));//new Student("lisi",21).compareTo(new Student("zhangsan",23))
		ts.add(new Student("wangwu",26));
		ts.add(new Student("pengfei",20));
		ts.add(new Student("zhaosi",23));

		sop(ts);
	}

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

```


----------

```
/*
把字符串按照从短到长排序:
字符串所属的类String已经实现了Comparable接口的compareTo方法,但是是按照字符串大小比较的,
不符合我们的需求,所以使用自定义的排序方式
*/
import java.util.*;
class ComByLength implements Comparator
{
	public int compare(Object obj1,Object obj2)
	{
		if(!(obj1 instanceof String))
			throw new ClassCastException("类型转换异常");
		if(!(obj2 instanceof String))
			throw new ClassCastException("类型转换异常");

		String str1 = (String)obj1;
		String str2 = (String)obj2;

		int num = new Integer(str1.length()).compareTo(new Integer(str2.length()));
		return num ==0?str1.compareTo(str2):num;
	}
}
class Demo6 
{
	public static void main(String[] args) 
	{
		ComByLength cbl = new  ComByLength();
		TreeSet ts = new TreeSet(cbl);
		ts.add("oeiuroieurowerer");
		ts.add("ldkjfglkdfjgfgfgfdgdfgdfgdfgdfg");
		ts.add("iuerie");
		ts.add("eiueiu");

		sop(ts);
	}

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

```


----------

```
//把字符串"23 45 3 67 21"中的数据从小到大排序
import java.util.*;
class Demo7 
{
	public static void main(String[] args) 
	{
		String ss = "23 45 3 67 21";
		String[] arr = ss.split(" ");

		TreeSet ts = new TreeSet();
		for(int i=0;i<arr.length;i++)
		{
			ts.add(Integer.parseInt(arr[i]));
		}

        sop(ts);
	}

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

```


----------

```
/*
泛型:通过<引用数据类型>接收一种引用数据类型,作用是在编译时期用该类型检查集合中的对象是否是
     该类型的,如果不是编译不通过,从而把运行时期的问题转移到了编译时期,提高了程序的安全性

不用类型转换了

泛型擦除:泛型是在编译时期使用的,编译完之后的class中是不存在泛型的
*/
import java.util.*;
class Demo8 
{
	public static void main(String[] args) 
	{
		ArrayList<String> list = new ArrayList<String>();
		list.add("java01");
		list.add("java02");
		list.add("java03");
		//list.add(66);

		Iterator<String> ite = list.iterator();
		while(ite.hasNext())
		{
			String obj = ite.next();
			//String ss = (String)obj;
			sop(obj.toUpperCase());
		}
	}

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

```


----------

```
//泛型定义在类上
class Student
{
}
class Worker
{
}
class Tool //没使用泛型
{
	Object obj;
	public void setObject(Object obj)
	{
		this.obj = obj;
	}
	public Object getObject()
	{
		return obj;
	}
}
class Tools<E> //泛型类
{
	E obj;
	public void setObject(E obj)
	{
		this.obj = obj;
	}
	public E getObject()
	{
		return obj;
	}
}
class Demo9 
{
	public static void main(String[] args) 
	{   /*
		Tool tool = new Tool();
		tool.setObject(new Student());
		Object obj = tool.getObject();
		Worker w = (Worker)obj;*/

		Tools<Student> tools = new Tools<Student>();
		tools.setObject(new Student());
		Student w = tools.getObject();
	}
}

```


----------

```
//泛型定义在方法上
class Test<E>
{
	//当方法的参数的类型只有在类上的泛型确定了才能确定时,就需要使用泛型
	public void show(E ss)
	{
		System.out.println(ss);
	}
	//方法的参数是任意类型
	public <T> void fun(T obj)//方法自己使用泛型
	{
		System.out.println(obj);
	}
	//静态在进内存的时候没有对象,所以只能自己使用泛型
	public static <E> void function(E e)  
	{
		System.out.println(e);
	}
}
class Demo10 
{
	public static void main(String[] args) 
	{
		Test<String>  t = new Test<String>();
		t.show("hello");
		//t.show(66);
		t.fun("world");
		t.fun(78);
	}
}

```


----------

```
//在接口上定义泛型
interface inter<E>
{
	public abstract void show(E e);
}
class Test implements inter<String>
{
	public  void show(String e)
	{
		System.out.println(e);
	}
}
class Demo<E> implements inter<E>
{
    public void show(E e)
	{
		System.out.println(e);
	}
}
class Demo11 
{
	public static void main(String[] args) 
	{
		Demo<String> demo = new Demo<String>();
		demo.show("hehe");
	}
}

```


----------

```
//通配符: ?
import java.util.*;
class Demo12 
{
	public static void main(String[] args) 
	{
		ArrayList<String> list1 = new ArrayList<String>();
		list1.add("hello1");
		list1.add("hello2");
		list1.add("hello3");

        dieDai2(list1);

		ArrayList<Integer> list2 = new ArrayList<Integer>();
		list2.add(66);
		list2.add(77);
		list2.add(88);

		 dieDai2(list2);
	}
	public static void dieDai2(Collection<?> list)
	{
		Iterator<?> ite = list.iterator();
		while(ite.hasNext())
		{
			System.out.println(ite.next());
		}

	}

	public static <E> void dieDai(ArrayList<E> list)
	{
		Iterator<E> ite = list.iterator();
		while(ite.hasNext())
		{
			E obj = ite.next();
			System.out.println(obj);
		}
	}
}

```


----------

```
/*
泛型限定:  ? extends E :可以接收E类型或E的子类型,确定了上限
           ? super E:可以接收E类型或E的父类型,确定了下限
*/
import java.util.*;
class Person
{
	private String name;
	private int age;

	Person(){}
	Person(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	public String toString()
	{
		return name+","+age;
	}
	public String getName()
	{
		return name;
	}
	public int getAge()
	{
		return age;
	}
}
class Student extends Person
{
	Student(){}
	Student(String name,int age)
	{
		super(name,age);
	}
}
class Worker extends Person
{
	Worker(){}
	Worker(String name,int age)
	{
		super(name,age);
	}
}
class Demo13 
{
	public static void main(String[] args) 
	{
		ArrayList<Student> list1 = new ArrayList<Student>();
		list1.add(new Student("lisi",20));
		list1.add(new Student("zhaosi",23));
		list1.add(new Student("wangsi",26));

		dieDai(list1);

		ArrayList<Worker> list2 = new ArrayList<Worker>();
		list2.add(new Worker("lisi",20));
		list2.add(new Worker("zhaosi",23));
		list2.add(new Worker("wangsi",26));

		dieDai(list2);

	}

	public static void dieDai(ArrayList<? extends Person> list)
	{
		Iterator<? extends Person> ite = list.iterator();
		while(ite.hasNext())
		{
			Person ren = ite.next();
			System.out.println(ren);
		}
	}
}

```


----------

```
import java.util.*;
class Demo14 
{
	public static void main(String[] args) 
	{
		

		TreeSet<Student> ts1 = new TreeSet<Student>();
		ts1.add(new Student("lisi",25));
		ts1.add(new Student("zhaosi",20));
		ts1.add(new Student("wangsi",27));

		TreeSet<Worker> ts2 = new TreeSet<Worker>();
		ts2.add(new Worker("lisi",26));
		ts2.add(new Worker("zhaosi",26));
		ts2.add(new Worker("wangsi",26));

		//泛型上限的使用
		//TreeSet(Collection<? extends Person> c) 
		TreeSet<Person> t = new TreeSet<Person>(ts1);
		TreeSet<Person> t2 = new TreeSet<Person>(ts2);


		
	}
}

```


----------

```
//泛型下限的使用
/*class ComByAge implements Comparator<Student>
{
	int compare(Student s1,Student s2)
	{
		return s1.getAge()-s2.getAge();
	}
}
class ComByAge2 implements Comparator<Worker>
{
	int compare(Worker s1,Worker s2)
	{
		return s1.getAge()-s2.getAge();
	}
}*/
import java.util.*;
class ComByAge3 implements Comparator<Person>
{
	public int compare(Person s1,Person s2)
	{
		return s1.getAge()-s2.getAge();
	}
}

class Demo15 
{
	public static void main(String[] args) 
	{
		TreeSet<Student> ts1 = new TreeSet<Student>(new ComByAge3());
		ts1.add(new Student("lisi",25));
		ts1.add(new Student("zhaosi",20));
		ts1.add(new Student("wangsi",27));

		TreeSet<Worker> ts2 = new TreeSet<Worker>(new ComByAge3());
		ts2.add(new Worker("lisi",26));
		ts2.add(new Worker("zhaosi",26));
		ts2.add(new Worker("wangsi",26));


		//TreeSet(Comparator<? super E> comparator) 

	}
}

```


----------

```
/*
Map:一个单独的接口,存储的键值对,一对一对的存,键不能重复
   HashMap:底层使用的数据结构是哈希表,线程不安全的
   TreeMap:底层使用的数据结构是二叉树,线程不安全的


添加:
 V put(K key, V value) 
 void putAll(Map<? extends K,? extends V> m)
 
删除:
 V remove(Object key) 
 void clear() 
         
获取:
  V get(Object key) 

  Set<K> keySet() 
  Set<Map.Entry<K,V>> entrySet() 
  int size() 
  Collection<V> values() 
判断:
   boolean isEmpty() 
   boolean containsKey(Object key) 
   boolean containsValue(Object value) 

*/
import java.util.*;
class Demo16 
{
	public static void main(String[] args) 
	{
		HashMap<String,String>  map = new HashMap<>();

		//sop(map.put("001","zhangfei"));//null
		//sop(map.put("001","yangliu"));//键不能重复

		map.put("001","zhangfei");
		map.put("002","zhaosi");
		map.put("003","wangwu");

		//map.remove("001");//根据键来删除一对
		//sop(map);

		String value = map.get("002");//根据键获取值
		sop(value);

	    Collection col = map.values();//得到所有值的集合
		sop(col);

		sop(map.containsKey("003"));//判读是否包含某个键
		sop(map.containsValue("wangwu"));//判读是否包含某个值

		
	}
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

```


----------

```
/*
Map中没有迭代器,那么该如何遍历?
 1.Set<K> keySet(): 得到所有键的集合,存储到Set集合中并返回该集合,因为Set有迭代器,
                    每次迭代出来的是一个键,再根据键获取值


 2.Set<Map.Entry<K,V>> entrySet():得到每个键值对形成的映射关系类型的值,存到Set集合中,
                       因为Set有迭代器,每次迭代出来的是一个映射关系,从这个映射关系中既可以得到键
					   又可以得到值
					   映射关系是Map.Entry<K,V>类型的,Entry是定义在Map中的一个静态成员接口
					   Entry为什么定义在Map中,有了集合,有了集合中的键值对,才会存在映射关系
					   ,是对集合内部键值对形成的关系的描述,所以定义在Map内部

*/
import java.util.*;
class Demo17 
{
	public static void main(String[] args) 
	{
		HashMap<String,String> map = new HashMap<>();
		map.put("001","zhangfei");
		map.put("002","zhaosi");
		map.put("003","wangwu");

		Set<Map.Entry<String,String>> sets = map.entrySet();
		Iterator<Map.Entry<String,String>> ite = sets.iterator();
		while(ite.hasNext())
		{
			Map.Entry<String,String> en = ite.next();
			String key = en.getKey();
			String value = en.getValue();
			sop(key+"="+value);
		}

		/*Set<String> keys = map.keySet();
		Iterator<String> ite = keys.iterator();
		while(ite.hasNext())
		{
			String key = ite.next();
			String value = map.get(key);
			sop(key+"="+value);
		}*/

		
	}
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

```


----------

```
/*
什么时候使用map?
当存在映射关系时
比如每个学生都有一个住址
*/

import java.util.*;
class Student  implements Comparable<Student>
{
	private String name;
	private int age;

	Student(){}
	Student(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	public int compareTo(Student stu)
	{
		int num =  this.age-stu.age;
		return num==0?this.name.compareTo(stu.name):num;
	}
	public int hashCode()
	{
		return name.hashCode()+age*36;
	}
	public boolean equals(Object obj)
	{
		if(!(obj instanceof Student))
			throw new ClassCastException();
		Student stu = (Student)obj;
		return this.name.equals(stu.name)&& this.age==stu.age;
	}
	public String toString()
	{
		return name+","+age;
	}
	public String getName()
	{
		return name;
	}
	public int getAge()
	{
		return age;
	}
}
class Demo18 
{
	public static void main(String[] args) 
	{
		HashMap<Student,String> map = new HashMap<>();
        //保证键唯一的原理和HashSet一样
		map.put(new Student("lisi",23),"shanghai");
		map.put(new Student("zhaosi",22),"beijing");
		map.put(new Student("wangwu",20),"shenzhen");
		map.put(new Student("zhaosi",22),"guangzhou");

		Set<Student> keys = map.keySet();
		Iterator<Student> ite = keys.iterator();
		while(ite.hasNext())
		{
			Student stu = ite.next();
			String value = map.get(stu);
            sop(stu+"="+value);
		}

	}
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

``


----------

```
import java.util.*;
class Demo19 
{
	public static void main(String[] args) 
	{
		TreeMap<Student,String> map = new TreeMap<>();
        //根据键排序的,排序的原理和TreeSet一样
		map.put(new Student("lisi",23),"shanghai");
		map.put(new Student("zhaosi",22),"beijing");
		map.put(new Student("wangwu",20),"shenzhen");
		map.put(new Student("zhaosi",22),"guangzhou");

		Set<Map.Entry<Student,String>> ent = map.entrySet();
		Iterator<Map.Entry<Student,String>> ite = ent.iterator();
		while(ite.hasNext())
		{
			Map.Entry<Student,String> en = ite.next();
			Student key = en.getKey();
			String value = en.getValue();
			System.out.println(key+"="+value);
		}
	}
}

```


----------

```
/*
"java"  若干个学生

android 若干个学生

*/
import java.util.*;
class Demo20 
{
	public static void main(String[] args) 
	{
		HashMap<String,List<Student>> map = new HashMap<>();

		List<Student> list1 = new ArrayList<Student>();
		list1.add(new Student("lisi",23));
		list1.add(new Student("zhaosi",22));
		list1.add(new Student("wangsi",25));

		List<Student> list2 = new ArrayList<Student>();
		list2.add(new Student("lisi2",20));
		list2.add(new Student("zhaosi2",28));
		list2.add(new Student("wangsi2",23));

		map.put("java",list1);
		map.put("android",list2);

		Set<String> keys = map.keySet();
		Iterator<String> ite = keys.iterator();
		while(ite.hasNext())
		{
			String key = ite.next();
			System.out.println("班级名称:"+key);
			System.out.println("班级成员");
			List<Student> list = map.get(key);
			Iterator<Student> ites = list.iterator();
			while(ites.hasNext())
			{
				Student stu = ites.next();
                System.out.println(stu);
			}
		}





	}
}

```


----------

```
/*
练习:字符串"oeiproitwoeuraecde34535fgabc$#@#defg"
希望打印结果:a(2)b(1)c(2)

*/
import java.util.*;
class Demo21 
{
	public static void main(String[] args) 
	{
		String ss = "oeiproitwoeuraecde34535fgabc$#@#defg";

		TreeMap<Character,Integer> map = new TreeMap<>();

		char[] arr = ss.toCharArray();

		for(int i=0;i<arr.length;i++)
		{
			if(arr[i]>='a' && arr[i]<='z' || arr[i]>='A' && arr[i]<='Z')
			{
				Integer value = map.get(arr[i]);//把字符作为键,根据键获取值,若不存在返回null

				if(value==null)
					map.put(arr[i],1);
				else
				{
					value = value+1;
					map.put(arr[i],value);
				}
			}
		}

		StringBuilder sb = new StringBuilder();
		Set<Character> keys = map.keySet();
		Iterator<Character> ite = keys.iterator();
		while(ite.hasNext())
		{
			Character key = ite.next();
			Integer value = map.get(key);

			sb.append(key).append("(").append(value).append(")");
		}
		System.out.println(sb.toString());
		
	}
}

```


----------



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值