- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在制作一个返回 TreeMap<String, ArrayList<String>
的方法。持有 definitions(key=term,value=definition)
。
由于某种原因,在我创建该方法后,它放置了一个以 A
开头的术语。在 keySet
的末尾。我以为TreeMap
按排序顺序在哪里?这只是第一个项目,所有其他项目都按顺序排列。
真的很烦我,因为我看不到我的错误在哪里。
主要:
public static void main(String[] args)
{
//Create new object
FileParser fp = new FileParser();
//call to method
Map<String, ArrayList<String>> output = fp.createGlossary("glossary.txt");
//print output
for(Map.Entry<String,ArrayList<String>> entry : output.entrySet())
{
String key = entry.getKey();
ArrayList<String> value = entry.getValue();
System.out.println(key + " => " + value);
}
}
方法:
public Map<String, ArrayList<String>> createGlossary(String fileName)
{
//Local variables that are equal to a parsed file of terms and definitions
ArrayList<String> termList = this.parseTerms(fileName);
ArrayList<ArrayList<String>> defList = this.parsedDefinitions(this.rawDefinitions(fileName));
//Map to to hold above variables
Map<String, ArrayList<String >> glossary = new TreeMap<>();
//Iterators to loop through the terms and definitions
Iterator<String> termIterator = termList.iterator();
Iterator<ArrayList<String>> defIterator = defList.iterator();
System.out.println(glossary);
//As both terms and definitions are the same length iterate until both are exhausted
while(termIterator.hasNext() && defIterator.hasNext())
{
//put key-value pair using term as the key and definition as the value.
String key = termIterator.next().toUpperCase();
ArrayList<String> value = defIterator.next();
glossary.put(key, value);
}
return glossary;
}
第一个和最后一个项目的输出:
//First Item in Map
ABSTRACT CLASS => [A, class, that, defines, a, common, message, protocol, and, common, set, of, instance, variables, for, its, subclasses., In, Java, an, abstract, class, cannot, be, instantiated.]
//Some K-V pairs from middle
SIGNATURE => [See, method, signature.]
SOFTWARE => [A, general, term, for, all, programs, that, can, be, run, on, a, desktop, computer, or, another, hardware, platform,, such, as, a, mobile, phone.]
SOFTWARE COMPONENT => [A, piece, of, software, that, can, be, readily, combined, with, other, components, to, construct, more, complex, software.]
SORTED COLLECTION => [A, collection, that, always, keeps, its, elements, in, their, natural, ordering,, if, any., This, contrasts, with, an, ordered, collection,, where, the, ordering, may, just, be, an, accident, of, where, elements, happen, to, have, been, placed, in, the, collection.]
SOURCE CODE => [Program, text, expressed, in, a, high-level, programming, language.]
STABLE SORT => [A, sort, that, does, not, reorder, equal, elements.]
//Last Item in Map
ABSOLUTE PATHNAME => [A, full, path, to, a, file, beginning, from, the, name, of, the, storage, device.]
我没有复制全部内容,因为有数百行。但是,其他一切都井然有序吗?
这几乎就像将 ABSOLUTE PATHNAME 推到了 map 的末尾?
编辑:阅读评论后,我已经加入了类(class),所以也许可以进行娱乐 Activity ......readFileIn() 方法只是一次读取每一行......如果该行是偶数,则有一个定义,如果该行是奇数,则它是一个术语......希望足以重新创建错误。抱歉,未注释的代码转储。
再次感谢。保罗
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class FileParser
{
private int numberOfTerms;
public void setNumberOfTerms(int numberOfTerms) { this.numberOfTerms = numberOfTerms; }
public ArrayList<String> readFileIn(String fileName)
{
File file = new File(fileName);
StringBuilder parsedFile = new StringBuilder();
try
{
Scanner glossaryTXTFile = new Scanner(file);
while (glossaryTXTFile.hasNextLine()) {
String line = glossaryTXTFile.nextLine();
parsedFile.append(line);
parsedFile.append("\n");
}
glossaryTXTFile.close();
parsedFile.setLength(parsedFile.length() - 1);
return new ArrayList<>(Arrays.asList(parsedFile.toString().split("\n")));
}
catch (FileNotFoundException e)
{
System.out.println("File not found, please check the path and make sure extension is included.");
e.printStackTrace();
return null;
}
}
public ArrayList<String> rawDefinitions(String fileName)
{
ArrayList<String> definitions = new ArrayList<>();
int counter = 1;
for(String holder : this.readFileIn(fileName))
{
if(counter % 2 == 0)
{
definitions.add(holder);
}
counter++;
}
return definitions;
}
public ArrayList<ArrayList<String>> parsedDefinitions(ArrayList<String> sentence)
{
ArrayList<ArrayList<String>> choppedSentences = new ArrayList<>();
for(String words : sentence)
{
String[] splitSentences = words.split(" ");
choppedSentences.add(new ArrayList(Arrays.asList(splitSentences)));
}
this.setNumberOfTerms(choppedSentences.size());
return choppedSentences;
}
public ArrayList<String> parseTerms(String fileName)
{
ArrayList<String> terms = new ArrayList<>();
int counter = 1;
for(String holder : this.readFileIn(fileName))
{
if(counter % 2 != 0)
{
terms.add(holder);
}
counter++;
}
return terms;
}
public Map<String, ArrayList<String>> createGlossary(String fileName)
{
//Local variables that are equal to a parsed file of terms and definitions
ArrayList<String> termList = this.parseTerms(fileName);
ArrayList<ArrayList<String>> defList = this.parsedDefinitions(this.rawDefinitions(fileName));
//Map to to hold above variables
Map<String, ArrayList<String >> glossary = new TreeMap<>();
//Iterators to loop through the terms and definitions
Iterator<String> termIterator = termList.iterator();
Iterator<ArrayList<String>> defIterator = defList.iterator();
System.out.println(glossary);
//As both terms and definitions are the same length iterate until both are exhausted
while(termIterator.hasNext() && defIterator.hasNext())
{
//put key-value pair using term as the key and definition as the value.
String key = termIterator.next().toUpperCase();
ArrayList<String> value = defIterator.next();
glossary.put(key, value);
}
return glossary;
}
public static void main(String[] args)
{
//Create new object
FileParser fp = new FileParser();
//call to method
Map<String, ArrayList<String>> output = fp.createGlossary("glossary.txt");
//print output
for(Map.Entry<String,ArrayList<String>> entry : output.entrySet())
{
String key = entry.getKey();
ArrayList<String> value = entry.getValue();
System.out.println(key + " => " + value);
}
}
}
最佳答案
根据代码,我确信您的文本文件有一些不可见的字符,这会在 Treemap
排序中产生问题。
考虑以下示例,
public class App {
public static void main(String[] args) {
Map<String, List<Integer>> treeMap = new TreeMap<>();
treeMap.put("ABSTRACT CLASS", Arrays.asList(1, 2, 3, 4));
treeMap.put("\u200eABSOLUTE PATHNAME", Arrays.asList(11, 22, 33, 44));
for (Entry<String, List<Integer>> entry : treeMap.entrySet()) {
System.out.println(entry);
}
}
}
输出
ABSTRACT CLASS=[1, 2, 3, 4]
ABSOLUTE PATHNAME=[11, 22, 33, 44]
确保您的文本文件具有正确的内容。要检查不可见字符,您可以使用 Notepad++,只需打开文件并启用查看 > 显示符号 > 显示所有字符。
关于java - 为什么我的 TreeMap 将以 'A' 开头的键移动到 keyValueSet 的末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48313056/
我目前正在制作一个返回 TreeMap 的方法。持有 definitions(key=term,value=definition) 。 由于某种原因,在我创建该方法后,它放置了一个以 A 开头的术语。
我是一名优秀的程序员,十分优秀!