修改java中的列表对象

时间:2017-09-26 06:06:38

标签: java

我有一个对象列表 -

List<Object> sample = new ArrayList<Object>(); 

它包含以下值 -

[This is an apple., Ram is a boy., What is your name?]. 

我想像这样修改它 -

[{ "q": "This is an apple." }, { "q": "Ram is a boy" }, { "q": "What is your name?" }]

怎么做?

P.S - 我是Java新手。感谢您的帮助。

3 个答案:

答案 0 :(得分:3)

您可以通过创建JSONObject列表来执行此操作,如下所示:

List<JSONObject> yourList = new ArrayList<JSONObject>();
for(Object obj: sample) {
    JSONObject jsonObj = new JSONObject();
    jsonObj.put("q",(String)obj);
    yourList.add(jsonObj);
}

上面的代码将创建一个名为yourList的新列表,其中包含所需的数据格式。

答案 1 :(得分:2)

您可以将自定义模型用作:

class CustomObject {
   String q;
   ...getters, setters etc 
}

并进一步使用

List<CustomObject> = new ArrayList<CustomObject>();

答案 2 :(得分:0)

如果您使用的是jdk8或更高版本,请执行以下操作:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RoboAnimate
{
public partial class Form1 : Form
{
    string RxString;
    string c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, 
    c16, c17;
    string fourbytesHexStr;
    UInt32 fourbytesHex = 0;
    byte A, B, C, D, E, F,timer1val;

    bool do1=false, do2 = false, do3 = false, do4 = false, do5 = false, do6 
    = false, sequence1 = true;

    private void timer1_Tick(object sender, EventArgs e)
    {
        timer1val++;
        textBox4.Text = timer1val.ToString();
        if (timer1val == 2) Do1();
        if (timer1val == 5) Do2();
        if (timer1val == 7) Do3();
        if (timer1val == 9) Do4();
        if (timer1val == 11) Do5();
        if (timer1val == 13) Do6();
        if (timer1val == 15) Do7();
        if (timer1val == 17) Do8();
        if (timer1val == 19) Do7();
        if (timer1val == 21) Do6();
        if (timer1val == 23) Do5();
        if (timer1val == 25) Do4();
        if (timer1val == 27) Do3();
        if (timer1val == 29) Do2();
        if (timer1val == 32)
        {
            Do1();
            sequence1 = false;
            timer1.Enabled = false;
            timer1val = 0;
        }
    }

    byte whichdo;

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void Pos2_Click(object sender, EventArgs e)
    {
        //                                 Gripper
        //                                    A   B   C   D   E   F     
        byte[] M1bytesToSend = new byte[8] { 160, 30, 10, 20, 30, 24, 0x91, 
        0xCA };
        serialPort1.Write(M1bytesToSend, 0, 8);
    }

    private void Pos3_Click(object sender, EventArgs e)
    {
        //                                 Gripper
        //                                    A   B   C   D   E   F     
        byte[] M1bytesToSend = new byte[8] { 160, 10, 10, 20, 30, 24, 0x91, 
        0xCA };
        serialPort1.Write(M1bytesToSend, 0, 8);
    }

    private void Pos4_Click(object sender, EventArgs e)
    {
        //                                 Gripper
        //                                    A   B   C   D   E   F     
        byte[] M1bytesToSend = new byte[8] { 160, 10, 30, 60, 40, 24, 0x91, 
        0xCA };
        serialPort1.Write(M1bytesToSend, 0, 8);
    }

    private void Pos5_Click(object sender, EventArgs e)
    {
        //                                 Gripper
        //                                    A   B   C   D   E   F     
        byte[] M1bytesToSend = new byte[8] { 160, 60, 30, 60, 40, 24, 0x91, 
         0xCA };
        serialPort1.Write(M1bytesToSend, 0, 8);
    }

    private void Pos6_Click(object sender, EventArgs e)
    {
        //                                 Gripper
        //                                    A   B   C   D   E   F     
        byte[] M1bytesToSend = new byte[8] { 90, 60, 30, 60, 40, 24, 0x91, 
     0xCA };
        serialPort1.Write(M1bytesToSend, 0, 8);
    }

    private void Pos7_Click(object sender, EventArgs e)
    {
        //                                 Gripper
        //                                    A   B   C   D   E   F     
        byte[] M1bytesToSend = new byte[8] { 90, 100, 60, 100, 40, 80, 0x91, 
      0xCA };
        serialPort1.Write(M1bytesToSend, 0, 8);
    }

    private void Pos8_Click(object sender, EventArgs e)
    {
        //                                 Gripper
        //                                    A   B   C   D   E   F     
        byte[] M1bytesToSend = new byte[8] { 95, 90, 130, 100, 40, 80, 0x91, 
         0xCA };
        serialPort1.Write(M1bytesToSend, 0, 8);
    }

    private void ScrollA_Scroll(object sender, ScrollEventArgs e)
    {
        ServoA.Text = ScrollA.Value.ToString();
    }

    private void ScrollB_Scroll(object sender, ScrollEventArgs e)
    {
        ServoB.Text = ScrollB.Value.ToString();
    }

    private void ScrollC_Scroll(object sender, ScrollEventArgs e)
    {
        ServoC.Text = ScrollC.Value.ToString();
    }

    private void ScrollD_Scroll(object sender, ScrollEventArgs e)
    {
        ServoD.Text = ScrollD.Value.ToString();
    }

    private void ScrollE_Scroll(object sender, ScrollEventArgs e)
    {
        ServoE.Text = ScrollE.Value.ToString();
    }

    private void ScrollF_Scroll(object sender, ScrollEventArgs e)
    {
        ServoF.Text = ScrollF.Value.ToString();
    }

    private void Send_Click(object sender, EventArgs e)
    {
        A = Convert.ToByte(ServoA.Text);
        B = Convert.ToByte(ServoB.Text);
        C = Convert.ToByte(ServoC.Text);
        D = Convert.ToByte(ServoD.Text);
        E = Convert.ToByte(ServoE.Text);
        F = Convert.ToByte(ServoF.Text);
        //                                    A     B     C     D     E     

        //byte[] M1bytesToSend = new byte[8] { 0x4A, 0x14, 0x0A, 0x3C, 0x64, 
        0x4B, 0x91, 0xCA };   
        byte[] M1bytesToSend = new byte[8] { A, B, C, D, E, F, 0x91, 0xCA };
        serialPort1.Write(M1bytesToSend, 0, 8);
    }

    private void serialPort1_DataReceived(object sender, 
     SerialDataReceivedEventArgs e)
    {
        Thread.Sleep(50);
        int i;
        int bytes = serialPort1.BytesToRead;
        byte[] buffer = new byte[bytes];
        string[] hex = new string[bytes];
        serialPort1.Read(buffer, 0, bytes);


        RxString = ByteArrayToHexString(buffer);
        for (i = 0; i < bytes; ++i)
        {
            hex[i] = buffer[i].ToString("X");
        }
        textBox2.Text = RxString;
        textBox3.Text = bytes.ToString();

        if (bytes == 9)
        {
            c1 = (hex[0]).PadLeft(2, '0');
            c2 = (hex[1]).PadLeft(2, '0');
            c3 = (hex[2]).PadLeft(2, '0');
            c4 = (hex[3]).PadLeft(2, '0');
            c5 = (hex[4]).PadLeft(2, '0');
            c6 = (hex[5]).PadLeft(2, '0');
            c7 = (hex[6]).PadLeft(2, '0');
            c8 = (hex[7]).PadLeft(2, '0');
            c9 = (hex[8]).PadLeft(2, '0');

            fourbytesHexStr = string.Concat(c1, c2, c3, c4, c5, c6, c7, c8, 
            c9);
            fourbytesHexStr = fourbytesHexStr.PadRight(8, '0');
            textBox1.Text = fourbytesHexStr;
            textBox3.Text = bytes.ToString();
            serialPort1.DiscardInBuffer();
        }

        if (bytes == 8)
        {
            c1 = (hex[0]).PadLeft(2, '0');
            c2 = (hex[1]).PadLeft(2, '0');
            c3 = (hex[2]).PadLeft(2, '0');
            c4 = (hex[3]).PadLeft(2, '0');
            c5 = (hex[4]).PadLeft(2, '0');
            c6 = (hex[5]).PadLeft(2, '0');
            c7 = (hex[6]).PadLeft(2, '0');
            c8 = (hex[7]).PadLeft(2, '0');


            fourbytesHexStr = string.Concat(c1, c2, c3, c4, c5, c6, c7, c8);
            fourbytesHexStr = fourbytesHexStr.PadRight(8, '0');
            textBox1.Text = fourbytesHexStr;
            textBox3.Text = bytes.ToString();
            serialPort1.DiscardInBuffer();
        }

        if (bytes == 7)
        {
            c1 = (hex[0]).PadLeft(2, '0');
            c2 = (hex[1]).PadLeft(2, '0');
            c3 = (hex[2]).PadLeft(2, '0');
            c4 = (hex[3]).PadLeft(2, '0');
            c5 = (hex[4]).PadLeft(2, '0');
            c6 = (hex[5]).PadLeft(2, '0');
            c7 = (hex[6]).PadLeft(2, '0');



            fourbytesHexStr = string.Concat(c1, c2, c3, c4, c5, c6, c7);
            fourbytesHexStr = fourbytesHexStr.PadRight(8, '0');
            textBox1.Text = fourbytesHexStr;
            textBox3.Text = bytes.ToString();
            serialPort1.DiscardInBuffer();
        }
    }

    private string ByteArrayToHexString(byte[] buffer)
    {
        StringBuilder sb = new StringBuilder(buffer.Length * 3);
        foreach (byte b in buffer)
            sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' 
       '));
        return sb.ToString().ToUpper();
    }

    private void Pos1_Click(object sender, EventArgs e)
    {
        //                                 Gripper
        //                                    A   B   C   D   E   F     
        byte[] M1bytesToSend = new byte[8] { 115, 95, 80, 21, 31, 2, 0x91, 
     0xCA };
        serialPort1.Write(M1bytesToSend, 0, 8);
    }

    private void Open_Click(object sender, EventArgs e)
    {
        serialPort1.PortName = "COM6";
        serialPort1.BaudRate = 9600;
        serialPort1.DataBits = 8;
        serialPort1.Parity = Parity.None;
        serialPort1.StopBits = StopBits.One;
        serialPort1.Open();

        if (serialPort1.IsOpen) PortStatus.Text = "PORT Opened...Click to 
        Start DAQ";
        Close.Enabled = true;
        Open.Enabled = false;
    }

    public Form1()
    {
        InitializeComponent();
        whichdo = 100;
    }

    private void Animate1_Click(object sender, EventArgs e)
    {
        timer1.Enabled = true;
    }


    private void Do1()
    {
        byte[] M1bytesToSend = new byte[8] { 115, 95, 80, 21, 31, 2, 0x91, 
    0xCA };
        serialPort1.Write(M1bytesToSend, 0, 8);
    }

    private void Do2()
    {
        byte[] M2bytesToSend = new byte[8] { 160, 30, 10, 20, 30, 24, 0x91, 
       0xCA };
        serialPort1.Write(M2bytesToSend, 0, 8);
    }

    private void Do3()
    {
        byte[] M3bytesToSend = new byte[8] { 160, 10, 10, 20, 30, 24, 0x91, 
      0xCA };
        serialPort1.Write(M3bytesToSend, 0, 8);
    }

    private void Do4()
    {
        byte[] M4bytesToSend = new byte[8] { 160, 10, 30, 60, 40, 24, 0x91, 
     0xCA };
        serialPort1.Write(M4bytesToSend, 0, 8);
    }

    private void Do5()
    {
        byte[] M5bytesToSend = new byte[8] { 160, 60, 30, 60, 40, 24, 0x91, 
     0xCA };
        serialPort1.Write(M5bytesToSend, 0, 8);
    }

    private void Do6()
    {
        byte[] M6bytesToSend = new byte[8] { 90, 60, 30, 60, 40, 24, 0x91, 
      0xCA };
        serialPort1.Write(M6bytesToSend, 0, 8);
    }

    private void Do7()
    {
        byte[] M7bytesToSend = new byte[8] { 90, 100, 60, 100, 40, 80, 0x91, 
      0xCA };
        serialPort1.Write(M7bytesToSend, 0, 8);
    }

    private void Do8()
    {
        byte[] M8bytesToSend = new byte[8] { 95, 90, 130, 100, 40, 80, 0x91, 
     0xCA };
        serialPort1.Write(M8bytesToSend, 0, 8);
    }




}
}

修改:您可以使用import java.util.*; import java.util.stream.*; public class HelloWorld{ public static void main(String []args){ List<String> sample = new ArrayList<String>(Arrays.asList("This is an apple.", "Ram is a boy.", "What is your name?")); System.out.println(sample); List<Map<String,String>> transformed = sample.stream() .map(p -> {Map<String,String> m = new HashMap<String,String>(); m.put("q",p); return m;}) .collect(Collectors.toList()); System.out.println(transformed); } } 代替List<Map<String,String>>转换,也可以使用列表中所需的键值对。