我正在尝试添加ArrayList到List。getSalesOrderitm()是Salesorder项目列表。
Criteria cr = session.createCriteria(SalesOrder.class);
cr.add(Restrictions.ge("date", new SimpleDateFormat("yyyy/MM/dd").parse(fdate)));
cr.add(Restrictions.le("date", new SimpleDateFormat("yyyy/MM/dd").parse(tdate)));
List<SalesOrder> orderList = cr.list();
List<SalesOrderItem> itemList = Collections.EMPTY_LIST;
double totalAmount = 0.00;
for (SalesOrder salesOrder : orderList) {
itemList.addAll(salesOrder.getSalesorderyitm());
}
Run Code Online (Sandbox Code Playgroud)
下面您可以在Salesorder类中看到休眠映射
@OneToMany(targetEntity=SalesOrderItem.class,mappedBy="salesorder",fetch=FetchType.LAZY)
public List<SalesOrderItem> getSalesorderyitm() {
return salesorderyitm;
}
public void setSalesorderyitm(List<SalesOrderItem> salesorderyitm) {
this.salesorderyitm = salesorderyitm;
}
Run Code Online (Sandbox Code Playgroud)
在下面您可以看到salesOrderItem类中的休眠映射
@ManyToOne
@JoinColumn(name="SOI_SalesOrderId")
public SalesOrder getSalesorder() {
return salesorder;
}
public void setSalesorder(SalesOrder salesorder) {
this.salesorder = salesorder;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108) …Run Code Online (Sandbox Code Playgroud) 我想知道如果我findAny()没有发现任何巧合,我可以在数据流中使用它,它不会返回null.
String CountryFinal= "Spain";
List<ParContriesTO> listContries= new ArrayList<SelectItem>();
listContries.add(new SelectItem(215, "Germany"));
Integer idCountry = (int) listContries.stream()
.filter(country -> country.getNoCountry().equals(CountryFinal))
.findAny().orElse(null).getCoCountry();
Run Code Online (Sandbox Code Playgroud) 我有一个包含以下数据的集合:
John eats candy
Richard runs around in the morning
Hannah drinks water
Run Code Online (Sandbox Code Playgroud)
我将每个单词分开,将其添加到数组中并得到:
[John, eats, candy]
[Richard, runs, around, in, the, morning]
[Hannah, drinks, water]
Run Code Online (Sandbox Code Playgroud)
输出应该是(第二个单词按字母顺序排序)
[Hannah, drinks, water]
[John, eats, candy]
[Richard, runs, around, in, the, morning]
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
List<List<String>> mainList = new ArrayList<>();
List<String> list = new ArrayList<>();
for (String s : array) {
String[] x = s.split(" ");
for (int i = 0; i < x.length; i++) {
list.add(x[i]);
}
mainList.add(list);
list = new ArrayList<>();
}
Run Code Online (Sandbox Code Playgroud)
但我不知道接下来该做什么.如何实现元素的比较,并在此基础上更换线条.将字符串分成数组以访问第二个元素是否正确?即使你只是提出建议,我也会很高兴.
我在使用我的代码时遇到了一些麻烦,我会给你一个简单的例子(虽然它会更复杂一点,这个简单的代码也不能正常工作).
class Sign {
private String char;
private Integer freq;
public Sign(String c) {
this.char = c;
}
@Override
public boolean equals(Object o) {
String check = (String)o;
return check.equals(this.char);
}
@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + this.char.hashCode();
return hash;
}
}
Run Code Online (Sandbox Code Playgroud)
我假设为了简单起见,总是会有一个等于equals方法的String.还有一些hashCode()也可以确保contains()方法可以工作,这里是测试本身:
ArrayList<Sign> queueOfSigns = new ArrayList<>();
Sign test = new Sign("C");
String c = "C";
queueOfSigns.add(test);
if(queueOfSigns.contains("C"))
System.out.println("I am here!");
Run Code Online (Sandbox Code Playgroud)
无论如何,这个简单的测试代码在这种情况下总是返回false - 所以"我在这里"消息永远不会出现.我一直在尝试一些不同的方法来处理我的代码,但这是因为这个想法是从String文本中获取单个字符并检查ArrayList中是否已存在单个字符.尽管如此 - 如果没有让这个简单的测试正常工作,我就无法继续前进,所以我想问你 - 我错过了什么.这是我第一次使用equals()和hashCode()方法来使我自己的对象与contains()方法一起正常工作.
我有一个字符串数组定义如下:
string[] items = { "Item 1", "Item 2", "Item 3", "Item 4"};
Run Code Online (Sandbox Code Playgroud)
然后我将模型定义为:
public class ItemModel {
public int Id { get; set; }
public string ItemName { get; set; }
public bool IsItem { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我已将列表定义为:
var listItems = new List<ItemModel>();
Run Code Online (Sandbox Code Playgroud)
我想将数组中的项添加items到对象列表中listItems.我想要添加项目ItemName
我正在使用csv文件构建基本数据库,我正在测试select函数,当我遇到一些奇怪的东西时.
private ArrayList<Record> selectField(String selectTerm)
{
Log.log("Selection " + selectTerm,2,"DB_io");
ArrayList<Record> ret = new ArrayList<Record>();
if (titleRow.values.contains(selectTerm))
{
Log.log("Adding values to " + selectTerm);
int ordinal = titleRow.values.indexOf(selectTerm);
Log.log("Ordinal " + ordinal);
List<String> tempList = new ArrayList<String>();
for (Record r : data)
{
List<String> tempList = new ArrayList<String>();
tempList.add(r.values.get(ordinal));
Record s = new Record(tempList);
ret.add(s);
tempList.clear();
}
Log.log("Number of records in ret " + ret.size());
for (Record t : ret)
{
Log.log(t.toString());
}
}
else
{
Log.log("keyField does not contain …Run Code Online (Sandbox Code Playgroud) 我创建了一个具有各种属性的student对象的arrayList.这些属性中最喜欢的颜色.所以在这个n学生的arraylist中,每个学生都有一个字符串favoriteColor成员变量.
Set<student> studentUnique = new HashSet<student>(studentList);
for (user key : studentUnique) {
System.out.println(key + ": " + Collections.frequency(studentList, key));
}
Run Code Online (Sandbox Code Playgroud)
我想计算所说颜色的频率,例如有一百名学生,可能输出:
red: 50
blue: 20
green: 30
Run Code Online (Sandbox Code Playgroud)
我将学生的arrayList(studentList)放入一个hashmap,但我不知道如何编写我的频率语句来获得喜欢各自颜色的学生的频率.
我有按学生排序的对象列表id:
List<Student> arraylist = new ArrayList<>();
arraylist.add(new Student(1, "Chaitanya", 26));
arraylist.add(new Student(2, "Chaitanya", 26));
arraylist.add(new Student(3, "Rahul", 24));
arraylist.add(new Student(4, "Ajeet", 32));
arraylist.add(new Student(5, "Chaitanya", 26));
arraylist.add(new Student(6, "Chaitanya", 26));
Run Code Online (Sandbox Code Playgroud)
我想使用流并只删除学生age等于的前三个元素26.你能帮帮我吗?
一些背景:我是Java的新手,我正在学习基础java课程.我目前正在进行该课程的最终项目,并完成了除最后一段代码之外的所有内容.出于某种原因,我有最艰难的时间从数组列表中删除元素.这是我正在处理的代码:
public static void delete(String bookID) {
for (book eachElement : catalog) {
if (eachElement.getBookID().equals(bookID)) {
catalog.remove(eachElement);
return;
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码执行,没有运行时错误但它不会删除任何内容.另外,我知道在remove语句之前一切正常,因为我有另一种方法,使用与select bookID字符串相同的for和if语句来计算计算.
我希望能够转换ArrayList<String>存储从BufferedReader读取的文件内容的内容,然后将内容转换为byte [],以允许使用Java的Cipher类对其进行加密.
我尝试过使用.getBytes()但它没有工作,因为我认为我需要首先转换ArrayList,而我在弄清楚如何做到这一点时遇到了麻烦.
码:
// File variable
private static String file;
// From main()
file = args[2];
private static void sendData(SecretKey desedeKey, DataOutputStream dos) throws Exception {
ArrayList<String> fileString = new ArrayList<String>();
String line;
String userFile = file + ".txt";
BufferedReader in = new BufferedReader(new FileReader(userFile));
while ((line = in.readLine()) != null) {
fileString.add(line.getBytes()); //error here
}
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, desedeKey);
byte[] output = cipher.doFinal(fileString.getBytes("UTF-8")); //error here
dos.writeInt(output.length);
dos.write(output);
System.out.println("Encrypted Data: " + Arrays.toString(output)); …Run Code Online (Sandbox Code Playgroud) arraylist ×10
java ×9
arrays ×3
collections ×2
java-stream ×2
byte ×1
c# ×1
contains ×1
encryption ×1
hashmap ×1
linq ×1
list ×1
sorting ×1
string ×1