package jihekuangjia;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class hashcode {
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<String> p=new HashSet<>();
p.add("a");
p.add("b");
p.add("c");
System.out.println(p);
Set<String> q=new HashSet<>();
q.add("1");
q.addAll(p);
System.out.println(q);
for(Iterator<String> it=q.iterator();it.hasNext();)
{
if("c".equals(it.next()))
{
it.remove();
}
}System.out.println(q);
}
}
package jihekuangjia;
import java.util.HashSet;
import java.util.Set;
class student{
int num;
String name;
int age;
public student(int n,String nam,int a)
{
this.num=n;
this.name=nam;
this.age=a;
}
@Override
//可以使用快捷键alt+shift+s自动生成hashCode()和equals(Object obj)
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + num;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
student other = (student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (num != other.num)
return false;
return true;
}
}
public class hash {
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<student> p=new HashSet<>();
p.add(new student(1, "df", 22));
p.add(new student(1, "dfg", 22));
p.add(new student(1, "dfl", 22));
System.out.println(p.size());
System.out.println(p.toString());
}
}
//自然排序
package jihekuangjia;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
class person implements Comparable<person>
{
private int age;
private String name;
public person(int a,String n) {
// TODO Auto-generated constructor stub
this.age=a;
this.name=n;
}
@Override
public int compareTo(person o) {
if(this.age>o.age)
return 1;
if(this.age<o.age)
return -1;
return 0;
}
public String toString() {
return this.name+this.age;
}
}
public class ziranpaixu {
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<person> p=new TreeSet<>();
p.add(new person(12, "西门"));
p.add(new person(15, "西门庆"));
p.add(new person(8, "西门飞雪"));
p.add(new person(99, "东风快递,使命必达"));
System.out.println(p);
}
}
//定制排序
package jihekuangjia;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
class people{
int age;
String name;
public people(int a,String n)
{
this.age=a;
this.name=n;
}
public String toString() {
return this.name+" "+this.age;
}
}
class paixu implements Comparator<people>
{
@Override
public int compare(people o1, people o2) {
if (o1.name.length()>o2.name.length())
return 1;
if(o1.name.length()<o2.name.length())
return -1;
return 0;
}
}
public class dingzhipaixu {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
Set<people> p=new TreeSet<>(new paixu());
p.add(new people(12, "fdjkfdsf"));
p.add(new people(12, "fdjkfdsffdsfdfd"));
p.add(new people(12, "fdjkfdsffdsf"));
p.add(new people(12, "fdjkfdsffds"));
System.out.println(p);*/
/*匿名内部类写法*/
Set<people> p=new TreeSet<>(new Comparator<people>() {
public int compare(people o1, people o2) {
if (o1.name.length()>o2.name.length())
return 1;
if(o1.name.length()<o2.name.length())
return -1;
return 0;
}
});
p.add(new people(12, "fdjkfdsf"));
p.add(new people(12, "fdjkfdsffdsfdfd"));
p.add(new people(12, "fdjkfdsffdsf"));
p.add(new people(12, "fdjkfdsffds"));
System.out.println(p);
}
}
package jihekuangjia;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class map {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, Object> p=new HashMap<>();
p.put("map1", 1);
p.put("map2", 2);
p.put("map3", 3);
p.put("map4", 4);
p.put("map5", 5);
System.out.println(p);
p.remove("map5");
System.out.println(p);
System.out.println("----------------------");
Set<String> keys=p.keySet();
for(String key:keys)
{
System.out.println(key+"->"+p.get(key));
}
System.out.println("----------------------");
Collection<Object> k=p.values();
for(Object c:k)
{
System.out.println(c);
}
System.out.println("----------------------");
Set<Entry<String, Object>> j=p.entrySet();
for(Entry<String, Object> i:j)
{
System.out.println(i.getKey()+"--->"+i.getValue());
}
}
}
Map的操作方法
计算字符出现的代码次数:
package jihekuangjia;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
public class count {
public static void main(String[] args) {
// TODO Auto-generated method stub
String array="fdsjfhdsjkfhdfuierekdndaterelfdls";
// Map<Character, Integer> p=new HashMap<>();//无序
// Map<Character, Integer> p=new LinkedHashMap<>();//有序
Map<Character, Integer> p=new TreeMap<>();//自然排序
char []arr=array.toCharArray();
for(char ele:arr)
{
if(p.containsKey(ele))
{
p.put(ele,p. get(ele)+1);
}
else {
p.put(ele, 1);
}
}
System.out.println(p);
}
}
package jihekuangjia;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class zonghe {
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<String> p=new HashSet<>();
p.add("ren");
p.add("ren1");
p.add("ren2");
Set<String> p1=new HashSet<>();
p1.add("ta1");
p1.add("ta2");
p1.add("ta3");
Set<String> p2=new HashSet<>();
p2.add("shu1");
p2.add("shu2");
p2.add("shu3");
Map<String, Set<String>> q=new HashMap<>();
q.put("软1",p);
q.put("软2", p1);
Map<String, Set<String>> q1=new HashMap<>();
q1.put("数统", p2);
List<Map<String, Set<String>>> f=new ArrayList<>();
f.add(q);
f.add(q1);
System.out.println(f);
System.out.println("=====================");
List<String> h=Arrays.asList("a","b","c");
System.out.println(h);
}
}