package _20160831;
import java.util.*;
public class Candy {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Candyes> box =new ArrayList<Candyes>();
box.add(new Candyes( "红" ));
box.add(new Candyes( "白" ));
box.add(new Candyes( "紫" ));
box.add(new Candyes( "蓝" ));
box.add(new Candyes( "红" ));
box.add(new Candyes( "黑" ));
System.out.println(box.size());
System.out.println(box);
Map<Candyes,Integer> evercandy =new HashMap<Candyes,Integer>();
getMap(evercandy,box);
System.out.println(evercandy);
}
//找到每种糖 每种糖几个
private static Map<Candyes,Integer> getMap( Map<Candyes,Integer> evercandy, List<Candyes> box ){
Candyes o;
Integer value;
for (int i = 0; i < box.size(); i++) {
o=box.get(i);//每个糖果
value=getCount(o,box);//自自定义方法得到每个糖果的个数
evercandy.put(o, value);
}
return evercandy;
}
//自定义得到每个糖出现的次数
private static int getCount(Candyes o ,List<Candyes> box){
int index=-1;
int count=0;
while( (index=box.indexOf(o))!=-1 ){
count++;
box.remove(index) ;
}
return count;
}
}
class Candyes{
private String colour;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((colour == null) ? 0 : colour.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Candyes other = (Candyes) obj;
if (colour == null) {
if (other.colour != null)
return false;
} else if (!colour.equals(other.colour))
return false;
return true;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public Candyes(String colour) {
super();
this.colour = colour;
}
@Override
public String toString() {
return colour+"糖" ;
}
}
import java.util.*;
public class Candy {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Candyes> box =new ArrayList<Candyes>();
box.add(new Candyes( "红" ));
box.add(new Candyes( "白" ));
box.add(new Candyes( "紫" ));
box.add(new Candyes( "蓝" ));
box.add(new Candyes( "红" ));
box.add(new Candyes( "黑" ));
System.out.println(box.size());
System.out.println(box);
Map<Candyes,Integer> evercandy =new HashMap<Candyes,Integer>();
getMap(evercandy,box);
System.out.println(evercandy);
}
//找到每种糖 每种糖几个
private static Map<Candyes,Integer> getMap( Map<Candyes,Integer> evercandy, List<Candyes> box ){
Candyes o;
Integer value;
for (int i = 0; i < box.size(); i++) {
o=box.get(i);//每个糖果
value=getCount(o,box);//自自定义方法得到每个糖果的个数
evercandy.put(o, value);
}
return evercandy;
}
//自定义得到每个糖出现的次数
private static int getCount(Candyes o ,List<Candyes> box){
int index=-1;
int count=0;
while( (index=box.indexOf(o))!=-1 ){
count++;
box.remove(index) ;
}
return count;
}
}
class Candyes{
private String colour;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((colour == null) ? 0 : colour.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Candyes other = (Candyes) obj;
if (colour == null) {
if (other.colour != null)
return false;
} else if (!colour.equals(other.colour))
return false;
return true;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public Candyes(String colour) {
super();
this.colour = colour;
}
@Override
public String toString() {
return colour+"糖" ;
}
}