Java集合
我需要解决这个任务。我想知道在这种情况下,什么样的集合最适合使用……而且,我很想放弃集合,而选择一个类来保存列表。请告诉我如何处理这件事 > 这就是我目前所拥有的。。。。然而,hashmap正在被覆盖,我相信这是意料之中的。鲍勃·琼斯读了第一行,后来被玛丽代替了。我想知道LinkedHashMap是否能够解决这个问题,或者我需要换一个收藏。谢谢Java集合,java,arrays,algorithm,collections,Java,Arrays,Algorithm,Collections,我需要解决这个任务。我想知道在这种情况下,什么样的集合最适合使用……而且,我很想放弃集合,而选择一个类来保存列表。请告诉我如何处理这件事 > 这就是我目前所拥有的。。。。然而,hashmap正在被覆盖,我相信这是意料之中的。鲍勃·琼斯读了第一行,后来被玛丽代替了。我想知道LinkedHashMap是否能够解决这个问题,或者我需要换一个收藏。谢谢 public void printOutput(String fileName){ String category = null; String
public void printOutput(String fileName){
String category = null;
String [] legalCategory ={"PERSON", "PLACE", "ANIMAL", "COMPUTER", "OTHER"};
List<String> mapList = new ArrayList<String>();
Map<String, String> categoryMap = new LinkedHashMap<String, String>();
try(BufferedReader br = new BufferedReader(new FileReader(fileName))) {
StringBuilder sb = new StringBuilder();
String line = null;
line = br.readLine();
sb.append(line);
sb.append(System.lineSeparator());
while (line != null) {
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreElements()) {
category = st.nextToken();
for (String element : legalCategory) {
if (category.equalsIgnoreCase(element)) {
categoryMap.put(category, line.substring(category.length(), line.length()));
break;
}
}
}
line = br.readLine();
}
String everything = sb.toString();
}
catch(Exception e){
e.printStackTrace();
}
public void打印输出(字符串文件名){
字符串类别=空;
String[]legalCategory={“人”、“地”、“动物”、“计算机”、“其他”};
List mapList=new ArrayList();
Map categoryMap=新建LinkedHashMap();
try(BufferedReader br=new BufferedReader(new FileReader(fileName))){
StringBuilder sb=新的StringBuilder();
字符串行=null;
line=br.readLine();
某人附加(行);
sb.append(System.lineSeparator());
while(line!=null){
StringTokenizer st=新的StringTokenizer(行);
而(st.hasMoreElements()){
类别=st.nextToken();
for(字符串元素:legalCategory){
if(类别等信号情况(元素)){
categoryMap.put(category,line.substring(category.length(),line.length());
打破
}
}
}
line=br.readLine();
}
String everything=sb.toString();
}
捕获(例外e){
e、 printStackTrace();
}
===========================================================================
这是完整的程序。穆罕默德的回答帮助很大
public void printOutput(String fileName){
String category = null;
String [] legalCategory ={"PERSON", "PLACE", "ANIMAL", "COMPUTER", "OTHER"};
List<String> mapList = new ArrayList<String>();
Map<String, String> categoryMap = new LinkedHashMap<String, String>();
List<Category> categoryList = new ArrayList<Category>();
boolean keyFound = false;
try(BufferedReader br = new BufferedReader(new FileReader(fileName))) {
StringBuilder sb = new StringBuilder();
String line = null;
line = br.readLine();
sb.append(line);
sb.append(System.lineSeparator());
while (line != null) {
StringTokenizer st = new StringTokenizer(line);
keyFound = false;
for(Category indCategory:categoryList){
if(indCategory.getCategoryKey().equalsIgnoreCase(line))
keyFound=true;
}
while (st.hasMoreElements()) {
category = st.nextToken();
for (String element : legalCategory) {
if (category.equalsIgnoreCase(element)) {
if(!keyFound){
categoryList.add(new Category(line, category, line.substring(category.length(), line.length())));
//categoryMap.put(category, line.substring(category.length(), line.length()));
break;
}
}
}
}
line = br.readLine();
}
String everything = sb.toString();
for(Category indCategory:categoryList){
System.out.println(indCategory.getCategoryItem() + " " + indCategory.getCategorySubItem());
}
int occurrences = 0;
System.out.println("Category" + "\t" + "Count" );
for(String categoryItem:legalCategory){
occurrences = 0;
for(Category indCategory:categoryList){
//System.out.println(indCategory.getCategoryItem() + " " + indCategory.getCategorySubItem());
if(categoryItem.equalsIgnoreCase(indCategory.getCategoryItem()))
occurrences +=1;
}
System.out.println(categoryItem + "\t" + occurrences );
}
// System.out.println("PERSON" + "\t" + Collections.frequency(categoryList, "PERSON") + categoryList.size() );
}
catch(Exception e){
e.printStackTrace();
}
}
public class Category{
String categoryKey;
String categoryItem;
String categorySubItem;
String occurrence;
public Category(String categoryKey, String categoryItem, String categorySubItem){
this.categoryKey = categoryKey;
this.categoryItem = categoryItem;
this.categorySubItem = categorySubItem;
}
/**
* @return the categoryKey
*/
public String getCategoryKey() {
return categoryKey;
}
/**
* @param categoryKey the categoryKey to set
*/
public void setCategoryKey(String categoryKey) {
this.categoryKey = categoryKey;
}
/**
* @return the categoryItem
*/
public String getCategoryItem() {
return categoryItem;
}
/**
* @param categoryItem the categoryItem to set
*/
public void setCategoryItem(String categoryItem) {
this.categoryItem = categoryItem;
}
/**
* @return the categorySubItem
*/
public String getCategorySubItem() {
return categorySubItem;
}
/**
* @param categorySubItem the categorySubItem to set
*/
public void setCategorySubItem(String categorySubItem) {
this.categorySubItem = categorySubItem;
}
}
public void打印输出(字符串文件名){
字符串类别=空;
String[]legalCategory={“人”、“地”、“动物”、“计算机”、“其他”};
List mapList=new ArrayList();
Map categoryMap=新建LinkedHashMap();
List categoryList=新建ArrayList();
布尔键发现=假;
try(BufferedReader br=new BufferedReader(new FileReader(fileName))){
StringBuilder sb=新的StringBuilder();
字符串行=null;
line=br.readLine();
某人附加(行);
sb.append(System.lineSeparator());
while(line!=null){
StringTokenizer st=新的StringTokenizer(行);
keyFound=false;
对于(类别:类别列表){
if(indCategory.getCategoryKey().equalsIgnoreCase(行))
keyfind=true;
}
而(st.hasMoreElements()){
类别=st.nextToken();
for(字符串元素:legalCategory){
if(类别等信号情况(元素)){
如果(!keyFound){
添加(新类别(行,类别,行.子字符串(Category.length(),line.length()));
//categoryMap.put(category,line.substring(category.length(),line.length());
打破
}
}
}
}
line=br.readLine();
}
String everything=sb.toString();
对于(类别indCategory:categoryList){
System.out.println(indCategory.getCategoryItem()+“”+indCategory.getCategorySubItem());
}
int=0;
System.out.println(“类别”+“\t”+“计数”);
对于(字符串类别项:legalCategory){
出现次数=0;
对于(类别indCategory:categoryList){
//System.out.println(indCategory.getCategoryItem()+“”+indCategory.getCategorySubItem());
if(categoryItem.equalsIgnoreCase(indCategory.getCategoryItem()))
出现次数+=1;
}
System.out.println(categoryItem+“\t”+出现次数);
}
//System.out.println(“PERSON”+“\t”+Collections.frequency(categoryList,“PERSON”)+categoryList.size());
}
捕获(例外e){
e、 printStackTrace();
}
}
公共类类别{
字符串类别;
字符串类别项;
串分类子项;
字符串出现;
公共类别(字符串categoryKey、字符串categoryItem、字符串categorySubItem){
this.categoryKey=categoryKey;
this.categoryItem=categoryItem;
this.categorySubItem=categorySubItem;
}
/**
*@returnthecategorykey
*/
公共字符串getCategoryKey(){
返回类别;
}
/**
*@param categoryKey要设置的类别
*/
public void setCategoryKey(字符串categoryKey){
这个
public void printOutput(String fileName){
String category = null;
String [] legalCategory ={"PERSON", "PLACE", "ANIMAL", "COMPUTER", "OTHER"};
List<String> mapList = new ArrayList<String>();
Map<String, String> categoryMap = new LinkedHashMap<String, String>();
List<Category> categoryList = new ArrayList<Category>();
boolean keyFound = false;
try(BufferedReader br = new BufferedReader(new FileReader(fileName))) {
StringBuilder sb = new StringBuilder();
String line = null;
line = br.readLine();
sb.append(line);
sb.append(System.lineSeparator());
while (line != null) {
StringTokenizer st = new StringTokenizer(line);
keyFound = false;
for(Category indCategory:categoryList){
if(indCategory.getCategoryKey().equalsIgnoreCase(line))
keyFound=true;
}
while (st.hasMoreElements()) {
category = st.nextToken();
for (String element : legalCategory) {
if (category.equalsIgnoreCase(element)) {
if(!keyFound){
categoryList.add(new Category(line, category, line.substring(category.length(), line.length())));
//categoryMap.put(category, line.substring(category.length(), line.length()));
break;
}
}
}
}
line = br.readLine();
}
String everything = sb.toString();
for(Category indCategory:categoryList){
System.out.println(indCategory.getCategoryItem() + " " + indCategory.getCategorySubItem());
}
int occurrences = 0;
System.out.println("Category" + "\t" + "Count" );
for(String categoryItem:legalCategory){
occurrences = 0;
for(Category indCategory:categoryList){
//System.out.println(indCategory.getCategoryItem() + " " + indCategory.getCategorySubItem());
if(categoryItem.equalsIgnoreCase(indCategory.getCategoryItem()))
occurrences +=1;
}
System.out.println(categoryItem + "\t" + occurrences );
}
// System.out.println("PERSON" + "\t" + Collections.frequency(categoryList, "PERSON") + categoryList.size() );
}
catch(Exception e){
e.printStackTrace();
}
}
public class Category{
String categoryKey;
String categoryItem;
String categorySubItem;
String occurrence;
public Category(String categoryKey, String categoryItem, String categorySubItem){
this.categoryKey = categoryKey;
this.categoryItem = categoryItem;
this.categorySubItem = categorySubItem;
}
/**
* @return the categoryKey
*/
public String getCategoryKey() {
return categoryKey;
}
/**
* @param categoryKey the categoryKey to set
*/
public void setCategoryKey(String categoryKey) {
this.categoryKey = categoryKey;
}
/**
* @return the categoryItem
*/
public String getCategoryItem() {
return categoryItem;
}
/**
* @param categoryItem the categoryItem to set
*/
public void setCategoryItem(String categoryItem) {
this.categoryItem = categoryItem;
}
/**
* @return the categorySubItem
*/
public String getCategorySubItem() {
return categorySubItem;
}
/**
* @param categorySubItem the categorySubItem to set
*/
public void setCategorySubItem(String categorySubItem) {
this.categorySubItem = categorySubItem;
}
}