简体   繁体   English

比较String与我的ArrayList

[英]Compare String with my ArrayList

I have this ArrayList: 我有这个ArrayList:

List<String> zainteresowanka = new ArrayList<String>();
zainteresowanka.add("Java");
zainteresowanka.add("Platforma .NET (C#)");
zainteresowanka.add("Android");
zainteresowanka.add("iOS");
zainteresowanka.add("Windows Phone");
zainteresowanka.add("Technologie WWW (HTML, CSS, JavaScript)");
zainteresowanka.add("Testowanie aplikacji");
zainteresowanka.add("Projektowanie aplikacji");
zainteresowanka.add("Analiza Biznesowa");
zainteresowanka.add("Inne");

And those are things that someone can choose what he likes. 这些就是某人可以选择自己喜欢的东西的东西。 If he choose "Inne" , then he must enter some other thing that he likes that this ArrayList doesn't contain. 如果他选择“ Inne” ,那么他必须输入他喜欢此ArrayList不包含的其他内容。

It is written to database like that: 它将这样写入数据库:

  1. Platforma .NET (C#) 2. iOS 3. Windows Phone Platforma .NET(C#)2. iOS 3. Windows Phone

in one string. 一串。

The method that saves it to a string looks like this: 将其保存到字符串的方法如下所示:

 zainteresowaniaa = zainteresowaniaa + " " + licz + ". " + zainteresowanka.get(wyboor);  
 licz = licz + 1;

What I need to do is to search the list based on the ArrayList zainteresowanka , so I can choose for example "Java, iOS" and I get everyone who had choosen those ones. 我需要做的是基于ArrayList zainteresowanka搜索列表,因此我可以选择“ Java,iOS”,然后得到每个选择这些列表的人。

How can I do this? 我怎样才能做到这一点?

From what i understand 据我了解

You have an arraylist that contains items that can be chosen by users.You will save the chosen items to database . 您有一个arraylist,其中包含用户可以选择的项目。您将选择的项目保存到数据库。 Then you need to get the users who selected some item like "windows phone" for example 然后,您需要获取选择了某些项目(例如“ Windows Phone”)的用户

First of all you need to assign an id for different users to distinguish them. 首先,您需要为不同的用户分配一个ID,以区分它们。

Store the details in database with userid 使用用户标识将详细信息存储在数据库中

for example 例如

 ==============================
 userid | chosen items
 ==============================
  1      | windows  ios android  
  2      | android ios
  ===============================

Then you need to search in the database ( assuming mysql or sql like ) for the specific items (value1,value2) . 然后,您需要在数据库中(假设使用mysql或sql之类)在数据库中搜索特定项(value1,value2)。 You can use "LIKE" or "IN" operator 您可以使用“ LIKE”或“ IN”运算符

you can use SQL IN operator to search multiple absolute values: 您可以使用SQL IN运算符搜索多个绝对值:

SELECT userid FROM tablename WHERE name IN ( 'Value1', 'Value2', ... ); SELECT userid FROM tablename WHERE name IN('Value1','Value2',...);

If you want to use LIKE you will need to use OR instead: 如果要使用LIKE,则需要改用OR:

SELECT userid FROM tablename WHERE name LIKE '%Value1' OR name LIKE '%Value2'; 从表名中选择用户ID,其中名称为“%Value1”或名称为“%Value2”; Using AND (as you tried) requires ALL conditions to be true, using OR requires at least one to be true. 使用AND(您尝试过)要求所有条件为真,而使用OR至少需要一个条件为真。

Here you will get the list of userid and hence users who have chooses specific items value1,value2 在这里,您将获得用户ID列表,因此将选择特定项的用户value1,value2

for Hibernate 对于休眠

You can use Native sql queries or create query . 您可以使用本机sql查询或创建查询。 But search feature is best implemented by Hibernate search query DSL 但是搜索功能最好由Hibernate搜索查询DSL实现

Easy way would be to use query 简单的方法是使用查询

Query query = session.createQuery("SELECT u FROM UserData u WHERE u.userRole IN (:roles)");
query.setParameterList("roles", roles);

more info in the link below 更多信息在下面的链接

http://w3facility.org/question/jpa-get-all-rows-that-matches-multiple-strings/ http://w3facility.org/question/jpa-get-all-rows-that-matches-multiple-strings/

Better,Correct, but advanced way could be to use Hibernate Search query DSL 更好,更正确但更高级的方法可能是使用休眠搜索查询DSL

You can search multiple keywords on multiple fields with this 您可以使用此功能在多个字段中搜索多个关键字

Basic syntax 基本语法

Query luceneQuery = queryBuilder
    .keyword()
      .wildcard()
    .onField("foo")
    .matching("bar*")
    .createQuery();

More info 更多信息

http://hibernate.org/search/documentation/getting-started/ http://hibernate.org/search/documentation/getting-started/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM