2016-04-20 4 views
0

Ich bin ein wenig mit meiner Bewerbung beschäftigt, und ich bin mir nicht sicher, wonach ich suchen soll. Ich hoffe also, dass jemand hier mir helfen kann.Wie teilt man eine Zeichenfolge in eine Karte mit einer Liste von Werten?

ich eine Liste von String s haben, sieht das wie folgt aus:

Cake;carrot 
Cake;apple 
Cake;spicy 
Pizza;pepperoni 
Pizza;mozzarella 

... und so weiter. Ich möchte diese Daten in eine Map<String, List<String>> setzen, wo und Pizza die Schlüssel in meinem Map bilden werden. Mit [carrot, apple, spicy] als Cake Werte und [pepperoni, mozzarella] als Pizza Werte.

Wie kann ich das erreichen? Vielen Dank im Voraus für jede Hilfe.

+0

Der Eingang ist eine Liste ? –

+0

Ja, meine Eingabe ist eine 'Liste '. – veleda

Antwort

3

Gerade iterieren Ihre Liste mit String.split()

ArrayList<String> myList; 
HashMap<String, List<String>> myMap = new HashMap<>(); 
for(String s : myList) 
{ 
    String[] split = s.split(";"); 
    List<String> bucket = myMap.get(split[0]); 
    if(bucket == null) 
    { 
     bucket = new ArrayList<String>(); 
     myMap.put(split[0], bucket); 
    } 

    bucket.add(split[1]); 
} 
+0

Splendid! Vielen Dank! :) – veleda

0

du versuchen können, verwenden Sie einen hashmap, speichern aufeinanderfolgende Strings mit (Leerzeichen) als Trennzeichen, schließlich die Zeichenfolge aufgeteilt, wenn Sie es als eine Liste wollen

//Assuming your list to be the variable 'list' 
HashMap<String,String> hm = new HashMap<>(); 
for(val : list){ 
    String st[] = val.split(";"); 
    if(hm.get(st[0])==null){ 
     hm.put(st[0],st[1]); 
    } 
    else{ 
     hm.put(st[0],hm.get(st[0])+" "+st[1]); 
    } 
} 

wenn Sie die String-Array von wollen sagen pizza damals

String pizz[] = (hm.get("pizza")).split(" "); 

pizz[] wird Ihr Array haben, Prost!

+0

Sie fehlen den Objekttyp in der for-each-Schleife-Deklaration – Nadir

0

In klassischen Asp habe ich tat dies

<% 
i=0 
a=Split(request.querystring("id1"),"/") 
for each x in a 
i=i+1 
if i=1 then 
id1=x 
elseif i=2 then 
id2=x 
elseif i=3 then 
id3=x 
elseif i=4 then 
id4=x 
elseif i=5 then 
id5=x 
elseif i=6 then 
id6=x 
elseif i=7 then 
id7=x 
elseif i=8 then 
id8=x 
elseif i=9 then 
id9=x 
elseif i=10 then 
id10=x 
else 
end if 
next 
%> 

eine meiner url ist

http://www.mrraja.com/a/b/c/d/e/f/g/h/i/j

Und meine Web.Config-Datei ist

<?xml version="1.0"?> 
<configuration> 
<system.webServer> 
<rewrite> 
<rules> 
<rule name="DeduplicateSlashes" stopProcessing="true"> 
<match url="." ignoreCase="false" /> 
<conditions> 
<add input="{URL}" pattern="^(.*)//(.*)$" ignoreCase="false" /> 
</conditions> 
<action type="Redirect" redirectType="Permanent" url="{C:1}/{C:2}" /> 
</rule> 
<rule name="CleanRouting" stopProcessing="true"> 
<match url="^.*$" ignoreCase="false" /> 
<conditions> 
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" /> 
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" /> 
</conditions> 
<action type="Rewrite" url="?id1={R:0}&amp;{QUERY_STRING}" appendQueryString="false" /> 
</rule> 
</rules> 
</rewrite> 
<!-- Double escaping is needed for URLs with '+', e.g. for the account page for a username with a space. --> 
<security> 
<requestFiltering allowDoubleEscaping="true" /> 
</security> 
<staticContent> 
<remove fileExtension=".woff2" /> 
<mimeMap fileExtension=".woff2" mimeType="application/font-woff2" /> 
</staticContent> 
<httpProtocol> 
<customHeaders> 
<add name="Access-Control-Allow-Origin" value="*" /> 
</customHeaders> 
</httpProtocol> 
<modules runAllManagedModulesForAllRequests="true"/> 
<directoryBrowse enabled="true"/> 
</system.webServer> 
</configuration> 
0
public static void main(String[] args) { 
    List<String> data = new ArrayList<String>(); 
    Map<String, List<String>> finalData = new HashMap<String,List<String>>(); 
    data.add("Cake;carrot"); 
    data.add("Cake;apple"); 
    data.add("Cake;spicy"); 
    data.add("Pizza;pepperoni"); 
    data.add("Pizza;mozzarella"); 
    for (String dataString : data) { 
     List<String> temp = null; 
     if (finalData.get(dataString.split(";")[0]) == null) { 
      temp = new ArrayList<String>(); 
      temp.add(dataString.split(";")[1]); 
      finalData.put(dataString.split(";")[0], temp); 
     } else { 
      temp = finalData.get(dataString.split(";")[0]); 
      temp.add(dataString.split(";")[1]); 
      finalData.put(dataString.split(";")[0], temp); 
     } 
    } 
    System.out.println(new Gson().toJson(finalData)); 
} 

Komplette Arbeits Lösung.

Verwandte Themen